diff --git a/.golangci.yml b/.golangci.yml index a579a69..129a0dd 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -7,6 +7,7 @@ linters: - dogsled - dupl - errcheck + - exportloopref - funlen - gochecknoinits - goconst @@ -14,7 +15,6 @@ linters: - gocyclo - gofmt - goimports - - golint - gomnd - goprintffuncname - gosec @@ -24,8 +24,10 @@ linters: - lll - misspell - nakedret + - noctx + - nolintlint + - revive - rowserrcheck - - scopelint - staticcheck - structcheck - stylecheck diff --git a/cmd/aws.go b/cmd/aws.go index 4dfe7dd..4066447 100644 --- a/cmd/aws.go +++ b/cmd/aws.go @@ -64,13 +64,11 @@ func newAWSCommand() *cobra.Command { "aws-partitions", []string{"aws"}, fmt.Sprintf("In what partitions to search for clusters. Supported %v", aws.AllowedParitions())) - AWSCommand.PersistentFlags().StringVar( &kubeconfigPath, "kubeconfig-path", kubeconfig.GetDefaultKubeconfigPath(), "Path to the kubeconfig to work with") - AWSCommand.PersistentFlags().StringVar( &alias, "context-name-alias", diff --git a/cmd/aws_list_test.go b/cmd/aws_list_test.go index c527cb3..2805908 100644 --- a/cmd/aws_list_test.go +++ b/cmd/aws_list_test.go @@ -22,7 +22,7 @@ type tableTestCase struct { } var ( - tableCases []tableTestCase = []tableTestCase{ + tableCases = []tableTestCase{ {Clusters: cluster.GetMockClusters(0)}, {Clusters: cluster.GetMockClusters(1)}, {Clusters: cluster.GetMockClusters(3)}, diff --git a/cmd/aws_test.go b/cmd/aws_test.go index e906662..5d84fe0 100644 --- a/cmd/aws_test.go +++ b/cmd/aws_test.go @@ -19,7 +19,7 @@ type testCase struct { Partitions []string } -var cases []testCase = []testCase{ +var cases = []testCase{ {[]string{"aws", "list"}, []string{"aws"}}, {[]string{"aws", "list"}, []string{"aws-cn"}}, {[]string{"aws", "list"}, []string{"aws-iso-b"}}, @@ -53,13 +53,13 @@ func TestQueryAllRegions(t *testing.T) { defer func() { log.SetOutput(os.Stdout) }() hook := test.NewGlobal() defer hook.Reset() - args := append(tt.Cmd, []string{ + tt.Cmd = append(tt.Cmd, []string{ "--log-level", "debug", "--kubeconfig-path", kubeconfigPath, "--aws-partitions", strings.Join(tt.Partitions, ","), }...) - cmd.SetArgs(args) + cmd.SetArgs(tt.Cmd) err = cmd.Execute() if err != nil { t.Error(err.Error()) diff --git a/cmd/aws_update.go b/cmd/aws_update.go index ad8c28b..4f570ed 100644 --- a/cmd/aws_update.go +++ b/cmd/aws_update.go @@ -112,6 +112,7 @@ func copy(src, dst string) error { return err } + // nolint: gomnd buf := make([]byte, 1000000) for { n, err := source.Read(buf) diff --git a/cmd/aws_update_test.go b/cmd/aws_update_test.go index 921831c..6371792 100644 --- a/cmd/aws_update_test.go +++ b/cmd/aws_update_test.go @@ -18,7 +18,7 @@ func Test_generateBackupNameNoConflict(t *testing.T) { kubeconfigPath := filepath.Join(dir, "kubeconfig") backupKubeconfigPath := filepath.Join(dir, "kubeconfig.bak") - if err := ioutil.WriteFile(kubeconfigPath, []byte("..."), 0666); err != nil { + if err := ioutil.WriteFile(kubeconfigPath, []byte("..."), 0600); err != nil { t.Error(err.Error()) } @@ -68,7 +68,7 @@ func Test_fileExistsFile(t *testing.T) { path := filepath.Join(dir, "kubeconfig") - if err := ioutil.WriteFile(path, []byte("...\n"), 0666); err != nil { + if err := ioutil.WriteFile(path, []byte("...\n"), 0600); err != nil { t.Error(err.Error()) } diff --git a/cmd/root.go b/cmd/root.go index b0e2f2f..6ca8f08 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -5,8 +5,8 @@ import ( "fmt" "io/ioutil" "os" - "strings" "path/filepath" + "strings" "github.com/mateimicu/kdiscover/internal/kubeconfig" "github.com/spf13/cobra" @@ -32,7 +32,6 @@ var ( ) func NewRootCommand(version, commit, date, commandPrefix string) *cobra.Command { - rootCmd := &cobra.Command{ Use: commandPrefix, Short: "Discover all EKS clusters on an account.", diff --git a/cmd/root_test.go b/cmd/root_test.go index 4b78428..787e965 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -13,9 +13,10 @@ import ( log "github.com/sirupsen/logrus" ) +// nolint:unused, varcheck, deadcode var update = flag.Bool("update", false, "update .golden files") -var basicCommands []struct{ cmd []string } = []struct { +var basicCommands = []struct { cmd []string }{ {[]string{"version"}}, @@ -44,12 +45,12 @@ func Test_CascadingPersistPreRunEHackWithLoggingLevels(t *testing.T) { cmd.SetOut(ioutil.Discard) cmd.SetErr(ioutil.Discard) - completCmd := append(tt.cmd, "--log-level") - completCmd = append(completCmd, k) - completCmd = append(completCmd, "--kubeconfig-path") - completCmd = append(completCmd, kubeconfigPath) + tt.cmd = append(tt.cmd, "--log-level") + tt.cmd = append(tt.cmd, k) + tt.cmd = append(tt.cmd, "--kubeconfig-path") + tt.cmd = append(tt.cmd, kubeconfigPath) - cmd.SetArgs(completCmd) + cmd.SetArgs(tt.cmd) err = cmd.Execute() if err != nil { t.Error(err.Error()) @@ -58,11 +59,11 @@ func Test_CascadingPersistPreRunEHackWithLoggingLevels(t *testing.T) { // none logging level is a special case if k == "none" { if log.StandardLogger().Out != ioutil.Discard { - t.Errorf("Running %v we were expecting logging to be discared but it is not ", completCmd) + t.Errorf("Running %v we were expecting logging to be discared but it is not ", tt.cmd) } } else { if exp != log.GetLevel() { - t.Errorf("Running %v we were expecting logger to be %v but it is %v", completCmd, exp, log.GetLevel()) + t.Errorf("Running %v we were expecting logger to be %v but it is %v", tt.cmd, exp, log.GetLevel()) } } }) @@ -82,9 +83,9 @@ func Test_HelpFunction(t *testing.T) { cmd.SetOut(buf) cmd.SetErr(buf) - completCmd := append(tt.cmd, "--help") + tt.cmd = append(tt.cmd, "--help") - cmd.SetArgs(completCmd) + cmd.SetArgs(tt.cmd) err := cmd.Execute() if err != nil { t.Error(err.Error()) @@ -93,7 +94,7 @@ func Test_HelpFunction(t *testing.T) { if !strings.Contains(buf.String(), expected) { t.Errorf( "Running %v we were expecting %v in the output but got: %v", - completCmd, expected, buf.String()) + tt.cmd, expected, buf.String()) } }) } diff --git a/cmd/version.go b/cmd/version.go index d728d53..30feb9e 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -7,8 +7,8 @@ import ( ) var ( - shortened bool = false - output string = "json" + shortened = false + output = "json" ) func newVersionCommand(version, commit, date string) *cobra.Command { diff --git a/cmd/version_test.go b/cmd/version_test.go index d7a3af6..15ec97b 100644 --- a/cmd/version_test.go +++ b/cmd/version_test.go @@ -38,10 +38,10 @@ func Test_Version(t *testing.T) { cmd.SetOut(buf) cmd.SetErr(buf) - completCmd := append(tt.Cmd, "--kubeconfig-path") - completCmd = append(completCmd, kubeconfigPath) + tt.Cmd = append(tt.Cmd, "--kubeconfig-path") + tt.Cmd = append(tt.Cmd, kubeconfigPath) - cmd.SetArgs(completCmd) + cmd.SetArgs(tt.Cmd) err = cmd.Execute() if err != nil { t.Error(err.Error()) diff --git a/internal/aws/eks.go b/internal/aws/eks.go index efd8928..80c4214 100644 --- a/internal/aws/eks.go +++ b/internal/aws/eks.go @@ -15,7 +15,6 @@ import ( log "github.com/sirupsen/logrus" ) -//type ClusterListDescriber struct { type EKSClient struct { EKS eksiface.EKSAPI Region string diff --git a/internal/aws/eks_cluster.go b/internal/aws/eks_cluster.go index 1a14d6f..7129e8b 100644 --- a/internal/aws/eks_cluster.go +++ b/internal/aws/eks_cluster.go @@ -14,7 +14,7 @@ const ( ) var ( - options map[AuthType][]string = map[AuthType][]string{ + options = map[AuthType][]string{ useAWSCLI: {"eks", "get-token", "--cluster-name"}, useIAMAuthenticator: {"token", "-i"}, } diff --git a/internal/aws/eks_kubeconfig.go b/internal/aws/eks_kubeconfig.go index a4429bb..9f01059 100644 --- a/internal/aws/eks_kubeconfig.go +++ b/internal/aws/eks_kubeconfig.go @@ -22,12 +22,12 @@ const ( ) var ( - commands map[AuthType]string = map[AuthType]string{ + commands = map[AuthType]string{ useAWSCLI: commandAWScli, useIAMAuthenticator: commandIAMAuthenticator, } - awsCLIVersionCommand []string = []string{"aws", "--version"} + awsCLIVersionCommand = []string{"aws", "--version"} ) func getAuthType() AuthType { diff --git a/internal/aws/eks_kubeconfig_test.go b/internal/aws/eks_kubeconfig_test.go index 2cc1690..60d6d94 100644 --- a/internal/aws/eks_kubeconfig_test.go +++ b/internal/aws/eks_kubeconfig_test.go @@ -24,7 +24,7 @@ func TestAWSCLIVersion(t *testing.T) { for _, tt := range tts { testname := fmt.Sprintf("%v -> %v (cmd %v)", tt.Output, tt.Expected, tt.SupportCommand) t.Run(testname, func(t *testing.T) { - //overwrite command + // overwrite command out := getAWSCLIversion([]string{tt.SupportCommand, fmt.Sprintf("'%v'", tt.Output)}) expVer, err := semver.NewVersion(tt.Expected) diff --git a/internal/aws/eks_test.go b/internal/aws/eks_test.go index cb5287d..0973945 100644 --- a/internal/aws/eks_test.go +++ b/internal/aws/eks_test.go @@ -15,6 +15,7 @@ import ( "github.com/stretchr/testify/assert" ) +//nolint:unused, varcheck, deadcode var update = flag.Bool("update", false, "update .golden files") type mockEKSClient struct { @@ -50,7 +51,7 @@ func (c *mockEKSClient) ListClustersPages(_ *eks.ListClustersInput, fn func(*eks o := eks.ListClustersOutput{} clusters := []*string{} - //prepare clusters + // prepare clusters for _, cls := range c.Clusters[start:end] { clusters = append(clusters, &cls.Name) } @@ -105,7 +106,7 @@ type testCase struct { Region string } -var cases []testCase = []testCase{ +var cases = []testCase{ // Happy flows { Client: mockEKSClient{ diff --git a/internal/cluster/testing.go b/internal/cluster/testing.go index f479f06..65010f8 100644 --- a/internal/cluster/testing.go +++ b/internal/cluster/testing.go @@ -16,6 +16,8 @@ const ( func stringWithCharset(length int, charset string) string { b := make([]byte, length) for i := range b { + // NOTE(mmicu): this is for testing only + // #nosec b[i] = charset[rand.Intn(len(charset))] } return string(b) @@ -27,7 +29,6 @@ func randomString(length int) string { func dummyGenerateAuthInfo(cls *Cluster) *clientcmdapi.AuthInfo { return clientcmdapi.NewAuthInfo() - } func getMockClusters(i int, r string) *Cluster { diff --git a/internal/kubeconfig/kubeconfig_test.go b/internal/kubeconfig/kubeconfig_test.go index 3a722e9..5bb6431 100644 --- a/internal/kubeconfig/kubeconfig_test.go +++ b/internal/kubeconfig/kubeconfig_test.go @@ -53,7 +53,7 @@ func TestLoadKubeconfig(t *testing.T) { t.Errorf("Failed to load kubeconfig %v", err.Error()) } - out := make([]simpleCluster, 0, 0) + out := make([]simpleCluster, 0) for _, c := range clusters { out = append(out, simpleCluster{ Endpoint: c.Endpoint, @@ -61,7 +61,7 @@ func TestLoadKubeconfig(t *testing.T) { }) } - expected := make([]simpleCluster, 0, 0) + expected := make([]simpleCluster, 0) for _, c := range tc.Clusters { expected = append(expected, simpleCluster{ Endpoint: c.Endpoint, diff --git a/main.go b/main.go index 3887d66..1c3c1cc 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "github.com/mateimicu/kdiscover/cmd" ) +//nolint:unused, varcheck, deadcode var update = flag.Bool("update", false, "update .golden files") var (