-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcmd_status.go
85 lines (71 loc) · 1.6 KB
/
cmd_status.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
package main
import (
"os"
"path/filepath"
"sort"
"strings"
"github.com/spf13/cobra"
)
var cmdStatus = &cobra.Command{
Use: "status",
Short: "displays the status of all modules",
Run: func(*cobra.Command, []string) {
state := currentState(globalOpts.Base, globalOpts.Target)
keys := make([]string, 0, len(state))
for name := range state {
keys = append(keys, name)
}
sort.Slice(keys, func(i, j int) bool {
return keys[i] < keys[j]
})
for _, name := range keys {
installed := state[name]
if !installed {
msg("[ ---- ] %v\n", name)
} else {
msg("[ INST ] %v\n", name)
}
}
},
}
func init() {
cmdRoot.AddCommand(cmdStatus)
}
// State contains the state whether a module is installed or not.
type State map[string]bool
func firstdir(dir string) string {
dirs := strings.Split(dir, string(filepath.Separator))
return dirs[0]
}
func installedModules(base, target string) State {
state := make(State)
walkOurSymlinks(base, target, func(filename, dir string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
rel, err := filepath.Rel(base, dir)
if err != nil {
return err
}
module := firstdir(rel)
state[module] = true
return nil
})
return state
}
func allModules(base string) (modules []string) {
for _, dir := range subdirs(base) {
module := filepath.Base(dir)
modules = append(modules, module)
}
return modules
}
func currentState(base, target string) State {
state := installedModules(base, target)
for _, module := range allModules(base) {
if _, ok := state[module]; !ok {
state[module] = false
}
}
return state
}