Skip to content

Commit

Permalink
refactor: add more go linters (filebrowser#970)
Browse files Browse the repository at this point in the history
  • Loading branch information
o1egl committed May 31, 2020
1 parent 54d92a2 commit 700f327
Show file tree
Hide file tree
Showing 56 changed files with 436 additions and 221 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ version: 2
jobs:
lint:
docker:
- image: golangci/golangci-lint:v1.16
- image: golangci/golangci-lint:v1.27.0
steps:
- checkout
- run: golangci-lint run -v -D errcheck
- run: golangci-lint run -v
build-node:
docker:
- image: circleci/node
Expand Down
132 changes: 132 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
linters-settings:
dupl:
threshold: 100
exhaustive:
default-signifies-exhaustive: false
funlen:
lines: 100
statements: 50
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/filebrowser/filebrowser
golint:
min-confidence: 0
gomnd:
settings:
mnd:
# don't include the "operation" and "assign"
checks: argument,case,condition,return
govet:
check-shadowing: true
lll:
line-length: 140
maligned:
suggest-new: true
misspell:
locale: US
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

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
- funlen
- gochecknoinits
- goconst
- gocritic
- gocyclo
- gofmt
- goimports
- golint
- gomnd
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- interfacer
- lll
- misspell
- nakedret
- nolintlint
- rowserrcheck
- scopelint
- staticcheck
- structcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
- varcheck
- whitespace
- prealloc

# don't enable:
# - asciicheck
# - exhaustive (TODO: enable after next release; current release at time of writing is v1.27)
# - gochecknoglobals
# - gocognit
# - godot
# - godox
# - goerr113
# - maligned
# - nestif
# - testpackage
# - wsl

issues:
exclude-rules:
- path: cmd/.*.go
linters:
- gochecknoinits
- path: .*_test.go
linters:
- lll
- gochecknoinits
- gocyclo
- funlen
- dupl
- scopelint
- text: "Auther"
linters:
- misspell

run:
skip-dirs:
- frontend/
skip-files:
- http/rice-box.go

# golangci.com configuration
# https://github.com/golangci/golangci/wiki/Configuration
service:
golangci-lint-version: 1.27.x # use the fixed version to not introduce new linters unexpectedly
7 changes: 4 additions & 3 deletions auth/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type jsonCred struct {
ReCaptcha string `json:"recaptcha"`
}

// JSONAuth is a json implementaion of an Auther.
// JSONAuth is a json implementation of an Auther.
type JSONAuth struct {
ReCaptcha *ReCaptcha `json:"recaptcha" yaml:"recaptcha"`
}
Expand All @@ -40,7 +40,7 @@ func (a JSONAuth) Auth(r *http.Request, sto *users.Storage, root string) (*users

// If ReCaptcha is enabled, check the code.
if a.ReCaptcha != nil && len(a.ReCaptcha.Secret) > 0 {
ok, err := a.ReCaptcha.Ok(cred.ReCaptcha)
ok, err := a.ReCaptcha.Ok(cred.ReCaptcha) //nolint:shadow

if err != nil {
return nil, err
Expand All @@ -66,7 +66,7 @@ func (a JSONAuth) LoginPage() bool {

const reCaptchaAPI = "/recaptcha/api/siteverify"

// ReCaptcha identifies a recaptcha conenction.
// ReCaptcha identifies a recaptcha connection.
type ReCaptcha struct {
Host string `json:"host"`
Key string `json:"key"`
Expand All @@ -89,6 +89,7 @@ func (r *ReCaptcha) Ok(response string) (bool, error) {
if err != nil {
return false, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return false, nil
Expand Down
4 changes: 2 additions & 2 deletions auth/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ type Storage struct {
}

// NewStorage creates a auth storage from a backend.
func NewStorage(back StorageBackend, users *users.Storage) *Storage {
return &Storage{back: back, users: users}
func NewStorage(back StorageBackend, userStore *users.Storage) *Storage {
return &Storage{back: back, users: userStore}
}

// Get wraps a StorageBackend.Get.
Expand Down
2 changes: 1 addition & 1 deletion cmd/cmds_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var cmdsAddCmd = &cobra.Command{
Use: "add <event> <command>",
Short: "Add a command to run on a specific event",
Long: `Add a command to run on a specific event.`,
Args: cobra.MinimumNArgs(2),
Args: cobra.MinimumNArgs(2), //nolint:mnd
Run: python(func(cmd *cobra.Command, args []string, d pythonData) {
s, err := d.store.Settings.Get()
checkErr(err)
Expand Down
4 changes: 2 additions & 2 deletions cmd/cmds_rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ You can also specify an optional parameter (index_end) so
you can remove all commands from 'index' to 'index_end',
including 'index_end'.`,
Args: func(cmd *cobra.Command, args []string) error {
if err := cobra.RangeArgs(2, 3)(cmd, args); err != nil {
if err := cobra.RangeArgs(2, 3)(cmd, args); err != nil { //nolint:mnd
return err
}

Expand All @@ -43,7 +43,7 @@ including 'index_end'.`,
i, err := strconv.Atoi(args[1])
checkErr(err)
f := i
if len(args) == 3 {
if len(args) == 3 { //nolint:mnd
f, err = strconv.Atoi(args[2])
checkErr(err)
}
Expand Down
11 changes: 7 additions & 4 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (
"strings"
"text/tabwriter"

"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/filebrowser/filebrowser/v2/auth"
"github.com/filebrowser/filebrowser/v2/errors"
"github.com/filebrowser/filebrowser/v2/settings"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

func init() {
Expand Down Expand Up @@ -44,6 +45,7 @@ func addConfigFlags(flags *pflag.FlagSet) {
flags.Bool("branding.disableExternal", false, "disable external links such as GitHub links")
}

//nolint:gocyclo
func getAuthentication(flags *pflag.FlagSet, defaults ...interface{}) (settings.AuthMethod, auth.Auther) {
method := settings.AuthMethod(mustGetString(flags, "auth.method"))

Expand All @@ -53,11 +55,12 @@ func getAuthentication(flags *pflag.FlagSet, defaults ...interface{}) (settings.
for _, arg := range defaults {
switch def := arg.(type) {
case *settings.Settings:
method = settings.AuthMethod(def.AuthMethod)
method = def.AuthMethod
case auth.Auther:
ms, err := json.Marshal(def)
checkErr(err)
json.Unmarshal(ms, &defaultAuther)
err = json.Unmarshal(ms, &defaultAuther)
checkErr(err)
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/config_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
"path/filepath"
"reflect"

"github.com/spf13/cobra"

"github.com/filebrowser/filebrowser/v2/auth"
"github.com/filebrowser/filebrowser/v2/settings"
"github.com/spf13/cobra"
)

func init() {
Expand Down Expand Up @@ -55,7 +56,7 @@ The path must be for a json or yaml file.`,
checkErr(err)

var rawAuther interface{}
if filepath.Ext(args[0]) != ".json" {
if filepath.Ext(args[0]) != ".json" { //nolint:goconst
rawAuther = cleanUpInterfaceMap(file.Auther.(map[interface{}]interface{}))
} else {
rawAuther = file.Auther
Expand Down
3 changes: 2 additions & 1 deletion cmd/config_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"fmt"
"strings"

"github.com/filebrowser/filebrowser/v2/settings"
"github.com/spf13/cobra"

"github.com/filebrowser/filebrowser/v2/settings"
)

func init() {
Expand Down
12 changes: 6 additions & 6 deletions cmd/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func generateMarkdown(cmd *cobra.Command, w io.Writer) {

short := cmd.Short
long := cmd.Long
if len(long) == 0 {
if long == "" {
long = short
}

Expand All @@ -106,21 +106,21 @@ func generateMarkdown(cmd *cobra.Command, w io.Writer) {
buf.WriteString(fmt.Sprintf("```\n%s\n```\n\n", cmd.Example))
}

printOptions(buf, cmd, name)
printOptions(buf, cmd)
_, err := buf.WriteTo(w)
checkErr(err)
}

func generateFlagsTable(fs *pflag.FlagSet, buf io.StringWriter) {
buf.WriteString("| Name | Shorthand | Usage |\n")
buf.WriteString("|------|-----------|-------|\n")
_, _ = buf.WriteString("| Name | Shorthand | Usage |\n")
_, _ = buf.WriteString("|------|-----------|-------|\n")

fs.VisitAll(func(f *pflag.Flag) {
buf.WriteString("|" + f.Name + "|" + f.Shorthand + "|" + f.Usage + "|\n")
_, _ = buf.WriteString("|" + f.Name + "|" + f.Shorthand + "|" + f.Usage + "|\n")
})
}

func printOptions(buf *bytes.Buffer, cmd *cobra.Command, name string) {
func printOptions(buf *bytes.Buffer, cmd *cobra.Command) {
flags := cmd.NonInheritedFlags()
flags.SetOutput(buf)
if flags.HasAvailableFlags() {
Expand Down
3 changes: 2 additions & 1 deletion cmd/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package cmd
import (
"fmt"

"github.com/filebrowser/filebrowser/v2/users"
"github.com/spf13/cobra"

"github.com/filebrowser/filebrowser/v2/users"
)

func init() {
Expand Down
Loading

0 comments on commit 700f327

Please sign in to comment.