From 6af93cd2bd89d38ade8d9384fe3798aed1a38a65 Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Sat, 4 Feb 2023 20:48:57 -0500 Subject: Remove uses of pkg/errors Signed-off-by: Dave Henderson --- data/data.go | 26 ++++++++++++-------------- data/data_test.go | 13 ++++++------- data/datasource.go | 16 +++++++--------- data/datasource_awssmp.go | 6 +++--- data/datasource_blob.go | 15 +++++++++------ data/datasource_file.go | 11 +++++------ data/datasource_http.go | 7 +++---- data/datasource_merge.go | 11 +++++------ data/datasource_stdin.go | 5 ++--- data/datasource_vault.go | 5 ++--- 10 files changed, 54 insertions(+), 61 deletions(-) (limited to 'data') diff --git a/data/data.go b/data/data.go index 96f82e7f..6952b41c 100644 --- a/data/data.go +++ b/data/data.go @@ -12,16 +12,14 @@ import ( "io" "strings" - "github.com/joho/godotenv" - "github.com/Shopify/ejson" ejsonJson "github.com/Shopify/ejson/json" "github.com/hairyhenderson/gomplate/v3/conv" "github.com/hairyhenderson/gomplate/v3/env" + "github.com/joho/godotenv" // XXX: replace once https://github.com/BurntSushi/toml/pull/179 is merged "github.com/hairyhenderson/toml" - "github.com/pkg/errors" "github.com/ugorji/go/codec" "github.com/hairyhenderson/yaml" @@ -30,7 +28,7 @@ import ( func unmarshalObj(obj map[string]interface{}, in string, f func([]byte, interface{}) error) (map[string]interface{}, error) { err := f([]byte(in), &obj) if err != nil { - return nil, errors.Wrapf(err, "Unable to unmarshal object %s", in) + return nil, fmt.Errorf("unable to unmarshal object %s: %w", in, err) } return obj, nil } @@ -38,7 +36,7 @@ func unmarshalObj(obj map[string]interface{}, in string, f func([]byte, interfac func unmarshalArray(obj []interface{}, in string, f func([]byte, interface{}) error) ([]interface{}, error) { err := f([]byte(in), &obj) if err != nil { - return nil, errors.Wrapf(err, "Unable to unmarshal array %s", in) + return nil, fmt.Errorf("unable to unmarshal array %s: %w", in, err) } return obj, nil } @@ -67,12 +65,12 @@ func decryptEJSON(in string) (map[string]interface{}, error) { rOut := &bytes.Buffer{} err := ejson.Decrypt(rIn, rOut, keyDir, key) if err != nil { - return nil, errors.WithStack(err) + return nil, err } obj := make(map[string]interface{}) out, err := unmarshalObj(obj, rOut.String(), yaml.Unmarshal) if err != nil { - return nil, errors.WithStack(err) + return nil, err } delete(out, ejsonJson.PublicKeyField) return out, nil @@ -337,7 +335,7 @@ func ToCSV(args ...interface{}) (string, error) { var ok bool delim, ok = args[0].(string) if !ok { - return "", errors.Errorf("Can't parse ToCSV delimiter (%v) - must be string (is a %T)", args[0], args[0]) + return "", fmt.Errorf("can't parse ToCSV delimiter (%v) - must be string (is a %T)", args[0], args[0]) } args = args[1:] } @@ -355,12 +353,12 @@ func ToCSV(args ...interface{}) (string, error) { for i, v := range a { ar, ok := v.([]interface{}) if !ok { - return "", errors.Errorf("Can't parse ToCSV input - must be a two-dimensional array (like [][]string or [][]interface{}) (was %T)", args[0]) + return "", fmt.Errorf("can't parse ToCSV input - must be a two-dimensional array (like [][]string or [][]interface{}) (was %T)", args[0]) } in[i] = conv.ToStrings(ar...) } default: - return "", errors.Errorf("Can't parse ToCSV input - must be a two-dimensional array (like [][]string or [][]interface{}) (was %T)", args[0]) + return "", fmt.Errorf("can't parse ToCSV input - must be a two-dimensional array (like [][]string or [][]interface{}) (was %T)", args[0]) } } b := &bytes.Buffer{} @@ -378,7 +376,7 @@ func ToCSV(args ...interface{}) (string, error) { func marshalObj(obj interface{}, f func(interface{}) ([]byte, error)) (string, error) { b, err := f(obj) if err != nil { - return "", errors.Wrapf(err, "Unable to marshal object %s", obj) + return "", fmt.Errorf("unable to marshal object %s: %w", obj, err) } return string(b), nil @@ -390,7 +388,7 @@ func toJSONBytes(in interface{}) ([]byte, error) { buf := new(bytes.Buffer) err := codec.NewEncoder(buf, h).Encode(in) if err != nil { - return nil, errors.Wrapf(err, "Unable to marshal %s", in) + return nil, fmt.Errorf("unable to marshal %s: %w", in, err) } return buf.Bytes(), nil } @@ -413,7 +411,7 @@ func ToJSONPretty(indent string, in interface{}) (string, error) { } err = json.Indent(out, b, "", indent) if err != nil { - return "", errors.Wrapf(err, "Unable to indent JSON %s", b) + return "", fmt.Errorf("unable to indent JSON %s: %w", b, err) } return out.String(), nil @@ -440,7 +438,7 @@ func ToTOML(in interface{}) (string, error) { buf := new(bytes.Buffer) err := toml.NewEncoder(buf).Encode(in) if err != nil { - return "", errors.Wrapf(err, "Unable to marshal %s", in) + return "", fmt.Errorf("unable to marshal %s: %w", in, err) } return buf.String(), nil } diff --git a/data/data_test.go b/data/data_test.go index 07593ea4..6fd47b8b 100644 --- a/data/data_test.go +++ b/data/data_test.go @@ -1,13 +1,12 @@ package data import ( + "fmt" "testing" "time" "github.com/ugorji/go/codec" - "github.com/pkg/errors" - "github.com/stretchr/testify/assert" "os" @@ -59,9 +58,9 @@ escaped: "\"\/\\\b\f\n\r\t\u221e" obj := make(map[string]interface{}) _, err := unmarshalObj(obj, "SOMETHING", func(in []byte, out interface{}) error { - return errors.New("fail") + return fmt.Errorf("fail") }) - assert.EqualError(t, err, "Unable to unmarshal object SOMETHING: fail") + assert.EqualError(t, err, "unable to unmarshal object SOMETHING: fail") } func TestUnmarshalArray(t *testing.T) { @@ -139,9 +138,9 @@ this shouldn't be reached obj := make([]interface{}, 1) _, err = unmarshalArray(obj, "SOMETHING", func(in []byte, out interface{}) error { - return errors.New("fail") + return fmt.Errorf("fail") }) - assert.EqualError(t, err, "Unable to unmarshal array SOMETHING: fail") + assert.EqualError(t, err, "unable to unmarshal array SOMETHING: fail") } func TestMarshalObj(t *testing.T) { @@ -152,7 +151,7 @@ func TestMarshalObj(t *testing.T) { assert.NoError(t, err) assert.Equal(t, expected, actual) _, err = marshalObj(nil, func(in interface{}) ([]byte, error) { - return nil, errors.New("fail") + return nil, fmt.Errorf("fail") }) assert.Error(t, err) } diff --git a/data/datasource.go b/data/datasource.go index 6e8117d9..93dc51b9 100644 --- a/data/datasource.go +++ b/data/datasource.go @@ -12,8 +12,6 @@ import ( "github.com/spf13/afero" - "github.com/pkg/errors" - "github.com/hairyhenderson/gomplate/v3/internal/config" "github.com/hairyhenderson/gomplate/v3/libkv" "github.com/hairyhenderson/gomplate/v3/vault" @@ -70,7 +68,7 @@ func (d *Data) lookupReader(scheme string) (func(context.Context, *Source, ...st } r, ok := d.sourceReaders[scheme] if !ok { - return nil, errors.Errorf("scheme %s not registered", scheme) + return nil, fmt.Errorf("scheme %s not registered", scheme) } return r, nil } @@ -219,7 +217,7 @@ func (s *Source) mimeType(arg string) (mimeType string, err error) { if mediatype != "" { t, _, err := mime.ParseMediaType(mediatype) if err != nil { - return "", errors.Wrapf(err, "MIME type was %q", mediatype) + return "", fmt.Errorf("MIME type was %q: %w", mediatype, err) } mediatype = t return mediatype, nil @@ -237,7 +235,7 @@ func (s *Source) String() string { // DefineDatasource - func (d *Data) DefineDatasource(alias, value string) (string, error) { if alias == "" { - return "", errors.New("datasource alias must be provided") + return "", fmt.Errorf("datasource alias must be provided") } if d.DatasourceExists(alias) { return "", nil @@ -269,7 +267,7 @@ func (d *Data) lookupSource(alias string) (*Source, error) { if !ok { srcURL, err := url.Parse(alias) if err != nil || !srcURL.IsAbs() { - return nil, errors.Errorf("Undefined datasource '%s'", alias) + return nil, fmt.Errorf("undefined datasource '%s': %w", alias, err) } source = &Source{ Alias: alias, @@ -291,7 +289,7 @@ func (d *Data) readDataSource(ctx context.Context, alias string, args ...string) } b, err := d.readSource(ctx, source, args...) if err != nil { - return "", "", errors.Wrapf(err, "Couldn't read datasource '%s'", alias) + return "", "", fmt.Errorf("couldn't read datasource '%s': %w", alias, err) } subpath := "" @@ -346,7 +344,7 @@ func parseData(mimeType, s string) (out interface{}, err error) { case textMimetype: out = s default: - return nil, errors.Errorf("Datasources of type %s not yet supported", mimeType) + return nil, fmt.Errorf("datasources of type %s not yet supported", mimeType) } return out, err } @@ -378,7 +376,7 @@ func (d *Data) readSource(ctx context.Context, source *Source, args ...string) ( } r, err := d.lookupReader(source.URL.Scheme) if err != nil { - return nil, errors.Wrap(err, "Datasource not yet supported") + return nil, fmt.Errorf("Datasource not yet supported") } data, err := r(ctx, source, args...) if err != nil { diff --git a/data/datasource_awssmp.go b/data/datasource_awssmp.go index d61affe8..2c0ca9af 100644 --- a/data/datasource_awssmp.go +++ b/data/datasource_awssmp.go @@ -2,12 +2,12 @@ package data import ( "context" + "fmt" "strings" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/request" "github.com/aws/aws-sdk-go/service/ssm" - "github.com/pkg/errors" gaws "github.com/hairyhenderson/gomplate/v3/aws" ) @@ -48,7 +48,7 @@ func readAWSSMPParam(ctx context.Context, source *Source, paramPath string) ([]b response, err := source.asmpg.GetParameterWithContext(ctx, input) if err != nil { - return nil, errors.Wrapf(err, "Error reading aws+smp from AWS using GetParameter with input %v", input) + return nil, fmt.Errorf("error reading aws+smp from AWS using GetParameter with input %v: %w", input, err) } result := *response.Parameter @@ -65,7 +65,7 @@ func listAWSSMPParams(ctx context.Context, source *Source, paramPath string) ([] response, err := source.asmpg.GetParametersByPathWithContext(ctx, input) if err != nil { - return nil, errors.Wrapf(err, "Error reading aws+smp from AWS using GetParameter with input %v", input) + return nil, fmt.Errorf("error reading aws+smp from AWS using GetParameter with input %v: %w", input, err) } listing := make([]string, len(response.Parameters)) diff --git a/data/datasource_blob.go b/data/datasource_blob.go index bb316e7f..cd260d3f 100644 --- a/data/datasource_blob.go +++ b/data/datasource_blob.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "fmt" "io" "mime" "net/url" @@ -12,7 +13,6 @@ import ( gaws "github.com/hairyhenderson/gomplate/v3/aws" "github.com/hairyhenderson/gomplate/v3/env" - "github.com/pkg/errors" "gocloud.dev/blob" "gocloud.dev/blob/gcsblob" @@ -22,7 +22,7 @@ import ( func readBlob(ctx context.Context, source *Source, args ...string) (output []byte, err error) { if len(args) >= 2 { - return nil, errors.New("maximum two arguments to blob datasource: alias, extraPath") + return nil, fmt.Errorf("maximum two arguments to blob datasource: alias, extraPath") } key := source.URL.Path @@ -70,14 +70,14 @@ func newOpener(ctx context.Context, u *url.URL) (opener blob.BucketURLOpener, er case "gs": creds, err := gcp.DefaultCredentials(ctx) if err != nil { - return nil, errors.Wrap(err, "failed to retrieve GCP credentials") + return nil, fmt.Errorf("failed to retrieve GCP credentials: %w", err) } client, err := gcp.NewHTTPClient( gcp.DefaultTransport(), gcp.CredentialsTokenSource(creds)) if err != nil { - return nil, errors.Wrap(err, "failed to create GCP HTTP client") + return nil, fmt.Errorf("failed to create GCP HTTP client: %w", err) } opener = &gcsblob.URLOpener{ Client: client, @@ -90,7 +90,7 @@ func getBlob(ctx context.Context, bucket *blob.Bucket, key string) (mediaType st key = strings.TrimPrefix(key, "/") attr, err := bucket.Attributes(ctx, key) if err != nil { - return "", nil, errors.Wrapf(err, "failed to retrieve attributes for %s", key) + return "", nil, fmt.Errorf("failed to retrieve attributes for %s: %w", key, err) } if attr.ContentType != "" { mt, _, e := mime.ParseMediaType(attr.ContentType) @@ -100,7 +100,10 @@ func getBlob(ctx context.Context, bucket *blob.Bucket, key string) (mediaType st mediaType = mt } data, err = bucket.ReadAll(ctx, key) - return mediaType, data, errors.Wrapf(err, "failed to read %s", key) + if err != nil { + return "", nil, fmt.Errorf("failed to read %s: %w", key, err) + } + return mediaType, data, nil } // calls the bucket listing API, returning a JSON Array diff --git a/data/datasource_file.go b/data/datasource_file.go index e6be0440..870c93f4 100644 --- a/data/datasource_file.go +++ b/data/datasource_file.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "fmt" "io" "net/url" "os" @@ -11,8 +12,6 @@ import ( "strings" "github.com/spf13/afero" - - "github.com/pkg/errors" ) func readFile(ctx context.Context, source *Source, args ...string) ([]byte, error) { @@ -39,7 +38,7 @@ func readFile(ctx context.Context, source *Source, args ...string) ([]byte, erro // make sure we can access the file i, err := source.fs.Stat(p) if err != nil { - return nil, errors.Wrapf(err, "Can't stat %s", p) + return nil, fmt.Errorf("stat %s: %w", p, err) } if strings.HasSuffix(p, string(filepath.Separator)) { @@ -47,19 +46,19 @@ func readFile(ctx context.Context, source *Source, args ...string) ([]byte, erro if i.IsDir() { return readFileDir(source, p) } - return nil, errors.Errorf("%s is not a directory", p) + return nil, fmt.Errorf("%s is not a directory", p) } f, err := source.fs.OpenFile(p, os.O_RDONLY, 0) if err != nil { - return nil, errors.Wrapf(err, "Can't open %s", p) + return nil, fmt.Errorf("openFile %s: %w", p, err) } defer f.Close() b, err := io.ReadAll(f) if err != nil { - return nil, errors.Wrapf(err, "Can't read %s", p) + return nil, fmt.Errorf("readAll %s: %w", p, err) } return b, nil } diff --git a/data/datasource_http.go b/data/datasource_http.go index 1061a3a1..23c7dc36 100644 --- a/data/datasource_http.go +++ b/data/datasource_http.go @@ -2,13 +2,12 @@ package data import ( "context" + "fmt" "io" "mime" "net/http" "net/url" "time" - - "github.com/pkg/errors" ) func buildURL(base *url.URL, args ...string) (*url.URL, error) { @@ -17,7 +16,7 @@ func buildURL(base *url.URL, args ...string) (*url.URL, error) { } p, err := url.Parse(args[0]) if err != nil { - return nil, errors.Wrapf(err, "bad sub-path %s", args[0]) + return nil, fmt.Errorf("bad sub-path %s: %w", args[0], err) } return base.ResolveReference(p), nil } @@ -48,7 +47,7 @@ func readHTTP(ctx context.Context, source *Source, args ...string) ([]byte, erro return nil, err } if res.StatusCode != 200 { - err := errors.Errorf("Unexpected HTTP status %d on GET from %s: %s", res.StatusCode, source.URL, string(body)) + err := fmt.Errorf("unexpected HTTP status %d on GET from %s: %s", res.StatusCode, source.URL, string(body)) return nil, err } ctypeHdr := res.Header.Get("Content-Type") diff --git a/data/datasource_merge.go b/data/datasource_merge.go index 136a3779..af188844 100644 --- a/data/datasource_merge.go +++ b/data/datasource_merge.go @@ -2,12 +2,11 @@ package data import ( "context" + "fmt" "strings" "github.com/hairyhenderson/gomplate/v3/coll" "github.com/hairyhenderson/gomplate/v3/internal/config" - - "github.com/pkg/errors" ) // readMerge demultiplexes a `merge:` datasource. The 'args' parameter currently @@ -24,7 +23,7 @@ func (d *Data) readMerge(ctx context.Context, source *Source, args ...string) ([ opaque := source.URL.Opaque parts := strings.Split(opaque, "|") if len(parts) < 2 { - return nil, errors.New("need at least 2 datasources to merge") + return nil, fmt.Errorf("need at least 2 datasources to merge") } data := make([]map[string]interface{}, len(parts)) for i, part := range parts { @@ -45,12 +44,12 @@ func (d *Data) readMerge(ctx context.Context, source *Source, args ...string) ([ b, err := d.readSource(ctx, subSource) if err != nil { - return nil, errors.Wrapf(err, "Couldn't read datasource '%s'", part) + return nil, fmt.Errorf("couldn't read datasource '%s': %w", part, err) } mimeType, err := subSource.mimeType("") if err != nil { - return nil, errors.Wrapf(err, "failed to read datasource %s", subSource.URL) + return nil, fmt.Errorf("failed to read datasource %s: %w", subSource.URL, err) } data[i], err = parseMap(mimeType, string(b)) @@ -95,7 +94,7 @@ func parseMap(mimeType, data string) (map[string]interface{}, error) { case map[string]interface{}: m = datum default: - return nil, errors.Errorf("unexpected data type '%T' for datasource (type %s); merge: can only merge maps", datum, mimeType) + return nil, fmt.Errorf("unexpected data type '%T' for datasource (type %s); merge: can only merge maps", datum, mimeType) } return m, nil } diff --git a/data/datasource_stdin.go b/data/datasource_stdin.go index 4592b209..98fbefb3 100644 --- a/data/datasource_stdin.go +++ b/data/datasource_stdin.go @@ -2,10 +2,9 @@ package data import ( "context" + "fmt" "io" "os" - - "github.com/pkg/errors" ) func readStdin(ctx context.Context, source *Source, args ...string) ([]byte, error) { @@ -13,7 +12,7 @@ func readStdin(ctx context.Context, source *Source, args ...string) ([]byte, err b, err := io.ReadAll(stdin) if err != nil { - return nil, errors.Wrapf(err, "Can't read %s", stdin) + return nil, fmt.Errorf("can't read %s: %w", stdin, err) } return b, nil } diff --git a/data/datasource_vault.go b/data/datasource_vault.go index 09f6dcb1..ea996e61 100644 --- a/data/datasource_vault.go +++ b/data/datasource_vault.go @@ -2,10 +2,9 @@ package data import ( "context" + "fmt" "strings" - "github.com/pkg/errors" - "github.com/hairyhenderson/gomplate/v3/vault" ) @@ -41,7 +40,7 @@ func readVault(ctx context.Context, source *Source, args ...string) (data []byte } if len(data) == 0 { - return nil, errors.Errorf("no value found for path %s", p) + return nil, fmt.Errorf("no value found for path %s", p) } return data, nil -- cgit v1.2.3