Skip to content

Commit

Permalink
A bunch of linting and refactorings
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Henderson <[email protected]>
  • Loading branch information
hairyhenderson committed Oct 27, 2023
1 parent a296ab9 commit df2daf0
Show file tree
Hide file tree
Showing 59 changed files with 403 additions and 306 deletions.
23 changes: 14 additions & 9 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
linters-settings:
govet:
check-shadowing: true
enable:
- fieldalignment
enable-all: true
golint:
min-confidence: 0
gocyclo:
Expand All @@ -11,7 +10,7 @@ linters-settings:
threshold: 100
goconst:
min-len: 2
min-occurrences: 6
min-occurrences: 5
ignore-tests: true
lll:
line-length: 140
Expand All @@ -28,7 +27,7 @@ linters:
- bodyclose
# - dogsled
# - dupl
# - errcheck
- errcheck
# - exhaustive
- exportloopref
# - funlen
Expand All @@ -41,7 +40,7 @@ linters:
# - gocyclo
# - godox
- gofmt
# - gofumpt
- gofumpt
- goheader
- goimports
# - gomnd
Expand All @@ -56,8 +55,8 @@ linters:
- nakedret
- nestif
# - nlreturn
# - noctx
# - nolintlint
- noctx
- nolintlint
- prealloc
- revive
- rowserrcheck
Expand All @@ -66,11 +65,17 @@ linters:
- stylecheck
- typecheck
- unconvert
# - unparam
- unparam
- unused
# - whitespace
- whitespace
# - wsl

issues:
exclude-rules:
- path: _test\.go
linters:
- errcheck

