Skip to content

Commit

Permalink
update golangci-lint rules and code.
Browse files Browse the repository at this point in the history
  • Loading branch information
goreleaserbot authored and guumaster committed Jul 13, 2021
1 parent 9477b29 commit 06a9253
Show file tree
Hide file tree
Showing 61 changed files with 266 additions and 189 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
uses: actions/checkout@v2

- name: Install golangci-lint
run: curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.24.0
run: curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.33.0

- name: Install golint
run: go get -u golang.org/x/lint/golint
Expand Down
119 changes: 112 additions & 7 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,115 @@
linters:
enable-all: true

# all available settings of specific linters
linters-settings:
unparam:
check-exported: true
dupl:
threshold: 100
funlen:
lines: 120
statements: 80
statements: 60
gci:
local-prefixes: github.com/golangci/golangci-lint
goconst:
min-len: 2
min-occurrences: 2
gocritic:
enabled-tags:
- diagnostic
- experimental
- opinionated
- performance
- style
disabled-checks:
- dupImport # https://github.com/go-critic/go-critic/issues/845
- ifElseChain
- octalLiteral
- whyNoLint
- wrapperFunc
gocyclo:
min-complexity: 15
goimports:
local-prefixes: github.com/golangci/golangci-lint
golint:
min-confidence: 0
gomnd:
settings:
mnd:
# don't include the "operation" and "assign"
checks: argument,case,condition,return
lll:
line-length: 140
maligned:
suggest-new: true
misspell:
locale: US
ignore-words:
- konstellation
nolintlint:
allow-leading-space: true # don't require machine-readable nolint directives (i.e. with no leading space)
allow-unused: false # report any unused nolint directives
require-explanation: false # don't require an explanation for nolint directives
require-specific: false # don't require nolint directives to be specific about which linter is being skipped
unparam:
check-exported: true
linters:
# please, do not use `enable-all`: it's deprecated and will be removed soon.
# inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint
disable-all: true
enable:
- bodyclose
- deadcode
- depguard
- dogsled
- dupl
- errcheck
- exhaustive
- funlen
- gochecknoinits
- goconst
# - gocritic
- gocyclo
- gofmt
- goimports
- golint
- gomnd
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- interfacer
- lll
- misspell
- nakedret
- noctx
- nolintlint
- rowserrcheck
- scopelint
- staticcheck
- structcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
- varcheck
- whitespace
- asciicheck
- gochecknoglobals
- gocognit
- godot
- godox
- maligned
- nestif
- prealloc
- wsl
- goerr113
issues:
# Excluding configuration per-path, per-linter, per-text and per-source
exclude-rules:
- path: _test\.go
linters:
- gomnd
- dupl
- goconst
# https://github.com/go-critic/go-critic/issues/926
- linters:
- gocritic
text: "unnecessaryDefer:"
4 changes: 1 addition & 3 deletions cmd/hostctl/actions/add_domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package actions
import (
"strings"

"github.com/spf13/cobra"

"github.com/guumaster/cligger"

"github.com/guumaster/hostctl/pkg/file"
"github.com/guumaster/hostctl/pkg/types"
"github.com/spf13/cobra"
)

