-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.go
More file actions
54 lines (45 loc) · 972 Bytes
/
helpers.go
File metadata and controls
54 lines (45 loc) · 972 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
)
func isHidden(path string) bool {
return strings.HasPrefix(filepath.Base(path), ".")
}
func validatePath(path string) error {
info, err := os.Stat(path)
if err != nil {
return err
}
if !info.IsDir() {
return fmt.Errorf("%s is not a directory", path)
}
return nil
}
func generateTree(path string, prefix string) (string, error) {
files, err := os.ReadDir(path)
if err != nil {
return "", err
}
sort.Slice(files, func(i, j int) bool {
return files[i].Name() < files[j].Name()
})
list := ""
for _, file := range files {
if isHidden(file.Name()) || file.Name() == "node_modules" || file.Name() == "README.md" {
continue
}
list += fmt.Sprintf("%s- %s\n", prefix, file.Name())
if file.IsDir() {
subList, err := generateTree(filepath.Join(path, file.Name()), prefix+" ")
if err != nil {
return "", err
}
list += subList
}
}
return list, nil
}