-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
77 lines (64 loc) · 1.29 KB
/
list.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
package main
import (
"fmt"
"time"
df "github.com/lorenzobotti/discordfs"
"github.com/spf13/cobra"
)
var listCmd = &cobra.Command{
Use: "list [path]",
Aliases: []string{"ls"},
Short: "List the files on the server",
Long: "List the files and folders on the server at the specified path",
Run: func(c *cobra.Command, args []string) {
listCmdArgs.folder = df.Root
if len(args) >= 1 {
listCmdArgs.folder = args[0]
}
listFiles()
},
}
var listCmdArgs struct {
color bool
metadata bool
folder string
}
func init() {
rootCmd.AddCommand(listCmd)
listCmd.Flags().BoolVarP(
&listCmdArgs.color,
"color",
"c",
true,
"color the folders' names",
)
listCmd.Flags().BoolVarP(&listCmdArgs.metadata,
"all",
"a",
false,
"print file metadata",
)
}
func listFiles() {
st := unsafeNewStorage()
files, err := st.ListFiles(listCmdArgs.folder)
cobra.CheckErr(err)
for _, file := range files {
if listCmdArgs.metadata {
if file.IsDir() {
fmt.Print("d ")
} else {
fmt.Print(" ")
}
fmt.Print(file.ModTime().Format(time.Stamp) + " ")
}
if file.IsDir() && listCmdArgs.color {
fmt.Println(ansiColorBlue(file.Name()))
} else {
fmt.Println(file.Name())
}
}
}
func ansiColorBlue(in string) string {
return "\x1b[34m" + in + "\x1b[0m"
}