-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
206 lines (170 loc) · 4.68 KB
/
plugin.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Package plasmactldependencies implements a dependencies launchr plugin
package plasmactldependencies
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"github.com/launchrctl/launchr"
"github.com/spf13/cobra"
"github.com/skilld-labs/plasmactl-bump/v2/pkg/sync"
)
func init() {
launchr.RegisterPlugin(&Plugin{})
}
// Plugin is [launchr.Plugin] providing dependencies search action.
type Plugin struct {
}
// PluginInfo implements [launchr.Plugin] interface.
func (p *Plugin) PluginInfo() launchr.PluginInfo {
return launchr.PluginInfo{
Weight: 20,
}
}
// CobraAddCommands implements [launchr.CobraPlugin] interface to provide bump functionality.
func (p *Plugin) CobraAddCommands(rootCmd *launchr.Command) error {
var source string
var depCmd = &cobra.Command{
Use: "dependencies",
Short: "Shows dependencies and dependent resources of selected resource",
Aliases: []string{"deps"},
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
RunE: func(cmd *launchr.Command, args []string) error {
// Don't show usage help on a runtime error.
cmd.SilenceUsage = true
if _, err := os.Stat(source); os.IsNotExist(err) {
launchr.Term().Warning().Printfln("%s doesn't exist, fallback to current dir", source)
source = "."
} else {
launchr.Term().Info().Printfln("Selected source is %s", source)
}
showPaths, err := cmd.Flags().GetBool("mrn")
if err != nil {
return err
}
showTree, err := cmd.Flags().GetBool("tree")
if err != nil {
return err
}
depth, err := cmd.Flags().GetInt8("depth")
if err != nil {
return err
}
if depth == 0 {
return fmt.Errorf("depth value should not be zero")
}
return dependencies(args[0], source, !showPaths, showTree, depth)
},
}
depCmd.Flags().StringVar(&source, "source", ".compose/build", "Resources source dir")
depCmd.Flags().Bool("mrn", false, "Show MRN instead of paths")
depCmd.Flags().Bool("tree", false, "Show dependencies in tree-like output")
depCmd.Flags().Int8("depth", 99, "Limit recursion lookup depth")
depCmd.SetArgs([]string{"target"})
rootCmd.AddCommand(depCmd)
return nil
}
func isMachineResourceName(target string) bool {
list := strings.Split(target, "__")
return len(list) == 3
}
func convertTarget(source, target string) (string, error) {
r := sync.BuildResourceFromPath(target, source)
if r == nil {
return "", fmt.Errorf("not valid resource %q", target)
}
return r.GetName(), nil
}
func convertToPath(mrn string) string {
parts := strings.Split(mrn, "__")
return filepath.Join(parts[0], parts[1], "roles", parts[2])
}
func dependencies(target, source string, toPath, showTree bool, depth int8) error {
searchMrn := target
if !isMachineResourceName(searchMrn) {
converted, err := convertTarget(source, target)
if err != nil {
return err
}
searchMrn = converted
}
var header string
if toPath {
header = convertToPath(searchMrn)
} else {
header = searchMrn
}
// @TODO move inventory into dependencies?
inv, err := sync.NewInventory(source)
if err != nil {
return err
}
parents := inv.GetRequiredByResources(searchMrn, depth)
if len(parents) > 0 {
launchr.Term().Info().Println("Dependent resources:")
if showTree {
var parentsTree forwardTree = inv.GetRequiredByMap()
parentsTree.print(header, "", 1, depth, searchMrn, toPath)
} else {
printList(parents, toPath)
}
}
children := inv.GetDependsOnResources(searchMrn, depth)
if len(children) > 0 {
launchr.Term().Info().Println("Dependencies:")
if showTree {
var childrenTree forwardTree = inv.GetDependsOnMap()
childrenTree.print(header, "", 1, depth, searchMrn, toPath)
} else {
printList(children, toPath)
}
}
return nil
}
func printList(items map[string]bool, toPath bool) {
keys := make([]string, 0, len(items))
for k := range items {
keys = append(keys, k)
}
sort.Strings(keys)
for _, item := range keys {
res := item
if toPath {
res = convertToPath(res)
}
launchr.Term().Print(res + "\n")
}
}
type forwardTree map[string]*sync.OrderedMap[bool]
func (t forwardTree) print(header, indent string, depth, limit int8, parent string, toPath bool) {
if indent == "" {
launchr.Term().Printfln(header)
}
if depth == limit {
return
}
children, ok := t[parent]
if !ok {
return
}
keys := children.Keys()
sort.Strings(keys)
for i, node := range keys {
isLast := i == len(keys)-1
var newIndent, edge string
if isLast {
newIndent = indent + " "
edge = "└── "
} else {
newIndent = indent + "│ "
edge = "├── "
}
value := node
if toPath {
value = convertToPath(value)
}
launchr.Term().Printfln(indent + edge + value)
t.print("", newIndent, depth+1, limit, node, toPath)
}
}