From 86b70f1893f8843e9adf6d234f1b70af27ddf362 Mon Sep 17 00:00:00 2001 From: TP Honey Date: Wed, 7 Aug 2024 15:29:02 +0100 Subject: [PATCH] (maint) add golangci-lint to ci --- .github/workflows/test-build.yml | 12 ++++++++++++ cmd/root.go | 22 +++++++++++++++++----- sources/shared_test.go | 22 +++++++++++----------- 3 files changed, 40 insertions(+), 16 deletions(-) diff --git a/.github/workflows/test-build.yml b/.github/workflows/test-build.yml index 9702a3e..882c3ac 100644 --- a/.github/workflows/test-build.yml +++ b/.github/workflows/test-build.yml @@ -19,6 +19,18 @@ jobs: - name: Vet run: go vet ./... + # get .golangci.yml from github.com/overmindtech/golangci-lint_config + - name: Get .golangci.yml from github.com/overmindtech/golangci-lint_configs + run: | + curl -sfL https://raw.githubusercontent.com/overmindtech/golangci-lint_config/main/.golangci.yml -o .golangci.yml + + - name: golangci-lint + uses: golangci/golangci-lint-action@v6 + with: + version: v1.56.1 + args: --timeout 3m + skip-cache: true # the linters require all code generation and dependecies to be present, but the cache implementation completely falls over when there is already existing content. See https://github.com/golangci/golangci-lint-action/issues/23, https://github.com/golangci/golangci-lint-action/issues/863, https://github.com/golangci/golangci-lint-action/issues/984 + - name: Test run: go test ./... diff --git a/cmd/root.go b/cmd/root.go index 9638ea4..19d6458 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -124,7 +124,13 @@ Edit this once you have created your source }).Debug("Starting healthcheck server") go func() { - err := http.ListenAndServe(":8080", nil) + server := &http.Server{ //G114: Use of net/http serve function that has no support for setting timeouts + Addr: ":8080", + Handler: nil, + ReadTimeout: 5 * time.Second, + WriteTimeout: 10 * time.Second, + } + err := server.ListenAndServe() if err != nil { log.WithError(err).Fatal("Could not start HTTP server for /healthz health checks") @@ -195,7 +201,7 @@ func init() { rootCmd.PersistentFlags().String("your-custom-flag", "someDefaultValue.conf", "Description of what your option is meant to do") // Bind these to viper - viper.BindPFlags(rootCmd.PersistentFlags()) + cobra.CheckErr(viper.BindPFlags(rootCmd.PersistentFlags())) // Run this before we do anything to set up the loglevel rootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) { @@ -211,7 +217,13 @@ func init() { cmd.PersistentFlags().VisitAll(func(f *pflag.Flag) { // Bind the flag to viper only if it has a non-empty default if f.DefValue != "" || f.Changed { - viper.BindPFlag(f.Name, f) + err := viper.BindPFlag(f.Name, f) + if err != nil { + log.WithFields(log.Fields{ + "error": err, + "flag": f.Name, + }).Fatal("Could not bind flag to viper") + } } }) } @@ -247,11 +259,11 @@ func createTokenClient(natsJWT string, natsNKeySeed string) (auth.TokenClient, e } if _, err = jwt.DecodeUserClaims(natsJWT); err != nil { - return nil, fmt.Errorf("could not parse nats-jwt: %v", err) + return nil, fmt.Errorf("could not parse nats-jwt: %w", err) } if kp, err = nkeys.FromSeed([]byte(natsNKeySeed)); err != nil { - return nil, fmt.Errorf("could not parse nats-nkey-seed: %v", err) + return nil, fmt.Errorf("could not parse nats-nkey-seed: %w", err) } return auth.NewBasicTokenClient(natsJWT, kp), nil diff --git a/sources/shared_test.go b/sources/shared_test.go index 21a7ec1..7d11219 100644 --- a/sources/shared_test.go +++ b/sources/shared_test.go @@ -2,6 +2,7 @@ package sources import ( "context" + "errors" "regexp" "testing" @@ -29,7 +30,7 @@ type ExpectedItems struct { NumItems int // A list of expected attributes for the items, will be checked in order - // with the first set of attributes neeing to match those of the first item + // with the first set of attributes needing to match those of the first item // etc. Note that this doesn't need to have the same number of entries as // there are items ExpectedAttributes []map[string]interface{} @@ -81,25 +82,24 @@ func RunSourceTests(t *testing.T, tests []SourceTest, source discovery.Source) { t.Error("expected error but got nil") } - ire, ok := err.(*sdp.QueryError) - - if !ok { + var ire *sdp.QueryError + if !errors.As(err, &ire) { t.Fatalf("error returned was type %T, expected *sdp.QueryError", err) } - if ee.Type != ire.ErrorType { - t.Fatalf("error type was %v, expected %v", ire.ErrorType, ee.Type) + if ee.Type != ire.GetErrorType() { + t.Fatalf("error type was %v, expected %v", ire.GetErrorType(), ee.Type) } if ee.Scope != "" { - if ee.Scope != ire.Scope { - t.Fatalf("error scope was %v, expected %v", ire.Scope, ee.Scope) + if ee.Scope != ire.GetScope() { + t.Fatalf("error scope was %v, expected %v", ire.GetScope(), ee.Scope) } } if ee.ErrorStringRegex != nil { - if !ee.ErrorStringRegex.MatchString(ire.ErrorString) { - t.Fatalf("error string did not match regex %v, raw value: %v", ee.ErrorStringRegex, ire.ErrorString) + if !ee.ErrorStringRegex.MatchString(ire.GetErrorString()) { + t.Fatalf("error string did not match regex %v, raw value: %v", ee.ErrorStringRegex, ire.GetErrorString()) } } } else { @@ -120,7 +120,7 @@ func RunSourceTests(t *testing.T, tests []SourceTest, source discovery.Source) { relevantItem := items[i] for key, expectedValue := range expectedAttributes { - value, err := relevantItem.Attributes.Get(key) + value, err := relevantItem.GetAttributes().Get(key) if err != nil { t.Error(err)