/
ls.go
71 lines (57 loc) 路 1.48 KB
/
ls.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
package cmd
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/gabrie30/ghorg/colorlog"
"github.com/spf13/cobra"
)
var lsCmd = &cobra.Command{
Use: "ls [dir]",
Short: "List contents of your ghorg home or ghorg directories",
Long: `If no dir is specified it will list contents of GHORG_ABSOLUTE_PATH_TO_CLONE_TO`,
Run: lsFunc,
}
func lsFunc(cmd *cobra.Command, argz []string) {
if len(argz) == 0 {
listGhorgHome()
}
if len(argz) >= 1 {
for _, arg := range argz {
listGhorgDir(arg)
}
}
}
func listGhorgHome() {
path := os.Getenv("GHORG_ABSOLUTE_PATH_TO_CLONE_TO")
files, err := ioutil.ReadDir(path)
if err != nil {
colorlog.PrintError("No clones found. Please clone some and try again.")
}
for _, f := range files {
if f.IsDir() {
colorlog.PrintInfo(path + f.Name())
}
}
}
func listGhorgDir(arg string) {
path := os.Getenv("GHORG_ABSOLUTE_PATH_TO_CLONE_TO") + arg
files, err := ioutil.ReadDir(path)
if err != nil {
// ghorg natively uses underscores in folder names, but a user can specify an output dir with underscores
// so first try what the user types if not then try replace
arg = strings.ReplaceAll(arg, "-", "_")
path = os.Getenv("GHORG_ABSOLUTE_PATH_TO_CLONE_TO") + arg
}
files, err = ioutil.ReadDir(path)
if err != nil {
colorlog.PrintError("No clones found. Please clone some and try again.")
}
for _, f := range files {
if f.IsDir() {
str := filepath.Join(path, f.Name())
colorlog.PrintSubtleInfo(str)
}
}
}