From 2e5825a380d31712a2b018d541bc2ef326024345 Mon Sep 17 00:00:00 2001 From: Daniel Milde Date: Mon, 29 Apr 2024 12:33:00 +0200 Subject: [PATCH 01/24] refactor: constants for action and acting --- tui/actions.go | 16 +++++++++++----- tui/background.go | 6 +++--- tui/marked.go | 14 +++++++------- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/tui/actions.go b/tui/actions.go index d09216b41..04e931b87 100644 --- a/tui/actions.go +++ b/tui/actions.go @@ -25,6 +25,12 @@ import ( const ( defaultLinesCount = 500 linesTreshold = 20 + + actionEmpty = "empty" + actionDelete = "delete" + + actingEmpty = "emptying" + actingDelete = "deleting" ) // ListDevices lists mounted devices and shows their disk usage @@ -178,11 +184,11 @@ func (ui *UI) deleteSelected(shouldEmpty bool) { var action, acting string if shouldEmpty { - action = "empty " - acting = "emptying" + action = actionEmpty + acting = actingEmpty } else { - action = "delete " - acting = "deleting" + action = actionDelete + acting = actingDelete } modal := tview.NewModal().SetText( // nolint: staticcheck // Why: fixed string @@ -214,7 +220,7 @@ func (ui *UI) deleteSelected(shouldEmpty bool) { go func() { for _, item := range deleteItems { if err := deleteFun(currentDir, item); err != nil { - msg := "Can't " + action + tview.Escape(selectedItem.GetName()) + msg := "Can't " + action + " " + tview.Escape(selectedItem.GetName()) ui.app.QueueUpdateDraw(func() { ui.pages.RemovePage(acting) ui.showErr(msg, err) diff --git a/tui/background.go b/tui/background.go index f373f233d..8974defb3 100644 --- a/tui/background.go +++ b/tui/background.go @@ -35,9 +35,9 @@ func (ui *UI) deleteItem(item fs.Item, shouldEmpty bool) { var action, acting string if shouldEmpty { - action = "empty " + action = actionEmpty } else { - action = "delete " + action = actionDelete } var deleteFun func(fs.Item, fs.Item) error @@ -61,7 +61,7 @@ func (ui *UI) deleteItem(item fs.Item, shouldEmpty bool) { for _, toDelete := range deleteItems { if err := deleteFun(parentDir, toDelete); err != nil { - msg := "Can't " + action + tview.Escape(toDelete.GetName()) + msg := "Can't " + action + " " + tview.Escape(toDelete.GetName()) ui.app.QueueUpdateDraw(func() { ui.pages.RemovePage(acting) ui.showErr(msg, err) diff --git a/tui/marked.go b/tui/marked.go index db9d3e98b..129b53e17 100644 --- a/tui/marked.go +++ b/tui/marked.go @@ -24,11 +24,11 @@ func (ui *UI) fileItemMarked(row int) { func (ui *UI) deleteMarked(shouldEmpty bool) { var action, acting string if shouldEmpty { - action = "empty " - acting = "emptying" + action = actionEmpty + acting = actingEmpty } else { - action = "delete " - acting = "deleting" + action = actionDelete + acting = actingDelete } var currentDir fs.Item @@ -81,7 +81,7 @@ func (ui *UI) deleteMarked(shouldEmpty bool) { for _, item := range deleteItems { if err := deleteFun(currentDir, item); err != nil { - msg := "Can't " + action + tview.Escape(one.GetName()) + msg := "Can't " + action + " " + tview.Escape(one.GetName()) ui.app.QueueUpdateDraw(func() { ui.pages.RemovePage(acting) ui.showErr(msg, err) @@ -112,9 +112,9 @@ func (ui *UI) deleteMarked(shouldEmpty bool) { func (ui *UI) confirmDeletionMarked(shouldEmpty bool) { var action string if shouldEmpty { - action = "empty" + action = actionEmpty } else { - action = "delete" + action = actionDelete } modal := tview.NewModal(). From 6371855c5d6e5bcfae7a79c85e70b695e0cdb219 Mon Sep 17 00:00:00 2001 From: Daniel Milde Date: Mon, 29 Apr 2024 12:41:21 +0200 Subject: [PATCH 02/24] refactor: combine param types --- internal/testanalyze/analyze.go | 6 +++--- pkg/remove/parallel.go | 2 +- pkg/remove/remove.go | 4 ++-- stdout/stdout.go | 2 +- tui/exec.go | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/internal/testanalyze/analyze.go b/internal/testanalyze/analyze.go index 3bb053279..d0f2bdb0d 100644 --- a/internal/testanalyze/analyze.go +++ b/internal/testanalyze/analyze.go @@ -85,18 +85,18 @@ func (a *MockedAnalyzer) ResetProgress() {} func (a *MockedAnalyzer) SetFollowSymlinks(v bool) {} // RemoveItemFromDirWithErr returns error -func RemoveItemFromDirWithErr(dir fs.Item, file fs.Item) error { +func RemoveItemFromDirWithErr(dir, file fs.Item) error { return errors.New("Failed") } // RemoveItemFromDirWithSleep returns error -func RemoveItemFromDirWithSleep(dir fs.Item, file fs.Item) error { +func RemoveItemFromDirWithSleep(dir, file fs.Item) error { time.Sleep(time.Millisecond * 600) return remove.RemoveItemFromDir(dir, file) } // RemoveItemFromDirWithSleepAndErr returns error -func RemoveItemFromDirWithSleepAndErr(dir fs.Item, file fs.Item) error { +func RemoveItemFromDirWithSleepAndErr(dir, file fs.Item) error { time.Sleep(time.Millisecond * 600) return errors.New("Failed") } diff --git a/pkg/remove/parallel.go b/pkg/remove/parallel.go index d5dd11e8a..27a43ea81 100644 --- a/pkg/remove/parallel.go +++ b/pkg/remove/parallel.go @@ -11,7 +11,7 @@ import ( var concurrencyLimit = make(chan struct{}, 3*runtime.GOMAXPROCS(0)) // RemoveItemFromDirParallel removes item from dir -func RemoveItemFromDirParallel(dir fs.Item, item fs.Item) error { +func RemoveItemFromDirParallel(dir, item fs.Item) error { if !item.IsDir() { return RemoveItemFromDir(dir, item) } diff --git a/pkg/remove/remove.go b/pkg/remove/remove.go index 1d17985ba..9fba6d4d2 100644 --- a/pkg/remove/remove.go +++ b/pkg/remove/remove.go @@ -8,7 +8,7 @@ import ( ) // RemoveItemFromDir removes item from dir -func RemoveItemFromDir(dir fs.Item, item fs.Item) error { +func RemoveItemFromDir(dir, item fs.Item) error { err := os.RemoveAll(item.GetPath()) if err != nil { return err @@ -19,7 +19,7 @@ func RemoveItemFromDir(dir fs.Item, item fs.Item) error { } // EmptyFileFromDir empty file from dir -func EmptyFileFromDir(dir fs.Item, file fs.Item) error { +func EmptyFileFromDir(dir, file fs.Item) error { err := os.Truncate(file.GetPath(), 0) if err != nil { return err diff --git a/stdout/stdout.go b/stdout/stdout.go index 5b1ecba84..669fc69d4 100644 --- a/stdout/stdout.go +++ b/stdout/stdout.go @@ -440,7 +440,7 @@ func maxLength(list []*device.Device, keyGetter func(*device.Device) string) int return maxLen } -func maxInt(x int, y int) int { +func maxInt(x, y int) int { if x > y { return x } diff --git a/tui/exec.go b/tui/exec.go index 7e5af2977..24c3e1169 100644 --- a/tui/exec.go +++ b/tui/exec.go @@ -6,7 +6,7 @@ import ( ) // Execute runs given bin path via exec.Command call -func Execute(argv0 string, argv []string, envv []string) error { +func Execute(argv0 string, argv, envv []string) error { cmd := exec.Command(argv0, argv...) cmd.Stdout = os.Stdout From 493edd8142a65c9b6e4507726a5ac6ea0853fd98 Mon Sep 17 00:00:00 2001 From: Daniel Milde Date: Mon, 29 Apr 2024 12:49:17 +0200 Subject: [PATCH 03/24] refactor: type switch to type cond --- pkg/analyze/dir_unix.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/analyze/dir_unix.go b/pkg/analyze/dir_unix.go index 02bed9325..88f5afaf7 100644 --- a/pkg/analyze/dir_unix.go +++ b/pkg/analyze/dir_unix.go @@ -12,8 +12,7 @@ import ( const devBSize = 512 func setPlatformSpecificAttrs(file *File, f os.FileInfo) { - switch stat := f.Sys().(type) { - case *syscall.Stat_t: + if stat, ok := f.Sys().(*syscall.Stat_t); ok { file.Usage = stat.Blocks * devBSize file.Mtime = time.Unix(int64(stat.Mtimespec.Sec), int64(stat.Mtimespec.Nsec)) From 8e40aec2e904051e2014c7e04fc023cc9be4d1a2 Mon Sep 17 00:00:00 2001 From: Daniel Milde Date: Mon, 29 Apr 2024 12:50:38 +0200 Subject: [PATCH 04/24] refactor: single case switch --- pkg/analyze/file.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/analyze/file.go b/pkg/analyze/file.go index 8184ec7dc..19217194d 100644 --- a/pkg/analyze/file.go +++ b/pkg/analyze/file.go @@ -66,8 +66,7 @@ func (f *File) GetMtime() time.Time { // GetType returns name type of item func (f *File) GetType() string { - switch f.Flag { - case '@': + if f.Flag == '@' { return "Other" } return "File" From f5ae2b7d2f678327fe0e2f34132041d53ee24efd Mon Sep 17 00:00:00 2001 From: Daniel Milde Date: Mon, 29 Apr 2024 12:52:13 +0200 Subject: [PATCH 05/24] refactor: type switch to type cond --- report/export.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/report/export.go b/report/export.go index e0119339a..9f0ee96b8 100644 --- a/report/export.go +++ b/report/export.go @@ -150,8 +150,7 @@ func (ui *UI) exportDir(dir fs.Item, waitWritten *sync.WaitGroup) error { return err } - switch f := ui.exportOutput.(type) { - case *os.File: + if f, ok := ui.exportOutput.(*os.File); ok { err = f.Close() if err != nil { return err From 173458b73a8a6dcf0b075b4b78e0e033ab089288 Mon Sep 17 00:00:00 2001 From: Daniel Milde Date: Tue, 30 Apr 2024 13:03:49 +0200 Subject: [PATCH 06/24] lint --- pkg/analyze/stored.go | 2 +- pkg/analyze/wait.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/analyze/stored.go b/pkg/analyze/stored.go index 4e0ba1041..1393f4bde 100644 --- a/pkg/analyze/stored.go +++ b/pkg/analyze/stored.go @@ -385,7 +385,7 @@ func (p *ParentDir) GetFiles() fs.Files { panic("m func (p *ParentDir) GetFilesLocked() fs.Files { panic("must not be called") } func (p *ParentDir) RLock() func() { panic("must not be called") } func (p *ParentDir) SetFiles(fs.Files) { panic("must not be called") } -func (f *ParentDir) RemoveFile(item fs.Item) { panic("must not be called") } +func (p *ParentDir) RemoveFile(item fs.Item) { panic("must not be called") } func (p *ParentDir) GetItemStats( linkedItems fs.HardLinkedItems, ) (int, int64, int64) { diff --git a/pkg/analyze/wait.go b/pkg/analyze/wait.go index 204921a54..f81b57740 100644 --- a/pkg/analyze/wait.go +++ b/pkg/analyze/wait.go @@ -19,7 +19,7 @@ func (s *WaitGroup) Init() *WaitGroup { // Add increments value func (s *WaitGroup) Add(value int) { s.access.Lock() - s.value = s.value + value + s.value += value s.access.Unlock() } From 002e182b3103af5bc9ef119887e511032e35c97c Mon Sep 17 00:00:00 2001 From: Daniel Milde Date: Tue, 30 Apr 2024 13:04:09 +0200 Subject: [PATCH 07/24] ci: golangci-lint config --- .golangci.yml | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++ Makefile | 4 ++ 2 files changed, 126 insertions(+) create mode 100644 .golangci.yml diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 000000000..e5e9a87db --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,122 @@ +linters-settings: + errcheck: + check-blank: true + revive: + rules: + - name: blank-imports + - name: context-as-argument + - name: context-keys-type + - name: dot-imports + - name: error-return + - name: error-strings + - name: error-naming + - name: exported + - name: increment-decrement + - name: var-naming + - name: var-declaration + - name: package-comments + - name: range + - name: receiver-naming + - name: time-naming + - name: unexported-return + - name: indent-error-flow + - name: errorf + - name: empty-block + - name: superfluous-else + - name: unreachable-code + - name: redefines-builtin-id + # While we agree with this rule, right now it would break too many + # projects. So, we disable it by default. + - name: unused-parameter + disabled: true + gocyclo: + min-complexity: 25 + dupl: + threshold: 100 + goconst: + min-len: 3 + min-occurrences: 3 + lll: + line-length: 160 + gocritic: + enabled-tags: + - diagnostic + - experimental + - opinionated + - performance + - style + disabled-checks: + - whyNoLint + funlen: + lines: 500 + statements: 50 + govet: + enable: + - shadow + +linters: + disable-all: true + enable: + - bodyclose + - dogsled + - errcheck + - errorlint + - exhaustive + - exportloopref + - funlen + - goconst + - gocritic + - gocyclo + - gofmt + - goimports + - revive + - gosimple + - govet + - ineffassign + - lll + - nakedret + - staticcheck + - typecheck + - unparam + - unused + - whitespace + +issues: + exclude: + # We allow error shadowing + - 'declaration of "err" shadows declaration at' + + # Excluding configuration per-path, per-linter, per-text and per-source + exclude-rules: + # Exclude some linters from running on tests files. + - path: _test\.go + linters: + - gocyclo + - errcheck + - gosec + - funlen + - gocritic + - gochecknoglobals # Globals in test files are tolerated. + - goconst # Repeated consts in test files are tolerated. + # This rule is buggy and breaks on our `///Block` lines. Disable for now. + - linters: + - gocritic + text: "commentFormatting: put a space" + # This rule incorrectly flags nil references after assert.Assert(t, x != nil) + - path: _test\.go + text: "SA5011" + linters: + - staticcheck + - linters: + - lll + source: "^//go:generate " + - linters: + - lll + - gocritic + path: \.resolvers\.go + source: '^func \(r \*[a-zA-Z]+Resolvers\) ' + +output: + formats: + - format: colored-line-number + sort-results: true diff --git a/Makefile b/Makefile index d2584797b..5699d582d 100644 --- a/Makefile +++ b/Makefile @@ -130,6 +130,9 @@ benchmark: 'gdu -npc ~' 'gdu -gnpc ~' 'gdu -npc --use-storage ~' sudo cpupower frequency-set -g schedutil +lint: + golangci-lint run -c .golangci.yml + clean: go mod tidy -rm coverage.txt @@ -151,5 +154,6 @@ install-dev-dependencies: go install gotest.tools/gotestsum@latest go install github.com/mitchellh/gox@latest go install honnef.co/go/gotraceui/cmd/gotraceui@master + go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest .PHONY: run build build-static build-all test gobench benchmark coverage coverage-html clean clean-uncompressed-dist man show-man release From 4d5d6081a97471d62e58b7681a7e6a55ba705e15 Mon Sep 17 00:00:00 2001 From: Daniel Milde Date: Tue, 30 Apr 2024 14:04:44 +0200 Subject: [PATCH 08/24] refactor: named result --- pkg/analyze/file.go | 4 ++-- pkg/fs/file.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/analyze/file.go b/pkg/analyze/file.go index 19217194d..87f749e82 100644 --- a/pkg/analyze/file.go +++ b/pkg/analyze/file.go @@ -96,7 +96,7 @@ func (f *File) alreadyCounted(linkedItems fs.HardLinkedItems) bool { } // GetItemStats returns 1 as count of items, apparent usage and real usage of this file -func (f *File) GetItemStats(linkedItems fs.HardLinkedItems) (int, int64, int64) { +func (f *File) GetItemStats(linkedItems fs.HardLinkedItems) (itemCount int, size, usage int64) { if f.alreadyCounted(linkedItems) { return 1, 0, 0 } @@ -197,7 +197,7 @@ func (f *Dir) GetPath() string { } // GetItemStats returns item count, apparent usage and real usage of this dir -func (f *Dir) GetItemStats(linkedItems fs.HardLinkedItems) (int, int64, int64) { +func (f *Dir) GetItemStats(linkedItems fs.HardLinkedItems) (itemCount int, size, usage int64) { f.UpdateStats(linkedItems) return f.ItemCount, f.GetSize(), f.GetUsage() } diff --git a/pkg/fs/file.go b/pkg/fs/file.go index 7d286b0cc..9b83aa277 100644 --- a/pkg/fs/file.go +++ b/pkg/fs/file.go @@ -22,7 +22,7 @@ type Item interface { SetParent(Item) GetMultiLinkedInode() uint64 EncodeJSON(writer io.Writer, topLevel bool) error - GetItemStats(linkedItems HardLinkedItems) (int, int64, int64) + GetItemStats(linkedItems HardLinkedItems) (itemCount int, size, usage int64) UpdateStats(linkedItems HardLinkedItems) AddFile(Item) GetFiles() Files From 5987685f54f5242716820fa5e9a7c561d832d922 Mon Sep 17 00:00:00 2001 From: Daniel Milde Date: Tue, 30 Apr 2024 14:05:21 +0200 Subject: [PATCH 09/24] chore: ignore gocyclo --- internal/common/ignore.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/common/ignore.go b/internal/common/ignore.go index 366b0598d..66d2b4e2b 100644 --- a/internal/common/ignore.go +++ b/internal/common/ignore.go @@ -100,6 +100,7 @@ func (ui *UI) IsHiddenDir(name, path string) bool { } // CreateIgnoreFunc returns function for detecting if dir should be ignored +// nolint: gocyclo // Why: This function is a switch statement that is not too complex func (ui *UI) CreateIgnoreFunc() ShouldDirBeIgnored { switch { case len(ui.IgnoreDirPaths) > 0 && ui.IgnoreDirPathPatterns == nil && !ui.IgnoreHidden: From d5fb5488b5fd6e829b4b2f2dc27248364f9f3a44 Mon Sep 17 00:00:00 2001 From: Daniel Milde Date: Tue, 30 Apr 2024 14:05:41 +0200 Subject: [PATCH 10/24] chore: ignore false positive unparam --- cmd/gdu/app/app_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/gdu/app/app_test.go b/cmd/gdu/app/app_test.go index e5a276552..61e0255f6 100644 --- a/cmd/gdu/app/app_test.go +++ b/cmd/gdu/app/app_test.go @@ -500,6 +500,7 @@ func TestMaxCoresLowEdge(t *testing.T) { assert.Nil(t, err) } +// nolint: unparam // Why: it's used in linux tests func runApp(flags *Flags, args []string, istty bool, getter device.DevicesInfoGetter) (string, error) { buff := bytes.NewBufferString("") From ca00034c59f7f2237b429a4d7b499e03721702c0 Mon Sep 17 00:00:00 2001 From: Daniel Milde Date: Tue, 30 Apr 2024 15:06:40 +0200 Subject: [PATCH 11/24] refactor: empty fallthrough --- pkg/analyze/parallel.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/pkg/analyze/parallel.go b/pkg/analyze/parallel.go index eedce4dc6..1488e601a 100644 --- a/pkg/analyze/parallel.go +++ b/pkg/analyze/parallel.go @@ -211,14 +211,10 @@ func getDirFlag(err error, items int) rune { } func getFlag(f os.FileInfo) rune { - switch { - case f.Mode()&os.ModeSymlink != 0: - fallthrough - case f.Mode()&os.ModeSocket != 0: + if f.Mode()&os.ModeSymlink != 0 || f.Mode()&os.ModeSocket != 0 { return '@' - default: - return ' ' } + return ' ' } func followSymlink(path string, f *os.FileInfo) error { From ce2cbca84e94dc6ae4ec3790786f468a3ef1bfe6 Mon Sep 17 00:00:00 2001 From: Daniel Milde Date: Tue, 30 Apr 2024 15:56:23 +0200 Subject: [PATCH 12/24] refactor: non-pointer type --- pkg/analyze/parallel.go | 15 +++++++++------ pkg/analyze/sequential.go | 5 ++++- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/pkg/analyze/parallel.go b/pkg/analyze/parallel.go index 1488e601a..7fa517a6c 100644 --- a/pkg/analyze/parallel.go +++ b/pkg/analyze/parallel.go @@ -140,12 +140,15 @@ func (a *ParallelAnalyzer) processDir(path string) *Dir { continue } if a.followSymlinks && info.Mode()&os.ModeSymlink != 0 { - err = followSymlink(entryPath, &info) + infoF, err := followSymlink(entryPath) if err != nil { log.Print(err.Error()) dir.Flag = '!' continue } + if infoF != nil { + info = infoF + } } file = &File{ @@ -217,17 +220,17 @@ func getFlag(f os.FileInfo) rune { return ' ' } -func followSymlink(path string, f *os.FileInfo) error { +func followSymlink(path string) (os.FileInfo, error) { target, err := filepath.EvalSymlinks(path) if err != nil { - return err + return nil, err } tInfo, err := os.Lstat(target) if err != nil { - return err + return nil, err } if !tInfo.IsDir() { - *f = tInfo + return tInfo, nil } - return nil + return nil, nil } diff --git a/pkg/analyze/sequential.go b/pkg/analyze/sequential.go index 77fbd44d0..507ebfd98 100644 --- a/pkg/analyze/sequential.go +++ b/pkg/analyze/sequential.go @@ -127,12 +127,15 @@ func (a *SequentialAnalyzer) processDir(path string) *Dir { continue } if a.followSymlinks && info.Mode()&os.ModeSymlink != 0 { - err = followSymlink(entryPath, &info) + infoF, err := followSymlink(entryPath) if err != nil { log.Print(err.Error()) dir.Flag = '!' continue } + if infoF != nil { + info = infoF + } } file = &File{ From 6957953485b4a13dab496a5e886ae1baa0acebe0 Mon Sep 17 00:00:00 2001 From: Daniel Milde Date: Tue, 30 Apr 2024 15:58:28 +0200 Subject: [PATCH 13/24] refactor: named result --- pkg/analyze/stored.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/analyze/stored.go b/pkg/analyze/stored.go index 1393f4bde..2f9fde595 100644 --- a/pkg/analyze/stored.go +++ b/pkg/analyze/stored.go @@ -315,7 +315,7 @@ func (f *StoredDir) RemoveFile(item fs.Item) { } // GetItemStats returns item count, apparent usage and real usage of this dir -func (f *StoredDir) GetItemStats(linkedItems fs.HardLinkedItems) (int, int64, int64) { +func (f *StoredDir) GetItemStats(linkedItems fs.HardLinkedItems) (itemCount int, size, usage int64) { f.UpdateStats(linkedItems) return f.ItemCount, f.GetSize(), f.GetUsage() } @@ -388,6 +388,6 @@ func (p *ParentDir) SetFiles(fs.Files) { panic("m func (p *ParentDir) RemoveFile(item fs.Item) { panic("must not be called") } func (p *ParentDir) GetItemStats( linkedItems fs.HardLinkedItems, -) (int, int64, int64) { +) (itemCount int, size, usage int64) { panic("must not be called") } From 66a0c850a1ee210de2c68b9380f265690a8e7d37 Mon Sep 17 00:00:00 2001 From: Daniel Milde Date: Tue, 30 Apr 2024 16:00:46 +0200 Subject: [PATCH 14/24] refactor: invert cond --- pkg/device/dev_freebsd_darwin_other.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/pkg/device/dev_freebsd_darwin_other.go b/pkg/device/dev_freebsd_darwin_other.go index 67c3e844e..215462ea8 100644 --- a/pkg/device/dev_freebsd_darwin_other.go +++ b/pkg/device/dev_freebsd_darwin_other.go @@ -80,18 +80,20 @@ func processMounts(mounts Devices, ignoreErrors bool) (Devices, error) { devices := Devices{} for _, mount := range mounts { - if strings.HasPrefix(mount.Name, "/dev") || mount.Fstype == "zfs" { - info := &unix.Statfs_t{} - err := unix.Statfs(mount.MountPoint, info) - if err != nil && !ignoreErrors { - return nil, err - } - - mount.Size = int64(info.Bsize) * int64(info.Blocks) - mount.Free = int64(info.Bsize) * int64(info.Bavail) + if !strings.HasPrefix(mount.Name, "/dev") && mount.Fstype != "zfs" { + continue + } - devices = append(devices, mount) + info := &unix.Statfs_t{} + err := unix.Statfs(mount.MountPoint, info) + if err != nil && !ignoreErrors { + return nil, err } + + mount.Size = int64(info.Bsize) * int64(info.Blocks) + mount.Free = int64(info.Bsize) * int64(info.Bavail) + + devices = append(devices, mount) } return devices, nil From a12b097b5ef8f391993fa454b2aed9a14f0e7fc7 Mon Sep 17 00:00:00 2001 From: Daniel Milde Date: Tue, 30 Apr 2024 16:01:56 +0200 Subject: [PATCH 15/24] refactor: lll --- pkg/device/dev_freebsd_darwin_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/device/dev_freebsd_darwin_test.go b/pkg/device/dev_freebsd_darwin_test.go index db286c299..2cf007754 100644 --- a/pkg/device/dev_freebsd_darwin_test.go +++ b/pkg/device/dev_freebsd_darwin_test.go @@ -34,7 +34,9 @@ argon:/usr/obj on /usr/obj (nfs)`)) } func TestMountsWithSpace(t *testing.T) { - mounts, err := readMountOutput(strings.NewReader(`//inglor@vault.lan/volatile on /Users/inglor/Mountpoints/volatile (vault.lan) (smbfs, nodev, nosuid, mounted by inglor)`)) + mounts, err := readMountOutput(strings.NewReader( + `//inglor@vault.lan/volatile on /Users/inglor/Mountpoints/volatile (vault.lan) (smbfs, nodev, nosuid, mounted by inglor)`, + )) assert.Equal(t, "//inglor@vault.lan/volatile", mounts[0].Name) assert.Equal(t, "/Users/inglor/Mountpoints/volatile (vault.lan)", mounts[0].MountPoint) assert.Equal(t, "smbfs", mounts[0].Fstype) From 3a607e4b8730022c2888a45c84a1c8604eb85aa2 Mon Sep 17 00:00:00 2001 From: Daniel Milde Date: Tue, 30 Apr 2024 16:12:34 +0200 Subject: [PATCH 16/24] refactor: rename --- internal/testanalyze/analyze.go | 14 +++++++------- pkg/remove/parallel.go | 6 +++--- pkg/remove/parallel_linux_test.go | 8 ++++---- pkg/remove/parallel_test.go | 4 ++-- pkg/remove/remove.go | 4 ++-- pkg/remove/remove_linux_test.go | 2 +- pkg/remove/remove_test.go | 2 +- tui/tui.go | 4 ++-- tui/tui_test.go | 12 ++++++------ 9 files changed, 28 insertions(+), 28 deletions(-) diff --git a/internal/testanalyze/analyze.go b/internal/testanalyze/analyze.go index d0f2bdb0d..22a8f9ca1 100644 --- a/internal/testanalyze/analyze.go +++ b/internal/testanalyze/analyze.go @@ -84,19 +84,19 @@ func (a *MockedAnalyzer) ResetProgress() {} // SetFollowSymlinks does nothing func (a *MockedAnalyzer) SetFollowSymlinks(v bool) {} -// RemoveItemFromDirWithErr returns error -func RemoveItemFromDirWithErr(dir, file fs.Item) error { +// ItemFromDirWithErr returns error +func ItemFromDirWithErr(dir, file fs.Item) error { return errors.New("Failed") } -// RemoveItemFromDirWithSleep returns error -func RemoveItemFromDirWithSleep(dir, file fs.Item) error { +// ItemFromDirWithSleep returns error +func ItemFromDirWithSleep(dir, file fs.Item) error { time.Sleep(time.Millisecond * 600) - return remove.RemoveItemFromDir(dir, file) + return remove.ItemFromDir(dir, file) } -// RemoveItemFromDirWithSleepAndErr returns error -func RemoveItemFromDirWithSleepAndErr(dir, file fs.Item) error { +// ItemFromDirWithSleepAndErr returns error +func ItemFromDirWithSleepAndErr(dir, file fs.Item) error { time.Sleep(time.Millisecond * 600) return errors.New("Failed") } diff --git a/pkg/remove/parallel.go b/pkg/remove/parallel.go index 27a43ea81..606db205b 100644 --- a/pkg/remove/parallel.go +++ b/pkg/remove/parallel.go @@ -10,10 +10,10 @@ import ( var concurrencyLimit = make(chan struct{}, 3*runtime.GOMAXPROCS(0)) -// RemoveItemFromDirParallel removes item from dir -func RemoveItemFromDirParallel(dir, item fs.Item) error { +// ItemFromDirParallel removes item from dir +func ItemFromDirParallel(dir, item fs.Item) error { if !item.IsDir() { - return RemoveItemFromDir(dir, item) + return ItemFromDir(dir, item) } errChan := make(chan error, 1) // we show only first error var wait sync.WaitGroup diff --git a/pkg/remove/parallel_linux_test.go b/pkg/remove/parallel_linux_test.go index fca34ef17..f628842c9 100644 --- a/pkg/remove/parallel_linux_test.go +++ b/pkg/remove/parallel_linux_test.go @@ -13,7 +13,7 @@ import ( "github.com/stretchr/testify/assert" ) -func TestRemoveItemFromDirParallelWithErr(t *testing.T) { +func TestItemFromDirParallelWithErr(t *testing.T) { fin := testdir.CreateTestDir() defer fin() @@ -38,11 +38,11 @@ func TestRemoveItemFromDirParallelWithErr(t *testing.T) { }, } - err = RemoveItemFromDirParallel(dir, subdir) + err = ItemFromDirParallel(dir, subdir) assert.Contains(t, err.Error(), "permission denied") } -func TestRemoveItemFromDirParallelWithErr2(t *testing.T) { +func TestItemFromDirParallelWithErr2(t *testing.T) { fin := testdir.CreateTestDir() defer fin() @@ -62,6 +62,6 @@ func TestRemoveItemFromDirParallelWithErr2(t *testing.T) { subdir := dir.Files[0].(*analyze.Dir) - err = RemoveItemFromDirParallel(dir, subdir) + err = ItemFromDirParallel(dir, subdir) assert.Contains(t, err.Error(), "permission denied") } diff --git a/pkg/remove/parallel_test.go b/pkg/remove/parallel_test.go index 6196d7875..f53ef5cf5 100644 --- a/pkg/remove/parallel_test.go +++ b/pkg/remove/parallel_test.go @@ -39,7 +39,7 @@ func TestRemoveFileParallel(t *testing.T) { dir.Files = fs.Files{subdir} subdir.Files = fs.Files{file} - err := RemoveItemFromDirParallel(subdir, file) + err := ItemFromDirParallel(subdir, file) assert.Nil(t, err) assert.Equal(t, 0, len(subdir.Files)) @@ -64,6 +64,6 @@ func TestRemoveDirParallel(t *testing.T) { subdir := dir.Files[0].(*analyze.Dir) - err := RemoveItemFromDirParallel(dir, subdir) + err := ItemFromDirParallel(dir, subdir) assert.Nil(t, err) } diff --git a/pkg/remove/remove.go b/pkg/remove/remove.go index 9fba6d4d2..a385eeba1 100644 --- a/pkg/remove/remove.go +++ b/pkg/remove/remove.go @@ -7,8 +7,8 @@ import ( "github.com/dundee/gdu/v5/pkg/fs" ) -// RemoveItemFromDir removes item from dir -func RemoveItemFromDir(dir, item fs.Item) error { +// ItemFromDir removes item from dir +func ItemFromDir(dir, item fs.Item) error { err := os.RemoveAll(item.GetPath()) if err != nil { return err diff --git a/pkg/remove/remove_linux_test.go b/pkg/remove/remove_linux_test.go index 19fc5036c..2739ba928 100644 --- a/pkg/remove/remove_linux_test.go +++ b/pkg/remove/remove_linux_test.go @@ -37,6 +37,6 @@ func TestRemoveFileWithErr(t *testing.T) { }, } - err = RemoveItemFromDir(dir, subdir) + err = ItemFromDir(dir, subdir) assert.Contains(t, err.Error(), "permission denied") } diff --git a/pkg/remove/remove_test.go b/pkg/remove/remove_test.go index 11a1c2362..a2ed69785 100644 --- a/pkg/remove/remove_test.go +++ b/pkg/remove/remove_test.go @@ -83,7 +83,7 @@ func TestRemoveFile(t *testing.T) { dir.Files = fs.Files{subdir} subdir.Files = fs.Files{file} - err := RemoveItemFromDir(subdir, file) + err := ItemFromDir(subdir, file) assert.Nil(t, err) assert.Equal(t, 0, len(subdir.Files)) diff --git a/tui/tui.go b/tui/tui.go index 23d059953..70c11e8b8 100644 --- a/tui/tui.go +++ b/tui/tui.go @@ -110,7 +110,7 @@ func CreateUI( output: output, askBeforeDelete: true, showItemCount: false, - remover: remove.RemoveItemFromDir, + remover: remove.ItemFromDir, emptier: remove.EmptyFileFromDir, exec: Execute, linkedItems: make(fs.HardLinkedItems, 10), @@ -224,7 +224,7 @@ func (ui *UI) SetChangeCwdFn(fn func(string) error) { // SetDeleteInParallel sets the flag to delete files in parallel func (ui *UI) SetDeleteInParallel() { - ui.remover = remove.RemoveItemFromDirParallel + ui.remover = remove.ItemFromDirParallel } // StartUILoop starts tview application diff --git a/tui/tui_test.go b/tui/tui_test.go index 33a600d0d..a3a7503b0 100644 --- a/tui/tui_test.go +++ b/tui/tui_test.go @@ -358,7 +358,7 @@ func TestDeleteSelectedInBackground(t *testing.T) { defer fin() ui := getAnalyzedPathMockedApp(t, true, true, false) - ui.remover = testanalyze.RemoveItemFromDirWithSleep + ui.remover = testanalyze.ItemFromDirWithSleep ui.done = make(chan struct{}) ui.SetDeleteInBackground() @@ -382,7 +382,7 @@ func TestDeleteSelectedInBackgroundAndParallel(t *testing.T) { defer fin() ui := getAnalyzedPathMockedApp(t, true, true, false) - ui.remover = testanalyze.RemoveItemFromDirWithSleep + ui.remover = testanalyze.ItemFromDirWithSleep ui.done = make(chan struct{}) ui.SetDeleteInBackground() ui.SetDeleteInParallel() @@ -485,7 +485,7 @@ func TestDeleteSelectedWithErr(t *testing.T) { defer fin() ui := getAnalyzedPathMockedApp(t, false, true, false) - ui.remover = testanalyze.RemoveItemFromDirWithErr + ui.remover = testanalyze.ItemFromDirWithErr assert.Equal(t, 1, ui.table.GetRowCount()) @@ -509,7 +509,7 @@ func TestDeleteSelectedInBackgroundWithErr(t *testing.T) { ui := getAnalyzedPathMockedApp(t, false, true, false) ui.SetDeleteInBackground() - ui.remover = testanalyze.RemoveItemFromDirWithSleepAndErr + ui.remover = testanalyze.ItemFromDirWithSleepAndErr assert.Equal(t, 1, ui.table.GetRowCount()) @@ -540,7 +540,7 @@ func TestDeleteMarkedWithErr(t *testing.T) { defer fin() ui := getAnalyzedPathMockedApp(t, false, true, false) - ui.remover = testanalyze.RemoveItemFromDirWithErr + ui.remover = testanalyze.ItemFromDirWithErr assert.Equal(t, 1, ui.table.GetRowCount()) @@ -652,7 +652,7 @@ func TestDeleteMarkedInBackgroundWithErr(t *testing.T) { ui := getAnalyzedPathMockedApp(t, false, true, false) ui.SetDeleteInBackground() - ui.remover = testanalyze.RemoveItemFromDirWithErr + ui.remover = testanalyze.ItemFromDirWithErr assert.Equal(t, 1, ui.table.GetRowCount()) From 11c7345d885108156e42cfc4e38a7e8bea0e2919 Mon Sep 17 00:00:00 2001 From: Daniel Milde Date: Tue, 30 Apr 2024 16:22:00 +0200 Subject: [PATCH 17/24] chore: nolints --- tui/keys.go | 1 + tui/mouse.go | 1 + tui/show.go | 1 + 3 files changed, 3 insertions(+) diff --git a/tui/keys.go b/tui/keys.go index c28ceb5fb..5891ba2ac 100644 --- a/tui/keys.go +++ b/tui/keys.go @@ -200,6 +200,7 @@ func (ui *UI) handleFiltering(key *tcell.EventKey) *tcell.EventKey { return key } +// nolint: funlen // Why: there's a lot of options to handle func (ui *UI) handleMainActions(key *tcell.EventKey) *tcell.EventKey { switch key.Rune() { case 'd': diff --git a/tui/mouse.go b/tui/mouse.go index 991c62e64..75f8f1d0e 100644 --- a/tui/mouse.go +++ b/tui/mouse.go @@ -19,6 +19,7 @@ func (ui *UI) onMouse(event *tcell.EventMouse, action tview.MouseAction) (*tcell return nil, action } + // nolint: exhaustive // Why: we don't need to handle all mouse events switch action { case tview.MouseLeftDoubleClick: row, column := ui.table.GetSelection() diff --git a/tui/show.go b/tui/show.go index b8709fdac..f8b1026e3 100644 --- a/tui/show.go +++ b/tui/show.go @@ -42,6 +42,7 @@ Sort by (twice toggles asc/desc): [::b]C [white:black:-]Sort by file count (asc/desc) [::b]M [white:black:-]Sort by mtime (asc/desc)` +// nolint: funlen // Why: complex function func (ui *UI) showDir() { var ( totalUsage int64 From e5d043fd56545eeb6a052c23d8115315b90ba0bf Mon Sep 17 00:00:00 2001 From: Daniel Milde Date: Tue, 30 Apr 2024 16:22:18 +0200 Subject: [PATCH 18/24] refactor: re-assign --- report/export.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/report/export.go b/report/export.go index 9f0ee96b8..c0c4964b5 100644 --- a/report/export.go +++ b/report/export.go @@ -140,7 +140,7 @@ func (ui *UI) exportDir(dir fs.Item, waitWritten *sync.WaitGroup) error { buff.Write([]byte(strconv.FormatInt(time.Now().Unix(), 10))) buff.Write([]byte("},\n")) - if err = dir.EncodeJSON(&buff, true); err != nil { + if err := dir.EncodeJSON(&buff, true); err != nil { return err } if _, err = buff.Write([]byte("]\n")); err != nil { From 310b4fdd27e6ffb473353ea8c3377a37b20644ee Mon Sep 17 00:00:00 2001 From: Daniel Milde Date: Tue, 30 Apr 2024 16:59:32 +0200 Subject: [PATCH 19/24] ci: bump golangci-lint version --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5b70e96c2..d01c53972 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -20,7 +20,7 @@ jobs: - name: Run linters uses: golangci/golangci-lint-action@v4 with: - version: v1.45 + version: v1.57.2 test: strategy: From 6c911e840b16eeb658308781e29786e936406fab Mon Sep 17 00:00:00 2001 From: Daniel Milde Date: Tue, 30 Apr 2024 18:26:45 +0200 Subject: [PATCH 20/24] ci: bump golangci-lint-action --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d01c53972..c70bf21af 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -18,7 +18,7 @@ jobs: - name: Checkout code uses: actions/checkout@v4 - name: Run linters - uses: golangci/golangci-lint-action@v4 + uses: golangci/golangci-lint-action@v5 with: version: v1.57.2 From 9e0806217dce7ef1b5b2211adea618a212986d2d Mon Sep 17 00:00:00 2001 From: Daniel Milde Date: Thu, 2 May 2024 10:28:35 +0200 Subject: [PATCH 21/24] refactor: single case switch --- pkg/analyze/dir_linux-openbsd.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/analyze/dir_linux-openbsd.go b/pkg/analyze/dir_linux-openbsd.go index c14a340b7..9f8346ae8 100644 --- a/pkg/analyze/dir_linux-openbsd.go +++ b/pkg/analyze/dir_linux-openbsd.go @@ -12,8 +12,7 @@ import ( const devBSize = 512 func setPlatformSpecificAttrs(file *File, f os.FileInfo) { - switch stat := f.Sys().(type) { - case *syscall.Stat_t: + if stat, ok := f.Sys().(*syscall.Stat_t); ok { file.Usage = stat.Blocks * devBSize file.Mtime = time.Unix(int64(stat.Mtim.Sec), int64(stat.Mtim.Nsec)) From e750fc58cd1fb98368a838ec06fa4e67630a981c Mon Sep 17 00:00:00 2001 From: Daniel Milde Date: Thu, 2 May 2024 10:28:51 +0200 Subject: [PATCH 22/24] fix: error wrap --- pkg/device/dev_linux.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/device/dev_linux.go b/pkg/device/dev_linux.go index b6920d876..98b59fe8b 100644 --- a/pkg/device/dev_linux.go +++ b/pkg/device/dev_linux.go @@ -28,7 +28,7 @@ func (t LinuxDevicesInfoGetter) GetMounts() (Devices, error) { devices, err := readMountsFile(file) if err != nil { if cerr := file.Close(); cerr != nil { - return nil, fmt.Errorf("%w; %s", err, cerr) + return nil, fmt.Errorf("%w; %w", err, cerr) } return nil, err } From 6dbe7fe0e049a35aabf512cfbbcd8eec0b00a203 Mon Sep 17 00:00:00 2001 From: Daniel Milde Date: Thu, 2 May 2024 10:28:58 +0200 Subject: [PATCH 23/24] chore: nolint --- pkg/device/dev_linux_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/device/dev_linux_test.go b/pkg/device/dev_linux_test.go index d43e39b9f..72e09087d 100644 --- a/pkg/device/dev_linux_test.go +++ b/pkg/device/dev_linux_test.go @@ -48,6 +48,7 @@ rootpool/home /home zfs rw,nodev,relatime,xattr,posixacl 0 0 } func TestNfsMountsShown(t *testing.T) { + // nolint: lll // Why: Test data mounts, _ := readMountsFile(strings.NewReader(`host1:/dir1/ /mnt/dir1 nfs4 rw,nosuid,nodev,noatime,nodiratime,vers=4.2,rsize=1048576,wsize=1048576,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=192.168.1.1,fsc,local_lock=none,addr=192.168.1.2 0 0 host2:/dir2/ /mnt/dir2 nfs rw,relatime,vers=3,rsize=524288,wsize=524288,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,mountaddr=192.168.1.3,mountvers=3,mountport=38081,mountproto=udp,fsc,local_lock=none,addr=192.168.1.4 0 0`)) @@ -59,6 +60,7 @@ host2:/dir2/ /mnt/dir2 nfs rw,relatime,vers=3,rsize=524288,wsize=524288,namlen=2 } func TestMountsWithSpaces(t *testing.T) { + // nolint: lll // Why: Test data mounts, _ := readMountsFile(strings.NewReader(`host1:/dir1/ /mnt/dir\040with\040spaces nfs4 rw,nosuid,nodev,noatime,nodiratime,vers=4.2,rsize=1048576,wsize=1048576,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=192.168.1.1,fsc,local_lock=none,addr=192.168.1.2 0 0 host2:/dir2/ /mnt/dir2 nfs rw,relatime,vers=3,rsize=524288,wsize=524288,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,mountaddr=192.168.1.3,mountvers=3,mountport=38081,mountproto=udp,fsc,local_lock=none,addr=192.168.1.4 0 0`)) From e887e2cf64ba8754ca020a55a48f1d5f2aa35a38 Mon Sep 17 00:00:00 2001 From: Daniel Milde Date: Thu, 2 May 2024 10:38:09 +0200 Subject: [PATCH 24/24] fix: combine errors --- pkg/device/dev_linux.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/device/dev_linux.go b/pkg/device/dev_linux.go index 98b59fe8b..116b01162 100644 --- a/pkg/device/dev_linux.go +++ b/pkg/device/dev_linux.go @@ -28,7 +28,7 @@ func (t LinuxDevicesInfoGetter) GetMounts() (Devices, error) { devices, err := readMountsFile(file) if err != nil { if cerr := file.Close(); cerr != nil { - return nil, fmt.Errorf("%w; %w", err, cerr) + return nil, fmt.Errorf("%w; %s", err, cerr.Error()) } return nil, err }