Skip to content

Commit

Permalink
Fix remaining linting issues
Browse files Browse the repository at this point in the history
Fix the linting issues with last commit, broken out as a separate
commit to track changes.
  • Loading branch information
wrouesnel committed Aug 8, 2022
1 parent aa1c8c0 commit 4eb3f91
Show file tree
Hide file tree
Showing 17 changed files with 260 additions and 94 deletions.
15 changes: 15 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ linters:
- lll
- gofumpt
- gci
- exhaustruct # ultimately too noisy for most coding
# This one is just infuriating, produces a lot of warnings, and doesn't matter.
- godot
# These all stopped working suddenly due to a non-existent compilation error.
# Retry them regularly.
- stylecheck
- exhaustive
- staticcheck
- gosimple
- govet
- unused

issues:
exclude-rules:
Expand All @@ -29,6 +40,9 @@ issues:
- dupl
- funlen
- varnamelen
- path: magefile.go
linters:
- unparam

linters-settings:
varnamelen:
Expand Down Expand Up @@ -66,6 +80,7 @@ linters-settings:
ignore-decls:
- s string
- w http.ResponseWriter
- r *http.Request

tagliatelle:
# Check the struck tag name case.
Expand Down
2 changes: 1 addition & 1 deletion cmd/poller_exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (
"go.uber.org/zap"
)

var Version = "0.0.0"
var Version = "0.0.0" // nolint: gochecknoglobals

