Skip to content
This repository has been archived by the owner on Oct 9, 2024. It is now read-only.

Commit

Permalink
(maint) add golangci-lint to ci
Browse files Browse the repository at this point in the history
  • Loading branch information
tphoney committed Aug 7, 2024
1 parent 4cf32eb commit 86b70f1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/test-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ./...

Expand Down
22 changes: 17 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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) {
Expand All @@ -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")
}
}
})
}
Expand Down Expand Up @@ -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
Expand Down
22 changes: 11 additions & 11 deletions sources/shared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sources

import (
"context"
"errors"
"regexp"
"testing"

Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit 86b70f1

Please sign in to comment.