Skip to content

Commit

Permalink
Remove uses of pkg/errors
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Henderson <[email protected]>
  • Loading branch information
hairyhenderson committed Feb 5, 2023
1 parent 08d70cf commit 6af93cd
Show file tree
Hide file tree
Showing 29 changed files with 137 additions and 160 deletions.
8 changes: 4 additions & 4 deletions coll/jsonpath.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package coll

import (
"fmt"
"reflect"

"github.com/pkg/errors"
"k8s.io/client-go/util/jsonpath"
)

// JSONPath -
func JSONPath(p string, in interface{}) (interface{}, error) {
jp, err := parsePath(p)
if err != nil {
return nil, errors.Wrapf(err, "couldn't parse JSONPath %s", p)
return nil, fmt.Errorf("couldn't parse JSONPath %s: %w", p, err)
}
results, err := jp.FindResults(in)
if err != nil {
return nil, errors.Wrap(err, "executing JSONPath failed")
return nil, fmt.Errorf("executing JSONPath failed: %w", err)
}

var out interface{}
Expand Down Expand Up @@ -59,5 +59,5 @@ func extractResult(v reflect.Value) (interface{}, error) {
return v.Interface(), nil
}

return nil, errors.Errorf("JSONPath couldn't access field")
return nil, fmt.Errorf("JSONPath couldn't access field")
}
5 changes: 2 additions & 3 deletions conv/conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"

iconv "github.com/hairyhenderson/gomplate/v3/internal/conv"
"github.com/pkg/errors"
)

// Bool converts a string to a boolean value, using strconv.ParseBool under the covers.
Expand Down Expand Up @@ -90,7 +89,7 @@ func Join(in interface{}, sep string) (out string, err error) {
if !ok {
a, err = iconv.InterfaceSlice(in)
if err != nil {
return "", errors.Wrap(err, "input to Join must be an array")
return "", fmt.Errorf("input to Join must be an array: %w", err)
}
ok = true
}
Expand All @@ -102,7 +101,7 @@ func Join(in interface{}, sep string) (out string, err error) {
return strings.Join(b, sep), nil
}

return "", errors.New("input to Join must be an array")
return "", fmt.Errorf("input to Join must be an array")
}

// Has determines whether or not a given object has a property with the given key
Expand Down
7 changes: 3 additions & 4 deletions crypto/pbkdf2.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import (
"crypto/sha1" //nolint: gosec
"crypto/sha256"
"crypto/sha512"
"fmt"
"hash"

"github.com/pkg/errors"

"golang.org/x/crypto/pbkdf2"
)

Expand Down Expand Up @@ -43,15 +42,15 @@ func StrToHash(hash string) (crypto.Hash, error) {
case "SHA512_256", "SHA512/256", "SHA-512_256", "SHA-512/256":
return crypto.SHA512_256, nil
}
return 0, errors.Errorf("no such hash %s", hash)
return 0, fmt.Errorf("no such hash %s", hash)
}

// PBKDF2 - Run the Password-Based Key Derivation Function #2 as defined in
// RFC 8018 (PKCS #5 v2.1)
func PBKDF2(password, salt []byte, iter, keylen int, hashFunc crypto.Hash) ([]byte, error) {
h, ok := hashFuncs[hashFunc]
if !ok {
return nil, errors.Errorf("hashFunc not supported: %v", hashFunc)
return nil, fmt.Errorf("hashFunc not supported: %v", hashFunc)
}
return pbkdf2.Key(password, salt, iter, keylen, h), nil
}
26 changes: 12 additions & 14 deletions data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -30,15 +28,15 @@ 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
}

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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:]
}
Expand All @@ -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{}
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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
}
13 changes: 6 additions & 7 deletions data/data_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
}
Expand Down
16 changes: 7 additions & 9 deletions data/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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 := ""
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions data/datasource_awssmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand All @@ -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))
Expand Down
Loading

0 comments on commit 6af93cd

Please sign in to comment.