Skip to content

Commit

Permalink
chore: bump golangci-lint and fix linting issues
Browse files Browse the repository at this point in the history
Fix errors reported by new version of golangci-lint.

Signed-off-by: Alex Szakaly <[email protected]>
  • Loading branch information
alex1989hu authored and talos-bot committed Dec 3, 2020
1 parent c5dc2e6 commit 5b58100
Show file tree
Hide file tree
Showing 24 changed files with 75 additions and 22 deletions.
3 changes: 2 additions & 1 deletion cmd/enforce.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import (
"fmt"

"github.com/spf13/cobra"

"github.com/talos-systems/conform/internal/enforcer"
"github.com/talos-systems/conform/internal/policy"
)

// enforceCmd represents the enforce command
// enforceCmd represents the enforce command.
var enforceCmd = &cobra.Command{
Use: "enforce",
Short: "",
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/spf13/cobra"
)

// rootCmd represents the base command when called without any subcommands
// rootCmd represents the base command when called without any subcommands.
var rootCmd = &cobra.Command{
Use: "conform",
Short: "Policy enforcement for your pipelines.",
Expand Down
27 changes: 19 additions & 8 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,57 +14,61 @@ import (
"os/exec"
"path/filepath"

"github.com/google/go-github/github"
"github.com/spf13/cobra"

git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/google/go-github/github"
"github.com/spf13/cobra"
)

const (
path = "/github"
)

// serveCmd represents the serve command
// serveCmd represents the serve command.
var serveCmd = &cobra.Command{
Use: "serve",
Short: "",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
if err := os.MkdirAll("/tmp", 0700); err != nil {
if err := os.MkdirAll("/tmp", 0o700); err != nil {
log.Fatal(err)
}

http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
payload, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("failed to read payload: %+v\n", err)

return
}

go func() {
dir, err := ioutil.TempDir("/tmp", "conform")
if err != nil {
log.Printf("failed to create temporary directory: %+v\n", err)

return
}
// nolint: errcheck
defer os.RemoveAll(dir)

if err = os.MkdirAll(filepath.Join(dir, "github"), 0700); err != nil {
if err = os.MkdirAll(filepath.Join(dir, "github"), 0o700); err != nil {
log.Printf("failed to create github directory: %+v\n", err)

return
}
if err = os.MkdirAll(filepath.Join(dir, "repo"), 0700); err != nil {
if err = os.MkdirAll(filepath.Join(dir, "repo"), 0o700); err != nil {
log.Printf("failed to create repo directory: %+v\n", err)

return
}

event := filepath.Join(dir, "github", "event.json")
pullRequestEvent := &github.PullRequestEvent{}
if err = json.Unmarshal(payload, pullRequestEvent); err != nil {
log.Printf("failed to parse pull_request event: %+v\n", err)

return
}

Expand All @@ -80,6 +84,7 @@ var serveCmd = &cobra.Command{
})
if err != nil {
log.Printf("failed to clone repo: %+v\n", err)

return
}

Expand All @@ -98,12 +103,14 @@ var serveCmd = &cobra.Command{
})
if err != nil {
log.Printf("failed to fetch %q: %v", refSpec, err)

return
}

worktree, err := repo.Worktree()
if err != nil {
log.Printf("failed to get working tree: %v", err)

return
}

Expand All @@ -113,13 +120,15 @@ var serveCmd = &cobra.Command{

if err != nil {
log.Printf("failed to checkout %q: %v", ref, err)

return
}

log.Printf("writing %s to disk", event)

if err = ioutil.WriteFile(event, payload, 0600); err != nil {
if err = ioutil.WriteFile(event, payload, 0o600); err != nil {
log.Printf("failed to write event to disk: %+v\n", err)

return
}
cmd := exec.Command("/proc/self/exe", "enforce", "--reporter=github", "--commit-ref=refs/heads/"+pullRequestEvent.GetPullRequest().GetBase().GetRef())
Expand All @@ -130,11 +139,13 @@ var serveCmd = &cobra.Command{
err = cmd.Start()
if err != nil {
log.Printf("failed to start command: %+v\n", err)

return
}
err = cmd.Wait()
if err != nil {
log.Printf("command failed: %+v\n", err)

return
}
}()
Expand Down
3 changes: 2 additions & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"text/template"

"github.com/spf13/cobra"

"github.com/talos-systems/conform/internal/constants"
)

Expand Down Expand Up @@ -42,7 +43,7 @@ type Version struct {
Arch string
}

// versionCmd represents the version command
// versionCmd represents the version command.
var versionCmd = &cobra.Command{
Use: "version",
Short: "Prints the version",
Expand Down
10 changes: 9 additions & 1 deletion hack/golangci-lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ linters-settings:
simple: true
range-loops: true # Report preallocation suggestions on range loops, true by default
for-loops: false # Report preallocation suggestions on for loops, false by default
gci:
local-prefixes: github.com/talos-systems/conform

linters:
enable-all: true
Expand All @@ -102,8 +104,14 @@ linters:
- gochecknoglobals
- gochecknoinits
- funlen
- gomnd
- godox
- gocognit
- gomnd
- goerr113
- nestif
- exhaustivestruct
- errorlint
- wrapcheck
disable-all: false
fast: false

Expand Down
2 changes: 1 addition & 1 deletion hack/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ CGO_ENABLED=1
lint_packages() {
if [ "${lint}" = true ]; then
echo "linting packages"
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $GOPATH/bin v1.24.0
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $GOPATH/bin v1.32.2
golangci-lint run --config "${BASH_SOURCE%/*}/golangci-lint.yaml"
fi
}
Expand Down
4 changes: 2 additions & 2 deletions internal/enforcer/enforcer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import (

"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
yaml "gopkg.in/yaml.v2"

"github.com/talos-systems/conform/internal/policy"
"github.com/talos-systems/conform/internal/policy/commit"
"github.com/talos-systems/conform/internal/policy/license"
"github.com/talos-systems/conform/internal/reporter"

yaml "gopkg.in/yaml.v2"
)

// Conform is a struct that conform.yaml gets decoded into.
Expand Down
1 change: 1 addition & 0 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ func (g *Git) AheadBehind(ref string) (ahead int, behind int, err error) {
err = iter.ForEach(func(comm *object.Commit) error {
if comm.Hash != ref1.Hash() {
count++

return nil
}

Expand Down
2 changes: 2 additions & 0 deletions internal/policy/commit/check_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/pkg/errors"

"github.com/talos-systems/conform/internal/policy"
)

Expand Down Expand Up @@ -54,6 +55,7 @@ func (c Commit) ValidateBody() policy.Check {

if line != "" {
valid = true

break
}
}
Expand Down
5 changes: 5 additions & 0 deletions internal/policy/commit/check_conventional_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

"github.com/pkg/errors"

"github.com/talos-systems/conform/internal/policy"
)

Expand Down Expand Up @@ -67,6 +68,7 @@ func (c Commit) ValidateConventionalCommit() policy.Check {

if len(groups) != 6 {
check.errors = append(check.errors, errors.Errorf("Invalid conventional commits format: %q", c.msg))

return check
}

Expand All @@ -81,6 +83,7 @@ func (c Commit) ValidateConventionalCommit() policy.Check {

if !typeIsValid {
check.errors = append(check.errors, errors.Errorf("Invalid type %q: allowed types are %v", groups[1], c.Conventional.Types))

return check
}

Expand All @@ -92,12 +95,14 @@ func (c Commit) ValidateConventionalCommit() policy.Check {
re := regexp.MustCompile(scope)
if re.Match([]byte(groups[3])) {
scopeIsValid = true

break
}
}

if !scopeIsValid {
check.errors = append(check.errors, errors.Errorf("Invalid scope %q: allowed scopes are %v", groups[3], c.Conventional.Scopes))

return check
}
}
Expand Down
1 change: 1 addition & 0 deletions internal/policy/commit/check_dco.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

"github.com/pkg/errors"

"github.com/talos-systems/conform/internal/policy"
)

Expand Down
2 changes: 2 additions & 0 deletions internal/policy/commit/check_gpg_signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package commit

import (
"github.com/pkg/errors"

"github.com/talos-systems/conform/internal/git"
"github.com/talos-systems/conform/internal/policy"
)
Expand Down Expand Up @@ -41,6 +42,7 @@ func (c Commit) ValidateGPGSign(g *git.Git) policy.Check {
ok, err := g.HasGPGSignature()
if err != nil {
check.errors = append(check.errors, err)

return check
}

Expand Down
4 changes: 4 additions & 0 deletions internal/policy/commit/check_header_case.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"unicode/utf8"

"github.com/pkg/errors"

"github.com/talos-systems/conform/internal/policy"
)

Expand Down Expand Up @@ -44,12 +45,14 @@ func (c Commit) ValidateHeaderCase() policy.Check {
firstWord, err := c.firstWord()
if err != nil {
check.errors = append(check.errors, err)

return check
}

first, _ := utf8.DecodeRuneInString(firstWord)
if first == utf8.RuneError {
check.errors = append(check.errors, errors.New("Header does not start with valid UTF-8 text"))

return check
}

Expand All @@ -62,6 +65,7 @@ func (c Commit) ValidateHeaderCase() policy.Check {
valid = unicode.IsLower(first)
default:
check.errors = append(check.errors, errors.Errorf("Invalid configured case %s", c.Header.Case))

return check
}

Expand Down
1 change: 1 addition & 0 deletions internal/policy/commit/check_header_last_character.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"unicode/utf8"

"github.com/pkg/errors"

"github.com/talos-systems/conform/internal/policy"
)

Expand Down
1 change: 1 addition & 0 deletions internal/policy/commit/check_header_length.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"

"github.com/pkg/errors"

"github.com/talos-systems/conform/internal/policy"
)

Expand Down
6 changes: 5 additions & 1 deletion internal/policy/commit/check_imperative_verb.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
"strings"

"github.com/pkg/errors"
"github.com/talos-systems/conform/internal/policy"
"gopkg.in/jdkato/prose.v2"

"github.com/talos-systems/conform/internal/policy"
)

// ImperativeCheck enforces that the first word of a commit message header is
Expand Down Expand Up @@ -48,17 +49,20 @@ func (c Commit) ValidateImperative() policy.Check {

if word, err = c.firstWord(); err != nil {
check.errors = append(check.errors, err)

return check
}

doc, err := prose.NewDocument("I " + strings.ToLower(word))
if err != nil {
check.errors = append(check.errors, errors.Errorf("Failed to create document: %v", err))

return check
}

if len(doc.Tokens()) != 2 {
check.errors = append(check.errors, errors.Errorf("Expected 2 tokens, got %d", len(doc.Tokens())))

return check
}

Expand Down
5 changes: 3 additions & 2 deletions internal/policy/commit/check_jira.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
"regexp"

"github.com/pkg/errors"

"github.com/talos-systems/conform/internal/policy"
)

// JiraCheck enforces that a Jira issue is mentioned in the header
// JiraCheck enforces that a Jira issue is mentioned in the header.
type JiraCheck struct {
errors []error
}
Expand All @@ -35,7 +36,7 @@ func (j *JiraCheck) Errors() []error {
return j.errors
}

// ValidateJiraCheck validates if a Jira issue is mentioned in the header
// ValidateJiraCheck validates if a Jira issue is mentioned in the header.
func (c Commit) ValidateJiraCheck() policy.Check {
check := &JiraCheck{}

Expand Down
Loading

0 comments on commit 5b58100

Please sign in to comment.