Skip to content

Commit

Permalink
Add more linters and fix issues
Browse files Browse the repository at this point in the history
Signed-off-by: Valentin Kiselev <[email protected]>
  • Loading branch information
mrexox committed Jun 9, 2022
1 parent 4556baa commit 360128f
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 31 deletions.
13 changes: 9 additions & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,5 +46,3 @@ linters:
- unused
- varcheck
- whitespace
- godot
- godox
16 changes: 7 additions & 9 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/available_hooks.go
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions pkg/config/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
6 changes: 4 additions & 2 deletions pkg/lefthook/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/evilmartians/lefthook/pkg/config"
)

const defaultDirMode = 0o755

type AddArgs struct {
Hook string

Expand Down Expand Up @@ -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
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/lefthook/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/lefthook/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

const (
checksumHookFilename = "prepare-commit-msg"
configFileMode = 0o666
configDefaultName = "lefthook.yml"
configGlob = "lefthook.y*ml"
)
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/lefthook/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/lefthook/lefthook.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/evilmartians/lefthook/pkg/templates"
)

const hookFileMode = 0o755

var lefthookContentRegexp = regexp.MustCompile("LEFTHOOK")

type Options struct {
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/lefthook/uninstall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
4 changes: 1 addition & 3 deletions pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import (
"github.com/logrusorgru/aurora"
)

var (
std = New()
)
var std = New()

type Level uint32

Expand Down

0 comments on commit 360128f

Please sign in to comment.