//nolint:gochecknoglobals
var CLI struct {
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/integralist/go-findroot v0.0.0-20160518114804-ac90681525dc
github.com/julienschmidt/httprouter v1.3.0
github.com/magefile/mage v1.13.0
github.com/magisterquis/connectproxy v0.0.0-20200725203833-3582e84f0c9b
github.com/mholt/archiver v3.1.1+incompatible
github.com/mitchellh/mapstructure v1.5.0
github.com/pkg/errors v0.9.1
Expand All @@ -17,10 +18,12 @@ require (
github.com/shaj13/go-guardian/v2 v2.11.5
github.com/shaj13/libcache v1.0.5
github.com/wrouesnel/multihttp v1.0.0
go.uber.org/atomic v1.7.0
go.uber.org/zap v1.21.0
golang.org/x/net v0.0.0-20220805013720-a33c5aa5df48
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c
gopkg.in/yaml.v3 v3.0.1
shanhu.io/virgo v0.0.0-20220803070549-c34ec4ce58b7
)

require (
Expand All @@ -33,7 +36,6 @@ require (
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/magisterquis/connectproxy v0.0.0-20200725203833-3582e84f0c9b // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/nwaples/rardecode v1.1.3 // indirect
github.com/pierrec/lz4 v2.6.1+incompatible // indirect
Expand All @@ -43,10 +45,8 @@ require (
github.com/stretchr/testify v1.8.0 // indirect
github.com/ulikunitz/xz v0.5.10 // indirect
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect
golang.org/x/sys v0.0.0-20220731174439-a90be440212d // indirect
google.golang.org/protobuf v1.26.0 // indirect
shanhu.io/virgo v0.0.0-20220803070549-c34ec4ce58b7 // indirect
)
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,6 @@ golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81R
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220728211354-c7608f3a8462 h1:UreQrH7DbFXSi9ZFox6FNT3WBooWmdANpU+IfkT1T4I=
golang.org/x/net v0.0.0-20220728211354-c7608f3a8462/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/net v0.0.0-20220805013720-a33c5aa5df48 h1:N9Vc/rorQUDes6B9CNdIxAn5jODGj2wzfrei2x4wNj4=
golang.org/x/net v0.0.0-20220805013720-a33c5aa5df48/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/oauth2 v0.0.0-20170912212905-13449ad91cb2/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand Down Expand Up @@ -466,8 +464,6 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 h1:WIoqL4EROvwiPdUtaip4VcDdpZ4kha7wBWZrbVKCIZg=
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220731174439-a90be440212d h1:Sv5ogFZatcgIMMtBSTTAgMYsicp25MXBubjXNDKwm80=
golang.org/x/sys v0.0.0-20220731174439-a90be440212d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down
25 changes: 25 additions & 0 deletions mage
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# See: https://stackoverflow.com/questions/59895/how-to-get-the-source-directory-of-a-bash-script-from-within-the-script-itself
# Note: you can't refactor this out: its at the top of every script so the scripts can find their includes.
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
SCRIPT_DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"

function log() {
echo "$*" 1>&2
}

function fatal() {
echo "$*" 1>&2
exit 1
}

pushd "$SCRIPT_DIR" 1>/dev/null 2>&1 || fatal "pushd failed: ${SCRIPT_DIR}"

go run mage.go "$@"
exit $?

popd 1>/dev/null 2>&1 || fatal "popd failed: ${SCRIPT_DIR}"
132 changes: 125 additions & 7 deletions magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,29 @@

// Self-contained go-project magefile.

// nolint: deadcode,gochecknoglobals,gochecknoinits,wrapcheck,varnamelen,gomnd,forcetypeassert,forbidigo,funlen,gocognit,cyclop
// nolint: deadcode,gochecknoglobals,gochecknoinits,wrapcheck,varnamelen,gomnd,forcetypeassert,forbidigo,funlen,gocognit,cyclop,nolintlint
package main

import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"math/bits"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"sort"
"strconv"
"strings"
"sync"
"time"

"gopkg.in/yaml.v3"

archiver "github.com/mholt/archiver"

"github.com/magefile/mage/mg"
Expand All @@ -34,6 +40,7 @@ var (
errAutogenUnknownPreCommitScriptFormat = errors.New("unknown pre-commit script format")
errPlatformNotSupported = errors.New("current platform is not supported")
errParallelBuildFailed = errors.New("parallel build failed")
errLintBisect = errors.New("error during lint bisect")
)

var curDir = func() string {
Expand Down Expand Up @@ -449,14 +456,111 @@ func Tools() (err error) {
return nil
}

func lintArgs(args ...string) []string {
returnedArgs := []string{"-j", fmt.Sprintf("%v", concurrency), fmt.Sprintf(
"--deadline=%s", linterDeadline.String())}
returnedArgs = append(returnedArgs, args...)
return returnedArgs
}

// Lint runs gometalinter for code quality. CI will run this before accepting PRs.
func Lint() error {
mg.Deps(Tools)
args := []string{"-j", fmt.Sprintf("%v", concurrency), fmt.Sprintf(
"--deadline=%s", linterDeadline.String()),
"run",
extraArgs := lintArgs("run")
extraArgs = append(extraArgs, goDirs...)
return sh.RunV("golangci-lint", extraArgs...)
}

// LintBisect runs the linters one directory at a time.
// It is useful for finding problems where golangci-lint won't compile and
// doesn't emit an error message.
func LintBisect() error {
errs := []error{}
for _, goDir := range goDirs {
fmt.Println("Linting:", goDir)
err := sh.RunV("golangci-lint", lintArgs("run", goDir)...)
if err != nil {
fmt.Println("LINT ERROR IN:", goDir)
errs = append(errs, err)
}
}
if len(errs) > 0 {
return errLintBisect
}
return nil
}

// listLinters gets the golangci-lint config
func listLinters() ([]string, error) {
cmd := exec.Command("golangci-lint", "linters")
output, err := cmd.Output()
if err != nil {
return []string{}, errors.Wrap(err, "golangci-lint linters failed to run")
}
bio := bufio.NewReader(bytes.NewBuffer(output))
linters := []string{}
for {
line, _ := bio.ReadString('\n')
line = strings.Trim(line, " \n\t")
if line == "" {
continue
}
if line == "Enabled by your configuration linters:" {
continue
}
if line == "Disabled by your configuration linters:" {
// Done
break
}
linter := strings.Split(line, ":")[0]
linter = strings.Split(linter, "(")[0]
linter = strings.Trim(linter, " \n\t")
linters = append(linters, linter)
}
return linters, nil
}

// LintersBisect runs all linters in golangci-lint one at a time.
// It is useful find broken linters.
func LintersBisect() error {
linters, err := listLinters()
if err != nil {
return errors.Wrap(err, "LintersBisect: listLinters failed")
}
errs := map[string]error{}

// Annoyingly, we have to override .golangci.yml to allow us to pick linters
// one by one.
golangCi := make(map[string]interface{})
_ = yaml.Unmarshal(must(ioutil.ReadFile(".golangci.yml")), golangCi)
delete(golangCi, "linters")
tempConfig, err := ioutil.TempFile("", ".golangci.*.yml")
if err != nil {
return errors.Wrap(err, "LintersBisect: TempFile failed")
}
_ = must(tempConfig.Write(must(yaml.Marshal(golangCi))))
defer os.Remove(tempConfig.Name())

for _, linter := range linters {
extraArgs := lintArgs("run",
fmt.Sprintf("--config=%s", tempConfig.Name()),
"--disable-all",
fmt.Sprintf("--enable=%s", linter))
extraArgs = append(extraArgs, goDirs...)
err := sh.RunV("golangci-lint", extraArgs...)
if err != nil {
errs[linter] = err
}
}

if len(errs) > 0 {
for linter := range errs {
fmt.Println("FAILED LINTER:", linter)
}

return errLintBisect
}
return sh.RunV("golangci-lint", append(args, goDirs...)...)
return nil
}

// fmt runs golangci-lint with the formatter options.
Expand Down Expand Up @@ -647,7 +751,7 @@ func doReleaseBin(OSArch string) func() error {
}

// ReleaseBin builds cross-platform release binaries under the bin/ directory.
// nolint:gocritic
//nolint:gocritic
func ReleaseBin(OSArch string) error {
return doReleaseBin(OSArch)()
}
Expand All @@ -663,7 +767,7 @@ func ReleaseBinAll() error {
}

// Release builds release archives under the release/ directory.
// nolint:gocritic
//nolint:gocritic
func doRelease(OSArch string) func() error {
platform, ok := platformsLookup[OSArch]
if !ok {
Expand Down Expand Up @@ -704,6 +808,20 @@ func doRelease(OSArch string) func() error {
}
}

// PlatformTargets prints the list of target platforms
func PlatformTargets() error {
platforms := make([]string, 0, len(platformsLookup))
for platform := range platformsLookup {
platforms = append(platforms, platform)
}
sort.Strings(platforms)
for _, platform := range platforms {
fmt.Println(platform)
}
return nil
}

// Release a binary archive for a specific platform
// nolint:gocritic
func Release(OSArch string) error {
return doRelease(OSArch)()
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ var defaultConfig []byte
// DefaultConfigFile returns the default embedded YAML config which sets the
// poller_defaults.
func DefaultConfigFile() []byte {
return defaultConfig[:]
return append(defaultConfig[:0:0], defaultConfig...)
}
12 changes: 6 additions & 6 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (s *TLSCACertsSuite) loadCertsFile(c *C, filename string) *x509.CertPool {
c.Check(yaml.Unmarshal(data, &intfMap), IsNil, Commentf("YAML decode failed"))

pool := new(config.TLSCertificatePool)
decoder, err := config.ConfigDecoder(pool)
decoder, err := config.Decoder(pool, false)
c.Assert(err, IsNil, Commentf("Config decoder initialization failed"))

err = decoder.Decode(intfMap)
Expand Down Expand Up @@ -254,10 +254,10 @@ func (ce *ConfigExpected) TestCompleteConfig(c *C) {
c.Check(crChecks.TLSEnable, Equals, false)
c.Check(len(GetPoolCertificates(crChecks.TLSCACerts.CertPool)), Equals, 2, Commentf("challenge_response has more certs"))

c.Check(crChecks.ChallengeString, Equals, "MY_UNIQUE_HEADER")
c.Check(*crChecks.ChallengeString, Equals, "MY_UNIQUE_HEADER")
c.Check([]byte(crChecks.ChallengeBinary), DeepEquals, []byte{114, 149, 9, 49, 56, 189, 30, 220, 186, 59, 139, 28, 127, 66, 178, 97})
c.Check(crChecks.ResponseRegex.String(), Equals, regexp.MustCompile("RESPONSE_HEADER").String())
c.Check(crChecks.ResponseLiteral, Equals, "literal-value")
c.Check(*crChecks.ResponseLiteral, Equals, "literal-value")
c.Check([]byte(crChecks.ResponseBinary), DeepEquals, []byte{114, 149, 9, 49, 56, 189, 30, 220, 186, 59, 139, 28, 127, 66, 178, 97})
c.Check(crChecks.MaxBytes, Equals, uint64(65535))

Expand All @@ -271,14 +271,14 @@ func (ce *ConfigExpected) TestCompleteConfig(c *C) {
httpServiceCerts := GetPoolCertificates(httpChecks.TLSCACerts.CertPool)
c.Check(len(httpServiceCerts), Equals, 1)

c.Check(httpChecks.ChallengeString, Equals, "some-data")
c.Check(*httpChecks.ChallengeString, Equals, "some-data")
c.Check([]byte(httpChecks.ChallengeBinary), DeepEquals, []byte{114, 149, 9, 49, 56, 189, 30, 220, 186, 59, 139, 28, 127, 66, 178, 97})
c.Check(httpChecks.ResponseRegex.String(), Equals, regexp.MustCompile("^<field-tag>").String())
c.Check(httpChecks.ResponseLiteral, Equals, "<html>")
c.Check(*httpChecks.ResponseLiteral, Equals, "<html>")
c.Check([]byte(httpChecks.ResponseBinary), DeepEquals, []byte{114, 149, 9, 49, 56, 189, 30, 220, 186, 59, 139, 28, 127, 66, 178, 97})
c.Check(httpChecks.MaxBytes, Equals, uint64(131072))

c.Check(httpChecks.Verb, Equals, "GET")
c.Check(httpChecks.Verb, Equals, config.HTTPVerb("GET"))
c.Check(httpChecks.URL.String(), Equals, "http://vhost/query-path?with_paramters=1")
testRange := config.HTTPStatusRange{}
c.Check(testRange.UnmarshalText([]byte("200 201 300-399")), IsNil)
Expand Down
Loading

0 comments on commit 4eb3f91

Please sign in to comment.