Skip to content

Commit

Permalink
refactor: gofumpted
Browse files Browse the repository at this point in the history
  • Loading branch information
dundee committed Apr 24, 2024
1 parent d167352 commit 2558cf7
Show file tree
Hide file tree
Showing 20 changed files with 137 additions and 116 deletions.
9 changes: 4 additions & 5 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 @@ -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
19 changes: 19 additions & 0 deletions 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,6 +217,7 @@ func TestParentDirGetMultiLinkedInodePanics(t *testing.T) {
dir := &ParentDir{}
dir.GetMultiLinkedInode()
}

func TestParentDirEncodeJSONPanics(t *testing.T) {
defer func() {
if r := recover(); r != nil {
Expand All @@ -217,6 +228,7 @@ func TestParentDirEncodeJSONPanics(t *testing.T) {
err := dir.EncodeJSON(nil, false)
assert.NoError(t, err)
}

func TestParentDirUpdateStatsPanics(t *testing.T) {
defer func() {
if r := recover(); r != nil {
Expand All @@ -226,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 @@ -235,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 @@ -244,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 @@ -253,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 @@ -262,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 @@ -271,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 @@ -280,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
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
4 changes: 2 additions & 2 deletions report/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestExportToFile(t *testing.T) {
fin := testdir.CreateTestDir()
defer fin()

reportOutput, err := os.OpenFile("output.json", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
reportOutput, err := os.OpenFile("output.json", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
assert.Nil(t, err)
defer func() {
os.Remove("output.json")
Expand All @@ -89,7 +89,7 @@ func TestExportToFile(t *testing.T) {
err = ui.StartUILoop()
assert.Nil(t, err)

reportOutput, err = os.OpenFile("output.json", os.O_RDONLY, 0644)
reportOutput, err = os.OpenFile("output.json", os.O_RDONLY, 0o644)
assert.Nil(t, err)
_, err = reportOutput.Seek(0, 0)
assert.Nil(t, err)
Expand Down
Loading

0 comments on commit 2558cf7

Please sign in to comment.