Skip to content

Commit

Permalink
Add gometalinter to pre-commit hook
Browse files Browse the repository at this point in the history
Enable varcheck and fix found issues

Add ineffassign to list of checks and fix found issues

Added nakedret and fixed found issues

Add interfacer check and fix found issue

Add lll and fix found issues

Add deadcode linter, remove unused code
  • Loading branch information
Oleg Atamanenko committed Jun 15, 2018
1 parent 8127b09 commit 6fd0330
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 118 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ before_install:
- go get -u github.com/onsi/ginkgo/ginkgo
- go get -u github.com/monopole/mdrip
- go get -u github.com/fzipp/gocyclo
- go get -u gopkg.in/alecthomas/gometalinter.v2 && gometalinter.v2 --install

# Install must be set to prevent default `go get` to run.
# The dependencies have already been vendored by `dep` so
Expand Down
24 changes: 19 additions & 5 deletions bin/pre-commit.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash
set -e

# Make sure, we run in the root of the repo and
# therefore run the tests on all packages
Expand Down Expand Up @@ -36,14 +37,27 @@ function testGoCyclo {
diff <(echo -n) <(go_dirs | xargs -0 gocyclo -over 15)
}

function testGoImports {
diff -u <(echo -n) <(go_dirs | xargs -0 goimports -l)
}

function testGoLint {
diff -u <(echo -n) <(go_dirs | xargs -0 golint --min_confidence 0.85 )
}

function testGoMetalinter {
diff -u <(echo -n) <(go_dirs | xargs -0 gometalinter.v2 --disable-all --deadline 5m \
--enable=misspell \
--enable=structcheck \
--enable=deadcode \
--enable=goimports \
--enable=varcheck \
--enable=goconst \
--enable=ineffassign \
--enable=nakedret \
--enable=interfacer \
--enable=misspell \
--line-length=170 --enable=lll \
--dupl-threshold=400 --enable=dupl)
}


function testGoVet {
go vet -all ./...
}
Expand All @@ -57,7 +71,7 @@ function testExamples {
}

runTest testGoFmt
runTest testGoImports
runTest testGoMetalinter
runTest testGoLint
runTest testGoVet
runTest testGoCyclo
Expand Down
4 changes: 3 additions & 1 deletion pkg/commands/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ func newCmdAddConfigMap(errOut io.Writer, fsys fs.FileSystem) *cobra.Command {
&config.FileSources,
"from-file",
[]string{},
"Key file can be specified using its file path, in which case file basename will be used as configmap key, or optionally with a key and file path, in which case the given key will be used. Specifying a directory will iterate each named file in the directory whose basename is a valid configmap key.")
"Key file can be specified using its file path, in which case file basename will be used as configmap "+
"key, or optionally with a key and file path, in which case the given key will be used. Specifying a "+
"directory will iterate each named file in the directory whose basename is a valid configmap key.")
cmd.Flags().StringArrayVar(
&config.LiteralSources,
"from-literal",
Expand Down
96 changes: 0 additions & 96 deletions pkg/commands/init.go

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/configmapandsecret/util/env_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func processEnvFileLine(line []byte, filePath string,
// from the environment.
value = os.Getenv(key)
}
return
return key, value, err
}

// addFromEnvFile processes an env file allows a generic addTo to handle the
Expand Down
2 changes: 1 addition & 1 deletion pkg/configmapandsecret/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func ParseRFC3339(s string, nowFn func() metav1.Time) (metav1.Time, error) {
}

