-
Notifications
You must be signed in to change notification settings - Fork 0
/
filelister.go
153 lines (137 loc) · 3.11 KB
/
filelister.go
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
)
type FileTree struct {
ModifiedTime time.Time
IsLink bool
IsDir bool
LinksTo string
Size int64
Name string
path string
Children []*FileTree
}
func GetTree(filePath string) *FileTree {
info, err := os.Lstat(filePath)
if err != nil {
fmt.Println("Error getting stats on file:", filePath, "\nError:", err)
os.Exit(2)
}
f := &FileTree{
path: filePath,
ModifiedTime: info.ModTime(),
Size: info.Size(),
IsDir: info.IsDir(),
Name: info.Name(),
IsLink: (info.Mode()&os.ModeSymlink != 0),
}
if f.IsLink {
link, err := os.Readlink(filePath)
if err != nil {
fmt.Println("Error reading link for file:", f.Name)
os.Exit(2)
}
linkPath, err := filepath.Abs(filepath.Join(filepath.Dir(f.path), link))
if err != nil {
fmt.Println("Error getting link path:\n", err)
}
f.LinksTo = linkPath
}
return f
}
func (f *FileTree) GetChildren(recursive bool) {
if f.IsDir && !f.IsLink {
children, err := ioutil.ReadDir(f.path)
if err != nil {
fmt.Println("Error reading directory:", f.path)
os.Exit(2)
}
var childPath string
for _, child := range children {
childPath = filepath.Join(f.path, child.Name())
f.Children = append(f.Children, GetTree(childPath))
}
if recursive {
for _, childInfo := range f.Children {
childInfo.GetChildren(recursive)
}
}
}
}
func (f *FileTree) ToText() string {
var buf bytes.Buffer
f.writeText(0, &buf)
return buf.String()
}
func (f *FileTree) writeText(depth int, buf *bytes.Buffer) {
prefix := strings.Repeat(" ", depth)
var suffix string
if f.IsLink {
suffix = "* (" + f.LinksTo + ")"
} else if f.IsDir {
suffix = "/"
}
name := f.Name
if depth == 0 {
name = f.path
}
buf.WriteString(prefix + name + suffix + "\n")
for _, child := range f.Children {
child.writeText(depth+1, buf)
}
}
func (f *FileTree) ToJson() string {
b, err := json.MarshalIndent(f.Children, "", " ")
if err != nil {
fmt.Println("Problem serializing to JSON:\n", err)
}
return string(b)
}
func (f *FileTree) ToYaml() string {
b, err := yaml.Marshal(f.Children)
if err != nil {
fmt.Println("Error converting to yaml:\n", err)
}
return string(b)
}
func main() {
// Specify/Parse/Validate CLI args
pth := flag.String("path", "", "path to folder")
recursive := flag.Bool("recursive", false, "list files recursively")
output := flag.String("output", "text", "<json|yaml|text>")
flag.Parse()
if *pth == "" {
fmt.Println("Must specify path.")
os.Exit(1)
}
if *output != "yaml" && *output != "json" && *output != "text" {
fmt.Println("Invalid output format: ", *output)
os.Exit(2)
}
// Build file tree structure
tree := GetTree(*pth)
tree.GetChildren(*recursive)
// Display Result
var toPrint string
switch *output {
case "json":
toPrint = tree.ToJson()
case "text":
toPrint = tree.ToText()
case "yaml":
toPrint = tree.ToYaml()
default:
// Never reached because of validation above
}
fmt.Println(toPrint)
}