run:
concurrency: 4
timeout: 5m
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2016-2018 Dave Henderson
Copyright (c) 2016-2023 Dave Henderson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ _Please report any bugs found in the [issue tracker](https://github.com/hairyhen

[The MIT License](http://opensource.org/licenses/MIT)

Copyright (c) 2016-2022 Dave Henderson
Copyright (c) 2016-2023 Dave Henderson

[gh-actions-image]: https://github.com/hairyhenderson/gomplate/workflows/Build/badge.svg?branch=main
[gh-actions-url]: https://github.com/hairyhenderson/gomplate/actions?workflow=Build&branch=main
Expand Down
24 changes: 16 additions & 8 deletions coll/coll_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ func TestMerge(t *testing.T) {
assert.EqualValues(t, expected, out)

dst = map[string]interface{}{"a": true, "c": 5}
src = map[string]interface{}{"a": false,
src = map[string]interface{}{
"a": false,
"b": map[string]interface{}{
"ca": "foo",
},
Expand All @@ -261,7 +262,8 @@ func TestMerge(t *testing.T) {
"ca": "foo",
"cb": "bar",
},
"c": 5}
"c": 5,
}
src = map[string]interface{}{
"a": false,
"b": map[string]interface{}{
Expand Down Expand Up @@ -478,24 +480,28 @@ func TestFlatten(t *testing.T) {
{in: []int{1, 2, 3}, expected: []interface{}{1, 2, 3}},
{in: [3]int{1, 2, 3}, expected: []interface{}{1, 2, 3}},
{in: []interface{}{[]string{}, []int{1, 2}, 3}, expected: []interface{}{[]string{}, []int{1, 2}, 3}},
{in: []interface{}{[]string{"one"}, [][]int{{1, 2}}, 3},
{
in: []interface{}{[]string{"one"}, [][]int{{1, 2}}, 3},
expected: []interface{}{[]string{"one"}, [][]int{{1, 2}}, 3},
},
{depth: 1, in: []int{1, 2, 3}, expected: []interface{}{1, 2, 3}},
{depth: 1, in: [3]int{1, 2, 3}, expected: []interface{}{1, 2, 3}},
{depth: 1, in: []interface{}{[]string{}, []int{1, 2}, 3}, expected: []interface{}{1, 2, 3}},
{depth: 1,
{
depth: 1,
in: []interface{}{[]string{"one"}, [][]int{{1, 2}}, 3},
expected: []interface{}{"one", []int{1, 2}, 3},
},
{depth: 2, in: []int{1, 2, 3}, expected: []interface{}{1, 2, 3}},
{depth: 2, in: [3]int{1, 2, 3}, expected: []interface{}{1, 2, 3}},
{depth: 2, in: []interface{}{[]string{}, []int{1, 2}, 3}, expected: []interface{}{1, 2, 3}},
{depth: 2,
{
depth: 2,
in: []interface{}{[]string{"one"}, [][]int{{1, 2}}, 3},
expected: []interface{}{"one", 1, 2, 3},
},
{depth: 2,
{
depth: 2,
in: []interface{}{
[]string{"one"},
[]interface{}{
Expand All @@ -512,11 +518,13 @@ func TestFlatten(t *testing.T) {
{depth: -1, in: []int{1, 2, 3}, expected: []interface{}{1, 2, 3}},
{depth: -1, in: [3]int{1, 2, 3}, expected: []interface{}{1, 2, 3}},
{depth: -1, in: []interface{}{[]string{}, []int{1, 2}, 3}, expected: []interface{}{1, 2, 3}},
{depth: -1,
{
depth: -1,
in: []interface{}{[]string{"one"}, [][]int{{1, 2}}, 3},
expected: []interface{}{"one", 1, 2, 3},
},
{depth: -1,
{
depth: -1,
in: []interface{}{
[]string{"one"},
[]interface{}{
Expand Down
6 changes: 4 additions & 2 deletions coll/jsonpath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"github.com/stretchr/testify/require"
)

type m = map[string]interface{}
type ar = []interface{}
type (
m = map[string]interface{}
ar = []interface{}
)

func TestJSONPath(t *testing.T) {
in := m{
Expand Down
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (o *Config) defaults() *Config {
return o
}

// nolint: gocyclo
//nolint:gocyclo
func (o *Config) String() string {
o.defaults()

Expand Down
6 changes: 3 additions & 3 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ func (c *tmplctx) Env() map[string]string {
}

// createTmplContext reads the datasources for the given aliases
func createTmplContext(_ context.Context, aliases []string,
//nolint:staticcheck
d *data.Data) (interface{}, error) {
//
//nolint:staticcheck
func createTmplContext(_ context.Context, aliases []string, d *data.Data) (interface{}, error) {
var err error
tctx := &tmplctx{}
for _, a := range aliases {
Expand Down
4 changes: 0 additions & 4 deletions conv/conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,28 +158,24 @@ func ToStrings(in ...interface{}) []string {

// MustParseInt - wrapper for strconv.ParseInt that returns 0 in the case of error
func MustParseInt(s string, base, bitSize int) int64 {
// nolint: gosec
i, _ := strconv.ParseInt(s, base, bitSize)
return i
}

// MustParseFloat - wrapper for strconv.ParseFloat that returns 0 in the case of error
func MustParseFloat(s string, bitSize int) float64 {
// nolint: gosec
i, _ := strconv.ParseFloat(s, bitSize)
return i
}

// MustParseUint - wrapper for strconv.ParseUint that returns 0 in the case of error
func MustParseUint(s string, base, bitSize int) uint64 {
// nolint: gosec
i, _ := strconv.ParseUint(s, base, bitSize)
return i
}

// MustAtoi - wrapper for strconv.Atoi that returns 0 in the case of error
func MustAtoi(s string) int {
// nolint: gosec
i, _ := strconv.Atoi(s)
return i
}
Expand Down
19 changes: 12 additions & 7 deletions conv/conv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ func TestJoin(t *testing.T) {
}

func TestHas(t *testing.T) {

in := map[string]interface{}{
"foo": "bar",
"baz": map[string]interface{}{
Expand Down Expand Up @@ -315,25 +314,31 @@ func TestDict(t *testing.T) {
{expected: map[string]interface{}{}},
{
args: []interface{}{},
expected: map[string]interface{}{}},
expected: map[string]interface{}{},
},
{
args: []interface{}{"foo"},
expected: map[string]interface{}{"foo": ""}},
expected: map[string]interface{}{"foo": ""},
},
{
args: []interface{}{42},
expected: map[string]interface{}{"42": ""}},
expected: map[string]interface{}{"42": ""},
},
{
args: []interface{}{"foo", nil},
expected: map[string]interface{}{"foo": nil}},
expected: map[string]interface{}{"foo": nil},
},
{
args: []interface{}{"foo", "bar"},
expected: map[string]interface{}{"foo": "bar"}},
expected: map[string]interface{}{"foo": "bar"},
},
{
args: []interface{}{"foo", "bar", "baz", true},
expected: map[string]interface{}{
"foo": "bar",
"baz": true,
}},
},
},
}

for _, d := range testdata {
Expand Down
22 changes: 14 additions & 8 deletions crypto/ecdsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,21 @@ import (
"fmt"
)

var (
// Curves is a map of curve names to curves
Curves = map[string]elliptic.Curve{
"P224": elliptic.P224(),
"P256": elliptic.P256(),
"P384": elliptic.P384(),
"P521": elliptic.P521(),
// Curves -
func Curves(c string) (elliptic.Curve, bool) {
switch c {
case "P224":
return elliptic.P224(), true
case "P256":
return elliptic.P256(), true
case "P384":
return elliptic.P384(), true
case "P521":
return elliptic.P521(), true
default:
return nil, false
}
)
}

// ECDSAGenerateKey -
func ECDSAGenerateKey(curve elliptic.Curve) ([]byte, error) {
Expand Down
14 changes: 6 additions & 8 deletions data/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package data

import (
"fmt"
"os"
"testing"
"time"

Expand All @@ -10,8 +11,6 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"os"

"gotest.tools/v3/fs"
)

Expand Down Expand Up @@ -65,12 +64,14 @@ escaped: "\"\/\\\b\f\n\r\t\u221e"
}

func TestUnmarshalArray(t *testing.T) {
expected := []interface{}{"foo", "bar",
expected := []interface{}{
"foo", "bar",
map[string]interface{}{
"baz": map[string]interface{}{"qux": true},
"quux": map[string]interface{}{"42": 18},
"corge": map[string]interface{}{"false": "blah"},
}}
},
}

test := func(actual []interface{}, err error) {
require.NoError(t, err)
Expand Down Expand Up @@ -167,15 +168,13 @@ func TestToJSONBytes(t *testing.T) {
assert.Error(t, err)
}

type badObject struct {
}
type badObject struct{}

func (b *badObject) CodecEncodeSelf(_ *codec.Encoder) {
panic("boom")
}

func (b *badObject) CodecDecodeSelf(_ *codec.Decoder) {

}

func TestToJSON(t *testing.T) {
Expand Down Expand Up @@ -505,7 +504,6 @@ true = true
assert.Equal(t, expected, out)
}

//nolint:gosec
func TestDecryptEJSON(t *testing.T) {
privateKey := "e282d979654f88267f7e6c2d8268f1f4314b8673579205ed0029b76de9c8223f"
publicKey := "6e05ec625bcdca34864181cc43e6fcc20a57732a453bc2f4a2e117ffdf1a6762"
Expand Down
2 changes: 1 addition & 1 deletion data/datasource_aws_sm.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func parseDatasourceURLArgs(sourceURL *url.URL, args ...string) (params map[stri
return params, p, nil
}

func readAWSSecretsManager(ctx context.Context, source *Source, args ...string) (output []byte, err error) {
func readAWSSecretsManager(ctx context.Context, source *Source, args ...string) ([]byte, error) {
if source.awsSecretsManager == nil {
source.awsSecretsManager = secretsmanager.New(gaws.SDKSession())
}
Expand Down
4 changes: 2 additions & 2 deletions data/datasource_aws_sm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func TestAWSSecretsManager_ReadSecret(t *testing.T) {
},
})

output, err := readAWSSecretsManagerParam(context.Background(), s, "/foo/bar")
output, err := readAWSSecretsManager(context.Background(), s, "/bar")
assert.True(t, calledOk)
require.NoError(t, err)
assert.Equal(t, []byte("blub"), output)
Expand All @@ -170,7 +170,7 @@ func TestAWSSecretsManager_ReadSecretBinary(t *testing.T) {
},
})

output, err := readAWSSecretsManagerParam(context.Background(), s, "/foo/bar")
output, err := readAWSSecretsManager(context.Background(), s, "/bar")
assert.True(t, calledOk)
require.NoError(t, err)
assert.Equal(t, []byte("supersecret"), output)
Expand Down
1 change: 0 additions & 1 deletion data/datasource_awssmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func readAWSSMPParam(ctx context.Context, source *Source, paramPath string) ([]b
}

response, err := source.asmpg.GetParameterWithContext(ctx, input)

if err != nil {
return nil, fmt.Errorf("error reading aws+smp from AWS using GetParameter with input %v: %w", input, err)
}
Expand Down
Loading

0 comments on commit df2daf0

Please sign in to comment.