// HashObject encodes object using given codec and returns MD5 sum of the result.
func HashObject(obj runtime.Object, codec runtime.Codec) (string, error) {
func HashObject(obj runtime.Object, codec runtime.Encoder) (string, error) {
data, err := runtime.Encode(codec, obj)
if err != nil {
return "", err
Expand Down
3 changes: 1 addition & 2 deletions pkg/diff/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@ func newProgram(out, errOut io.Writer) *program {
}

func (d *program) makeCommand(args ...string) exec.Cmd {
diff := ""
diff := "diff"
if envDiff := os.Getenv("KUBERNETES_EXTERNAL_DIFF"); envDiff != "" {
diff = envDiff
} else {
diff = "diff"
args = append([]string{"-u", "-N"}, args...)
}
cmd := exec.New().Command(diff, args...)
Expand Down
2 changes: 1 addition & 1 deletion pkg/exec/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestExecutorNoArgs(t *testing.T) {
}

cmd = ex.Command("/does/not/exist")
out, err = cmd.CombinedOutput()
_, err = cmd.CombinedOutput()
if err == nil {
t.Errorf("expected failure, got nil error")
}
Expand Down
5 changes: 0 additions & 5 deletions pkg/fs/fakefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ type FakeFile struct {
open bool
}

// makeFile makes a fake file.
func makeFile() *FakeFile {
return &FakeFile{}
}

// makeDir makes a fake directory.
func makeDir(name string) *FakeFile {
return &FakeFile{name: name, dir: true}
Expand Down
18 changes: 13 additions & 5 deletions pkg/hash/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,19 @@ func TestEncodeConfigMap(t *testing.T) {
// one key
{"one key", &v1.ConfigMap{Data: map[string]string{"one": ""}}, `{"data":{"one":""},"kind":"ConfigMap","name":""}`, ""},
// three keys (tests sorting order)
{"three keys", &v1.ConfigMap{Data: map[string]string{"two": "2", "one": "", "three": "3"}}, `{"data":{"one":"","three":"3","two":"2"},"kind":"ConfigMap","name":""}`, ""},
{"three keys", &v1.ConfigMap{Data: map[string]string{"two": "2", "one": "", "three": "3"}},
`{"data":{"one":"","three":"3","two":"2"},"kind":"ConfigMap","name":""}`, ""},
// empty binary map
{"empty data", &v1.ConfigMap{BinaryData: map[string][]byte{}}, `{"data":null,"kind":"ConfigMap","name":""}`, ""},
// one key with binary data
{"one key", &v1.ConfigMap{BinaryData: map[string][]byte{"one": []byte("")}}, `{"binaryData":{"one":""},"data":null,"kind":"ConfigMap","name":""}`, ""},
{"one key", &v1.ConfigMap{BinaryData: map[string][]byte{"one": []byte("")}},
`{"binaryData":{"one":""},"data":null,"kind":"ConfigMap","name":""}`, ""},
// three keys with binary data (tests sorting order)
{"three keys", &v1.ConfigMap{BinaryData: map[string][]byte{"two": []byte("2"), "one": []byte(""), "three": []byte("3")}}, `{"binaryData":{"one":"","three":"Mw==","two":"Mg=="},"data":null,"kind":"ConfigMap","name":""}`, ""},
{"three keys", &v1.ConfigMap{BinaryData: map[string][]byte{"two": []byte("2"), "one": []byte(""), "three": []byte("3")}},
`{"binaryData":{"one":"","three":"Mw==","two":"Mg=="},"data":null,"kind":"ConfigMap","name":""}`, ""},
// two keys, one string and one binary values
{"two keys with one each", &v1.ConfigMap{Data: map[string]string{"one": ""}, BinaryData: map[string][]byte{"two": []byte("")}}, `{"binaryData":{"two":""},"data":{"one":""},"kind":"ConfigMap","name":""}`, ""},
{"two keys with one each", &v1.ConfigMap{Data: map[string]string{"one": ""}, BinaryData: map[string][]byte{"two": []byte("")}},
`{"binaryData":{"two":""},"data":{"one":""},"kind":"ConfigMap","name":""}`, ""},
}
for _, c := range cases {
s, err := encodeConfigMap(c.cm)
Expand All @@ -129,7 +133,11 @@ func TestEncodeSecret(t *testing.T) {
// one key
{"one key", &v1.Secret{Type: "my-type", Data: map[string][]byte{"one": []byte("")}}, `{"data":{"one":""},"kind":"Secret","name":"","type":"my-type"}`, ""},
// three keys (tests sorting order) - note json.Marshal base64 encodes the values because they come in as []byte
{"three keys", &v1.Secret{Type: "my-type", Data: map[string][]byte{"two": []byte("2"), "one": []byte(""), "three": []byte("3")}}, `{"data":{"one":"","three":"Mw==","two":"Mg=="},"kind":"Secret","name":"","type":"my-type"}`, ""},
{"three keys", &v1.Secret{
Type: "my-type",
Data: map[string][]byte{"two": []byte("2"), "one": []byte(""), "three": []byte("3")},
},
`{"data":{"one":"","three":"Mw==","two":"Mg=="},"kind":"Secret","name":"","type":"my-type"}`, ""},
}
for _, c := range cases {
s, err := encodeSecret(c.secret)
Expand Down
1 change: 0 additions & 1 deletion pkg/transformers/labelsandannotations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ var secret = schema.GroupVersionKind{Version: "v1", Kind: "Secret"}
var cmap = schema.GroupVersionKind{Version: "v1", Kind: "ConfigMap"}
var ns = schema.GroupVersionKind{Version: "v1", Kind: "Namespace"}
var deploy = schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "Deployment"}
var statefulset = schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "StatefulSet"}
var foo = schema.GroupVersionKind{Group: "example.com", Version: "v1", Kind: "Foo"}
var crd = schema.GroupVersionKind{Group: "apiwctensions.k8s.io", Version: "v1beta1", Kind: "CustomResourceDefinition"}

Expand Down
3 changes: 3 additions & 0 deletions pkg/transformers/namereference_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ func TestNameReferenceRun(t *testing.T) {
}

nrt, err := NewDefaultingNameReferenceTransformer()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
err = nrt.Transform(m)
if err != nil {
t.Fatalf("unexpected error: %v", err)
Expand Down

0 comments on commit 6fd0330

Please sign in to comment.