-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
cmd_tree.go
133 lines (104 loc) · 2.34 KB
/
cmd_tree.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
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"strings"
)
// Structure for our options and state.
type treeCommand struct {
// show only directories?
directories bool
// show all files?
all bool
}
// Arguments adds per-command args to the object.
func (t *treeCommand) Arguments(f *flag.FlagSet) {
f.BoolVar(&t.directories, "d", false, "Show only directories.")
f.BoolVar(&t.all, "a", false, "Show all files, including dotfiles.")
}
// Info returns the name of this subcommand.
func (t *treeCommand) Info() (string, string) {
return "tree", `Show filesystem contents as a tree.
Details:
This is a minimal reimplementation of the standard 'tree' command, it
supports showing a directory tree.
Usage:
$ sysbox tree /etc/
To show only directory entries:
$ sysbox tree -d /opt
If there were any errors encountered then the return-code will be 1, otherwise 0.`
}
// Execute is invoked if the user specifies `tree` as the subcommand.
func (t *treeCommand) Execute(args []string) int {
//
// Starting directory defaults to the current working directory
//
start := "."
//
// But can be changed
//
if len(args) > 0 {
start = args[0]
}
type Entry struct {
name string
error string
directory bool
}
//
// Keep track of directory entries here.
//
entries := []*Entry{}
//
// Find the contents
//
filepath.Walk(start,
func(path string, info os.FileInfo, err error) error {
// Null info? That probably means that the
// destination we're trying to walk doesn't exist.
if info == nil {
return nil
}
entry := &Entry{name: path}
if err == nil {
switch mode := info.Mode(); {
case mode.IsDir():
entry.directory = true
}
} else {
entry.error = err.Error()
}
entries = append(entries, entry)
return nil
})
//
// Did we hit an error?
//
error := false
//
// Show the entries
//
for _, ent := range entries {
// showing only directories? Then skip this
// entry unless it is a directory
if t.directories && !ent.directory {
continue
}
// skip dotfiles by default
if (strings.Contains(ent.name, "/.") || strings.HasPrefix(ent.name, ".")) && !t.all {
continue
}
if ent.error != "" {
fmt.Printf("%s - %s\n", ent.name, ent.error)
error = true
continue
}
fmt.Printf("%s\n", ent.name)
}
if error {
return 1
}
return 0
}