Skip to content

Commit 4449a78

Browse files
committed
refactor: DRY, but is it better?
1 parent f8e5385 commit 4449a78

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

cmd/subcommands/list.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,17 @@ import (
1515

1616
// Shared Utilities
1717

18-
func ListSubfolders(dir string) ([]string, error) {
19-
folderNames := []string{}
20-
18+
func ListSubfolders(dir string) (folderNames []string, err error) {
2119
files, err := os.ReadDir(dir)
2220
if err != nil {
23-
return folderNames, err
21+
return
2422
}
25-
2623
for _, file := range files {
2724
if file.IsDir() && !(strings.HasPrefix(file.Name(), ".")) {
2825
folderNames = append(folderNames, file.Name())
2926
}
3027
}
31-
return folderNames, nil
28+
return
3229
}
3330

3431
// Sort Helpers
@@ -66,7 +63,7 @@ func summarize(file ExtDirEntry) string {
6663
func calculateStats(dir string) (stats []ExtDirEntry, err error) {
6764
files, err := os.ReadDir(dir)
6865
if err != nil {
69-
return stats, err
66+
return
7067
}
7168

7269
for _, file := range files {
@@ -79,13 +76,13 @@ func calculateStats(dir string) (stats []ExtDirEntry, err error) {
7976
stats = append(stats, stat)
8077
}
8178
}
82-
return stats, nil
79+
return
8380
}
8481

8582
func getStats(syncDir string) (stats []ExtDirEntry, err error) {
8683
folderNames, err := ListSubfolders(syncDir)
8784
if err != nil {
88-
return stats, err
85+
return
8986
}
9087
for _, name := range folderNames {
9188
subStats, err := calculateStats(filepath.Join(syncDir, name))
@@ -94,7 +91,7 @@ func getStats(syncDir string) (stats []ExtDirEntry, err error) {
9491
}
9592
stats = append(stats, subStats...)
9693
}
97-
return stats, nil
94+
return
9895
}
9996

10097
func AttachList(cli *clir.Cli) {
@@ -112,13 +109,13 @@ func AttachList(cli *clir.Cli) {
112109
outputFormat := "text"
113110
listCmd.StringFlag("output", "Output format", &outputFormat)
114111

115-
listCmd.Action(func() error {
112+
listCmd.Action(func() (err error) {
116113
sortMethod := map[string]SortMethod{"name": sortFileName, "mod": sortFileModTime}[sortMethodStr]
117114
output := map[string]OutputFormat{"text": summarize}[outputFormat]
118115

119116
stats, err := getStats(syncDir)
120117
if err != nil {
121-
return err
118+
return
122119
}
123120
sortMethod(stats)
124121
if sortAsc {
@@ -127,6 +124,6 @@ func AttachList(cli *clir.Cli) {
127124
for _, s := range stats {
128125
fmt.Println(output(s))
129126
}
130-
return nil
127+
return
131128
})
132129
}

0 commit comments

Comments
 (0)