diff --git a/.golangci.yml b/.golangci.yml index a6d09044..dc162832 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -15,21 +15,28 @@ linters: - dogsled - dupl - errcheck - - exportloopref - exhaustive + - exportloopref + - gci - goconst - gocyclo + - gocyclo + - godot + - godox - gofmt - - gci + - gofumpt - goimports + - gomnd - goprintffuncname - gosimple - govet - ineffassign - misspell - nakedret + - nestif - noctx - nolintlint + - revive - rowserrcheck - staticcheck - structcheck @@ -39,5 +46,3 @@ linters: - unused - varcheck - whitespace - - godot - - godox diff --git a/pkg/cmd/root.go b/pkg/cmd/root.go index 090cb7da..e5e5b907 100644 --- a/pkg/cmd/root.go +++ b/pkg/cmd/root.go @@ -8,15 +8,13 @@ import ( "github.com/evilmartians/lefthook/pkg/lefthook" ) -var ( - commands = [...]func(*lefthook.Options) *cobra.Command{ - NewVersionCmd, - NewAddCmd, - NewInstallCmd, - NewUninstallCmd, - NewRunCmd, - } -) +var commands = [...]func(*lefthook.Options) *cobra.Command{ + NewVersionCmd, + NewAddCmd, + NewInstallCmd, + NewUninstallCmd, + NewRunCmd, +} func NewRootCmd() *cobra.Command { options := lefthook.Options{ diff --git a/pkg/config/available_hooks.go b/pkg/config/available_hooks.go index 8a64d196..7eb29f21 100644 --- a/pkg/config/available_hooks.go +++ b/pkg/config/available_hooks.go @@ -1,6 +1,6 @@ package config -// See: https://git-scm.com/docs/githooks +// AvailableHooks - list of hooks taken from https://git-scm.com/docs/githooks. var AvailableHooks = [...]string{ "pre-applypatch", "applypatch-msg", diff --git a/pkg/config/load_test.go b/pkg/config/load_test.go index cf6ac9b2..12c42f26 100644 --- a/pkg/config/load_test.go +++ b/pkg/config/load_test.go @@ -141,11 +141,11 @@ pre-push: } { fs := afero.Afero{Fs: afero.NewMemMapFs()} t.Run(fmt.Sprintf("%d: %s", i, tt.name), func(t *testing.T) { - if err := fs.WriteFile("/lefthook.yml", tt.global, 0644); err != nil { + if err := fs.WriteFile("/lefthook.yml", tt.global, 0o644); err != nil { t.Errorf("unexpected error: %s", err) } - if err := fs.WriteFile("/lefthook-local.yml", tt.local, 0644); err != nil { + if err := fs.WriteFile("/lefthook-local.yml", tt.local, 0o644); err != nil { t.Errorf("unexpected error: %s", err) } diff --git a/pkg/lefthook/add.go b/pkg/lefthook/add.go index 6b8319c1..3e435c3c 100644 --- a/pkg/lefthook/add.go +++ b/pkg/lefthook/add.go @@ -7,6 +7,8 @@ import ( "github.com/evilmartians/lefthook/pkg/config" ) +const defaultDirMode = 0o755 + type AddArgs struct { Hook string @@ -44,10 +46,10 @@ func (l *Lefthook) Add(args *AddArgs) error { sourceDir := filepath.Join(l.repo.RootPath, global, args.Hook) sourceDirLocal := filepath.Join(l.repo.RootPath, local, args.Hook) println("HI", sourceDir, sourceDirLocal) - if err = l.Fs.MkdirAll(sourceDir, 0755); err != nil { + if err = l.Fs.MkdirAll(sourceDir, defaultDirMode); err != nil { return err } - if err = l.Fs.MkdirAll(sourceDirLocal, 0755); err != nil { + if err = l.Fs.MkdirAll(sourceDirLocal, defaultDirMode); err != nil { return err } } diff --git a/pkg/lefthook/add_test.go b/pkg/lefthook/add_test.go index 6bf35f02..a03add2c 100644 --- a/pkg/lefthook/add_test.go +++ b/pkg/lefthook/add_test.go @@ -124,10 +124,10 @@ source_dir_local: .source_dir_local lefthook := &Lefthook{Options: &Options{Fs: fs}, repo: repo} for file, content := range tt.existingFiles { - if err := fs.MkdirAll(filepath.Base(file), 0755); err != nil { + if err := fs.MkdirAll(filepath.Base(file), 0o755); err != nil { t.Errorf("unexpected error: %s", err) } - if err := afero.WriteFile(fs, file, []byte(content), 0644); err != nil { + if err := afero.WriteFile(fs, file, []byte(content), 0o644); err != nil { t.Errorf("unexpected error: %s", err) } } diff --git a/pkg/lefthook/install.go b/pkg/lefthook/install.go index 4902a14f..df426a9f 100644 --- a/pkg/lefthook/install.go +++ b/pkg/lefthook/install.go @@ -18,6 +18,7 @@ import ( const ( checksumHookFilename = "prepare-commit-msg" + configFileMode = 0o666 configDefaultName = "lefthook.yml" configGlob = "lefthook.y*ml" ) @@ -80,7 +81,7 @@ func (l *Lefthook) configExists(path string) bool { func (l *Lefthook) createConfig(path string) error { file := filepath.Join(path, configDefaultName) - err := afero.WriteFile(l.Fs, file, templates.Config(), 0666) + err := afero.WriteFile(l.Fs, file, templates.Config(), configFileMode) if err != nil { return err } diff --git a/pkg/lefthook/install_test.go b/pkg/lefthook/install_test.go index d2e5c752..beb3377e 100644 --- a/pkg/lefthook/install_test.go +++ b/pkg/lefthook/install_test.go @@ -209,17 +209,17 @@ post-commit: t.Run(fmt.Sprintf("%d: %s", n, tt.name), func(t *testing.T) { // Create configuration file if len(tt.config) > 0 { - if err := afero.WriteFile(fs, "/src/lefthook.yml", []byte(tt.config), 0644); err != nil { + if err := afero.WriteFile(fs, "/src/lefthook.yml", []byte(tt.config), 0o644); err != nil { t.Errorf("unexpected error: %s", err) } } // Create files that should exist for file, content := range tt.existingFiles { - if err := fs.MkdirAll(filepath.Base(file), 0755); err != nil { + if err := fs.MkdirAll(filepath.Base(file), 0o755); err != nil { t.Errorf("unexpected error: %s", err) } - if err := afero.WriteFile(fs, file, []byte(content), 0755); err != nil { + if err := afero.WriteFile(fs, file, []byte(content), 0o755); err != nil { t.Errorf("unexpected error: %s", err) } } diff --git a/pkg/lefthook/lefthook.go b/pkg/lefthook/lefthook.go index 269de136..9246e6a8 100644 --- a/pkg/lefthook/lefthook.go +++ b/pkg/lefthook/lefthook.go @@ -13,6 +13,8 @@ import ( "github.com/evilmartians/lefthook/pkg/templates" ) +const hookFileMode = 0o755 + var lefthookContentRegexp = regexp.MustCompile("LEFTHOOK") type Options struct { @@ -96,7 +98,8 @@ func (l *Lefthook) cleanHook(hook string, force bool) error { if force { log.Infof("File %s.old already exists, overwriting\n", hook) } else { - return fmt.Errorf("Can't rename %s to %s.old - file already exists\n", hook, hook) + log.Errorf("Can't rename %s to %s.old - file already exists\n", hook, hook) + return fmt.Errorf("file %s.old already exists", hook) } } @@ -112,7 +115,7 @@ func (l *Lefthook) cleanHook(hook string, force bool) error { func (l *Lefthook) addHook(hook, configChecksum string) error { hookPath := filepath.Join(l.repo.HooksPath, hook) err := afero.WriteFile( - l.Fs, hookPath, templates.Hook(hook, configChecksum), 0755, + l.Fs, hookPath, templates.Hook(hook, configChecksum), hookFileMode, ) if err != nil { return err diff --git a/pkg/lefthook/uninstall_test.go b/pkg/lefthook/uninstall_test.go index 29c2b79b..bd33740e 100644 --- a/pkg/lefthook/uninstall_test.go +++ b/pkg/lefthook/uninstall_test.go @@ -93,10 +93,10 @@ func TestLefthookUninstall(t *testing.T) { // Prepare files that should exist for file, content := range tt.existingFiles { - if err := fs.MkdirAll(filepath.Base(file), 0755); err != nil { + if err := fs.MkdirAll(filepath.Base(file), 0o755); err != nil { t.Errorf("unexpected error: %s", err) } - if err := afero.WriteFile(fs, file, []byte(content), 0755); err != nil { + if err := afero.WriteFile(fs, file, []byte(content), 0o755); err != nil { t.Errorf("unexpected error: %s", err) } } diff --git a/pkg/log/log.go b/pkg/log/log.go index c96866db..89002285 100644 --- a/pkg/log/log.go +++ b/pkg/log/log.go @@ -10,9 +10,7 @@ import ( "github.com/logrusorgru/aurora" ) -var ( - std = New() -) +var std = New() type Level uint32