func newAddRemoveDomainsCmd() (*cobra.Command, *cobra.Command) {
Expand Down
7 changes: 4 additions & 3 deletions cmd/hostctl/actions/add_replace.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package actions

import (
"context"
"io"
"os"

"github.com/spf13/cobra"

"github.com/guumaster/hostctl/pkg/file"
"github.com/guumaster/hostctl/pkg/parser"
"github.com/guumaster/hostctl/pkg/types"
"github.com/spf13/cobra"
)

type addRemoveFn func(h *file.File, p *types.Profile) error
Expand Down Expand Up @@ -93,7 +93,8 @@ func getProfileFromInput(in io.Reader, from string) (*types.Profile, error) {
r = in

case isValidURL(from):
r, err = readerFromURL(from)
ctx := context.Background()
r, err = readerFromURL(ctx, from)

default:
r, err = os.Open(from)
Expand Down
4 changes: 1 addition & 3 deletions cmd/hostctl/actions/backup.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package actions

import (
"github.com/spf13/cobra"

"github.com/guumaster/cligger"

"github.com/guumaster/hostctl/pkg/file"
"github.com/spf13/cobra"
)

func newBackupCmd() *cobra.Command {
Expand Down
2 changes: 1 addition & 1 deletion cmd/hostctl/actions/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func newCompletionCmd(rootCmd *cobra.Command) *cobra.Command {
var completionCmd = &cobra.Command{
completionCmd := &cobra.Command{
Use: "completion <bash|zsh>",
Short: "Generate bash or zsh completion script",
Hidden: true,
Expand Down
3 changes: 1 addition & 2 deletions cmd/hostctl/actions/enable_disable.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package actions

import (
"github.com/spf13/cobra"

"github.com/guumaster/hostctl/pkg/file"
"github.com/spf13/cobra"
)

type enableDisableFn func(h *file.File, profiles []string, only, all bool) error
Expand Down
8 changes: 4 additions & 4 deletions cmd/hostctl/actions/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (
)

var (
// ErrIncompatibleAllFlag when you can't use --all flag
// ErrIncompatibleAllFlag when you can't use --all flag.
ErrIncompatibleAllFlag = errors.New("args must be empty with --all flag")

// ErrMultipleProfiles when you can use only a single profile
// ErrMultipleProfiles when you can use only a single profile.
ErrMultipleProfiles = errors.New("specify only one profile")

// ErrEmptyProfiles when trying to update empty profile list
// ErrEmptyProfiles when trying to update empty profile list.
ErrEmptyProfiles = errors.New("there are no profiles")

// ErrReadingFile when a file can't be read
// ErrReadingFile when a file can't be read.
ErrReadingFile = errors.New("error reading data from file")
)
8 changes: 5 additions & 3 deletions cmd/hostctl/actions/gen_md_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/spf13/cobra/doc"
)

var errPathNotDirectory = errors.New("path should be a directory")

func newGenMdDocsCmd(rootCmd *cobra.Command) *cobra.Command {
return &cobra.Command{
Use: "gen-md-docs",
Expand All @@ -31,7 +33,7 @@ Creates full markdown documentation and store it on the given path.
}

if !s.IsDir() {
return errors.New("path should be a directory")
return errPathNotDirectory
}

return nil
Expand All @@ -55,7 +57,7 @@ Creates full markdown documentation and store it on the given path.
fname := path.Base(src)

dst := strings.Replace(src, "hostctl_", "", 1)
if fname == "hostctl.md" {
if fname == "hostctl.md" { // nolint: goconst
dst = path.Join(output, "_index.md")
err = fixContent(rootCmd.Long, src)
} else {
Expand Down Expand Up @@ -92,7 +94,7 @@ func fixContent(desc, dst string) error {
wrapped := fmt.Sprintf("```\n%s\n```\n", desc)
final := strings.Replace(string(data), desc, wrapped, 1)

return ioutil.WriteFile(dst, []byte(final), 0666)
return ioutil.WriteFile(dst, []byte(final), 0666) // nolint: gosec
}

func filePrepender(filename string) string {
Expand Down
19 changes: 13 additions & 6 deletions cmd/hostctl/actions/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ package actions

import (
"bytes"
"context"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"runtime"

"github.com/spf13/cobra"

"github.com/guumaster/hostctl/pkg/render"
"github.com/guumaster/hostctl/pkg/types"
"github.com/spf13/cobra"
)

func commonCheckProfileOnly(_ *cobra.Command, args []string) error {
Expand Down Expand Up @@ -58,7 +58,7 @@ func commonCheckArgs(_ *cobra.Command, args []string) error {
return nil
}

// isPiped detect if there is any input through STDIN
// isPiped detect if there is any input through STDIN.
func isPiped() bool {
info, err := os.Stdin.Stat()
if err != nil {
Expand All @@ -82,7 +82,7 @@ func containsDefault(args []string) error {

func getDefaultHostFile() string {
if runtime.GOOS == "linux" {
return "/etc/hosts"
return "/etc/hosts" // nolint: goconst
}

envHostFile := os.Getenv("HOSTCTL_FILE")
Expand Down Expand Up @@ -112,11 +112,17 @@ func isValidURL(s string) bool {
return true
}

func readerFromURL(url string) (io.Reader, error) {
resp, err := http.Get(url) // nolint:gosec
func readerFromURL(ctx context.Context, url string) (io.Reader, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}

defer resp.Body.Close()

b, err := ioutil.ReadAll(resp.Body)
Expand All @@ -141,6 +147,7 @@ func getRenderer(cmd *cobra.Command, opts *render.TableRendererOptions) types.Re
opts.Writer = cmd.OutOrStdout()
}

// nolint: goconst
switch {
case raw || out == "raw":
return render.NewRawRenderer(opts)
Expand Down
7 changes: 4 additions & 3 deletions cmd/hostctl/actions/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package actions

import (
"bytes"
"context"
"net/http"
"net/http/httptest"
"sync"
"testing"

"github.com/stretchr/testify/assert"

"github.com/guumaster/hostctl/pkg/parser"
"github.com/guumaster/hostctl/pkg/render"
"github.com/guumaster/hostctl/pkg/types"
"github.com/stretchr/testify/assert"
)

func TestContainsDefault(t *testing.T) {
Expand Down Expand Up @@ -113,7 +113,8 @@ func TestReadFromURL(t *testing.T) {
})
defer server.Close()

r, err := readerFromURL(server.URL)
ctx := context.Background()
r, err := readerFromURL(ctx, server.URL)
assert.NoError(t, err)

p, err := parser.ParseProfile(r)
Expand Down
3 changes: 1 addition & 2 deletions cmd/hostctl/actions/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package actions
import (
"fmt"

"github.com/spf13/cobra"

"github.com/guumaster/hostctl/pkg/file"
"github.com/guumaster/hostctl/pkg/types"
"github.com/spf13/cobra"
)

func newListCmd() *cobra.Command {
Expand Down
3 changes: 1 addition & 2 deletions cmd/hostctl/actions/post_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import (
"syscall"
"time"

"github.com/spf13/cobra"

"github.com/guumaster/cligger"
"github.com/spf13/cobra"
)

const longWaitTime = 999999
Expand Down
4 changes: 1 addition & 3 deletions cmd/hostctl/actions/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ package actions
import (
"strings"

"github.com/spf13/cobra"

"github.com/guumaster/cligger"

"github.com/guumaster/hostctl/pkg/file"
"github.com/spf13/cobra"
)

func newRemoveCmd() *cobra.Command {
Expand Down
Loading

0 comments on commit 06a9253

Please sign in to comment.