Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: lint #351

Merged
merged 2 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions cmd/gdu/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import (
"fmt"
"io"
"io/fs"
"net/http"
"net/http/pprof"
"os"
"path/filepath"
"runtime"
"strings"

"net/http"
"net/http/pprof"

log "github.com/sirupsen/logrus"

"github.com/dundee/gdu/v5/build"
Expand Down Expand Up @@ -215,7 +214,7 @@ func (a *App) createUI() (UI, error) {
if a.Flags.OutputFile == "-" {
output = os.Stdout
} else {
output, err = os.OpenFile(a.Flags.OutputFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
output, err = os.OpenFile(a.Flags.OutputFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)
if err != nil {
return nil, fmt.Errorf("opening output file: %w", err)
}
Expand Down Expand Up @@ -268,7 +267,7 @@ func (a *App) createUI() (UI, error) {
ui.SetDefaultSorting(a.Flags.Sorting.By, a.Flags.Sorting.Order)
})
}
if a.Flags.ChangeCwd != false {
if a.Flags.ChangeCwd {
opts = append(opts, func(ui *tui.UI) {
ui.SetChangeCwdFn(os.Chdir)
})
Expand Down Expand Up @@ -357,7 +356,7 @@ func (a *App) runAction(ui UI, path string) error {
if a.Flags.InputFile == "-" {
input = os.Stdin
} else {
input, err = os.OpenFile(a.Flags.InputFile, os.O_RDONLY, 0600)
input, err = os.OpenFile(a.Flags.InputFile, os.O_RDONLY, 0o600)
if err != nil {
return fmt.Errorf("opening input file: %w", err)
}
Expand Down
10 changes: 6 additions & 4 deletions cmd/gdu/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ import (
"github.com/dundee/gdu/v5/pkg/device"
)

var af *app.Flags
var configErr error
var (
af *app.Flags
configErr error
)

var rootCmd = &cobra.Command{
Use: "gdu [directory_to_scan]",
Expand Down Expand Up @@ -136,7 +138,7 @@ func runE(command *cobra.Command, args []string) error {
if af.CfgFile == "" {
setDefaultConfigFilePath()
}
err = os.WriteFile(af.CfgFile, data, 0600)
err = os.WriteFile(af.CfgFile, data, 0o600)
if err != nil {
return fmt.Errorf("Error writing config file %s: %w", af.CfgFile, err)
}
Expand All @@ -150,7 +152,7 @@ func runE(command *cobra.Command, args []string) error {
if af.LogFile == "-" {
f = os.Stdout
} else {
f, err = os.OpenFile(af.LogFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
f, err = os.OpenFile(af.LogFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)
if err != nil {
return fmt.Errorf("opening log file: %w", err)
}
Expand Down
1 change: 0 additions & 1 deletion internal/common/ignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ func (ui *UI) SetIgnoreFromFile(ignoreFile string) error {
log.Printf("Reading ignoring dir patterns from file '%s'", ignoreFile)

file, err := os.Open(ignoreFile)

if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/common/ignore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestIgnoreByPattern(t *testing.T) {
}

func TestIgnoreFromFile(t *testing.T) {
file, err := os.OpenFile("ignore", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
file, err := os.OpenFile("ignore", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/testdir/test_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ func CreateTestDir() func() {
if err := os.MkdirAll("test_dir/nested/subnested", os.ModePerm); err != nil {
panic(err)
}
if err := os.WriteFile("test_dir/nested/subnested/file", []byte("hello"), 0600); err != nil {
if err := os.WriteFile("test_dir/nested/subnested/file", []byte("hello"), 0o600); err != nil {
panic(err)
}
if err := os.WriteFile("test_dir/nested/file2", []byte("go"), 0600); err != nil {
if err := os.WriteFile("test_dir/nested/file2", []byte("go"), 0o600); err != nil {
panic(err)
}
return func() {
Expand Down
4 changes: 2 additions & 2 deletions pkg/analyze/dir_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestErr(t *testing.T) {
err := os.Chmod("test_dir/nested", 0)
assert.Nil(t, err)
defer func() {
err = os.Chmod("test_dir/nested", 0755)
err = os.Chmod("test_dir/nested", 0o755)
assert.Nil(t, err)
}()

Expand All @@ -45,7 +45,7 @@ func TestSeqErr(t *testing.T) {
err := os.Chmod("test_dir/nested", 0)
assert.Nil(t, err)
defer func() {
err = os.Chmod("test_dir/nested", 0755)
err = os.Chmod("test_dir/nested", 0o755)
assert.Nil(t, err)
}()

Expand Down
6 changes: 3 additions & 3 deletions pkg/analyze/dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func TestFlags(t *testing.T) {
fin := testdir.CreateTestDir()
defer fin()

err := os.Mkdir("test_dir/empty", 0644)
err := os.Mkdir("test_dir/empty", 0o644)
assert.Nil(t, err)

err = os.Symlink("test_dir/nested/file2", "test_dir/nested/file3")
Expand Down Expand Up @@ -137,7 +137,7 @@ func TestFollowSymlink(t *testing.T) {
fin := testdir.CreateTestDir()
defer fin()

err := os.Mkdir("test_dir/empty", 0644)
err := os.Mkdir("test_dir/empty", 0o644)
assert.Nil(t, err)

err = os.Symlink("./file2", "test_dir/nested/file3")
Expand Down Expand Up @@ -169,7 +169,7 @@ func TestBrokenSymlinkSkipped(t *testing.T) {
fin := testdir.CreateTestDir()
defer fin()

err := os.Mkdir("test_dir/empty", 0644)
err := os.Mkdir("test_dir/empty", 0o644)
assert.Nil(t, err)

err = os.Symlink("xxx", "test_dir/nested/file3")
Expand Down
12 changes: 6 additions & 6 deletions pkg/analyze/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ func manageMemoryUsage(c <-chan struct{}) {
}

/*
Try to balance performance and memory consumption.
Try to balance performance and memory consumption.

When less memory is used by gdu than the total free memory of the host,
Garbage Collection is disabled during the analysis phase at all.
When less memory is used by gdu than the total free memory of the host,
Garbage Collection is disabled during the analysis phase at all.

Otherwise GC is enabled.
The more memory is used and the less memory is free,
the more often will the GC happen.
Otherwise GC is enabled.
The more memory is used and the less memory is free,
the more often will the GC happen.
*/
func rebalanceGC(disabledGC *bool) {
memStats := runtime.MemStats{}
Expand Down
6 changes: 3 additions & 3 deletions pkg/analyze/sequential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func TestFlagsSeq(t *testing.T) {
fin := testdir.CreateTestDir()
defer fin()

err := os.Mkdir("test_dir/empty", 0644)
err := os.Mkdir("test_dir/empty", 0o644)
assert.Nil(t, err)

err = os.Symlink("test_dir/nested/file2", "test_dir/nested/file3")
Expand Down Expand Up @@ -137,7 +137,7 @@ func TestFollowSymlinkSeq(t *testing.T) {
fin := testdir.CreateTestDir()
defer fin()

err := os.Mkdir("test_dir/empty", 0644)
err := os.Mkdir("test_dir/empty", 0o644)
assert.Nil(t, err)

err = os.Symlink("./file2", "test_dir/nested/file3")
Expand Down Expand Up @@ -169,7 +169,7 @@ func TestBrokenSymlinkSkippedSeq(t *testing.T) {
fin := testdir.CreateTestDir()
defer fin()

err := os.Mkdir("test_dir/empty", 0644)
err := os.Mkdir("test_dir/empty", 0o644)
assert.Nil(t, err)

err = os.Symlink("xxx", "test_dir/nested/file3")
Expand Down
22 changes: 21 additions & 1 deletion pkg/analyze/stored_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func TestParentDirGetNamePanics(t *testing.T) {
dir := &ParentDir{}
dir.GetName()
}

func TestParentDirGetFlagPanics(t *testing.T) {
defer func() {
if r := recover(); r != nil {
Expand All @@ -126,6 +127,7 @@ func TestParentDirGetFlagPanics(t *testing.T) {
dir := &ParentDir{}
dir.GetFlag()
}

func TestParentDirIsDirPanics(t *testing.T) {
defer func() {
if r := recover(); r != nil {
Expand All @@ -135,6 +137,7 @@ func TestParentDirIsDirPanics(t *testing.T) {
dir := &ParentDir{}
dir.IsDir()
}

func TestParentDirGetSizePanics(t *testing.T) {
defer func() {
if r := recover(); r != nil {
Expand All @@ -144,6 +147,7 @@ func TestParentDirGetSizePanics(t *testing.T) {
dir := &ParentDir{}
dir.GetSize()
}

func TestParentDirGetTypePanics(t *testing.T) {
defer func() {
if r := recover(); r != nil {
Expand All @@ -153,6 +157,7 @@ func TestParentDirGetTypePanics(t *testing.T) {
dir := &ParentDir{}
dir.GetType()
}

func TestParentDirGetUsagePanics(t *testing.T) {
defer func() {
if r := recover(); r != nil {
Expand All @@ -162,6 +167,7 @@ func TestParentDirGetUsagePanics(t *testing.T) {
dir := &ParentDir{}
dir.GetUsage()
}

func TestParentDirGetMtimePanics(t *testing.T) {
defer func() {
if r := recover(); r != nil {
Expand All @@ -171,6 +177,7 @@ func TestParentDirGetMtimePanics(t *testing.T) {
dir := &ParentDir{}
dir.GetMtime()
}

func TestParentDirGetItemCountPanics(t *testing.T) {
defer func() {
if r := recover(); r != nil {
Expand All @@ -180,6 +187,7 @@ func TestParentDirGetItemCountPanics(t *testing.T) {
dir := &ParentDir{}
dir.GetItemCount()
}

func TestParentDirGetParentPanics(t *testing.T) {
defer func() {
if r := recover(); r != nil {
Expand All @@ -189,6 +197,7 @@ func TestParentDirGetParentPanics(t *testing.T) {
dir := &ParentDir{}
dir.GetParent()
}

func TestParentDirSetParentPanics(t *testing.T) {
defer func() {
if r := recover(); r != nil {
Expand All @@ -198,6 +207,7 @@ func TestParentDirSetParentPanics(t *testing.T) {
dir := &ParentDir{}
dir.SetParent(nil)
}

func TestParentDirGetMultiLinkedInodePanics(t *testing.T) {
defer func() {
if r := recover(); r != nil {
Expand All @@ -207,15 +217,18 @@ func TestParentDirGetMultiLinkedInodePanics(t *testing.T) {
dir := &ParentDir{}
dir.GetMultiLinkedInode()
}

func TestParentDirEncodeJSONPanics(t *testing.T) {
defer func() {
if r := recover(); r != nil {
assert.Equal(t, "must not be called", r)
}
}()
dir := &ParentDir{}
dir.EncodeJSON(nil, false)
err := dir.EncodeJSON(nil, false)
assert.NoError(t, err)
}

func TestParentDirUpdateStatsPanics(t *testing.T) {
defer func() {
if r := recover(); r != nil {
Expand All @@ -225,6 +238,7 @@ func TestParentDirUpdateStatsPanics(t *testing.T) {
dir := &ParentDir{}
dir.UpdateStats(nil)
}

func TestParentDirAddFilePanics(t *testing.T) {
defer func() {
if r := recover(); r != nil {
Expand All @@ -234,6 +248,7 @@ func TestParentDirAddFilePanics(t *testing.T) {
dir := &ParentDir{}
dir.AddFile(nil)
}

func TestParentDirGetFilesPanics(t *testing.T) {
defer func() {
if r := recover(); r != nil {
Expand All @@ -243,6 +258,7 @@ func TestParentDirGetFilesPanics(t *testing.T) {
dir := &ParentDir{}
dir.GetFiles()
}

func TestParentDirGetFilesLockedPanics(t *testing.T) {
defer func() {
if r := recover(); r != nil {
Expand All @@ -252,6 +268,7 @@ func TestParentDirGetFilesLockedPanics(t *testing.T) {
dir := &ParentDir{}
dir.GetFilesLocked()
}

func TestParentDirRLockPanics(t *testing.T) {
defer func() {
if r := recover(); r != nil {
Expand All @@ -261,6 +278,7 @@ func TestParentDirRLockPanics(t *testing.T) {
dir := &ParentDir{}
dir.RLock()
}

func TestParentDirSetFilesPanics(t *testing.T) {
defer func() {
if r := recover(); r != nil {
Expand All @@ -270,6 +288,7 @@ func TestParentDirSetFilesPanics(t *testing.T) {
dir := &ParentDir{}
dir.SetFiles(nil)
}

func TestParentDirRemoveFilePanics(t *testing.T) {
defer func() {
if r := recover(); r != nil {
Expand All @@ -279,6 +298,7 @@ func TestParentDirRemoveFilePanics(t *testing.T) {
dir := &ParentDir{}
dir.RemoveFile(nil)
}

func TestParentDirGetItemStatsPanics(t *testing.T) {
defer func() {
if r := recover(); r != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/device/dev_freebsd_darwin_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func readMountOutput(rdr io.Reader) (Devices, error) {
for scanner.Scan() {
line := scanner.Text()

re := regexp.MustCompile("^(.*) on (/.*) \\(([^)]+)\\)$")
re := regexp.MustCompile(`^(.*) on (/.*) \(([^)]+)\)$`)
parts := re.FindAllStringSubmatch(line, -1)

if len(parts) < 1 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/remove/parallel_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestRemoveItemFromDirParallelWithErr(t *testing.T) {
err := os.Chmod("test_dir/nested", 0)
assert.Nil(t, err)
defer func() {
err = os.Chmod("test_dir/nested", 0755)
err = os.Chmod("test_dir/nested", 0o755)
assert.Nil(t, err)
}()

Expand Down Expand Up @@ -49,7 +49,7 @@ func TestRemoveItemFromDirParallelWithErr2(t *testing.T) {
err := os.Chmod("test_dir/nested/subnested", 0)
assert.Nil(t, err)
defer func() {
err = os.Chmod("test_dir/nested/subnested", 0755)
err = os.Chmod("test_dir/nested/subnested", 0o755)
assert.Nil(t, err)
}()

Expand Down
2 changes: 1 addition & 1 deletion pkg/remove/remove_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestRemoveFileWithErr(t *testing.T) {
err := os.Chmod("test_dir/nested", 0)
assert.Nil(t, err)
defer func() {
err = os.Chmod("test_dir/nested", 0755)
err = os.Chmod("test_dir/nested", 0o755)
assert.Nil(t, err)
}()

Expand Down
Loading
Loading