Skip to content

Commit cb8fcaa

Browse files
committed
Added -g,--groups option to list resources by tags
1 parent 2c12e7d commit cb8fcaa

File tree

1 file changed

+76
-7
lines changed

1 file changed

+76
-7
lines changed

cmd/list.go

Lines changed: 76 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ import (
1212
)
1313

1414
type listOptions struct {
15-
all bool
16-
tags []string
15+
all bool
16+
groups bool
17+
tags []string
1718
}
1819

1920
func (o listOptions) tagMatch(tags []string) bool {
@@ -40,6 +41,9 @@ func newListCommand(opts *dobiOptions) *cobra.Command {
4041
flags.BoolVarP(
4142
&listOpts.all, "all", "a", false,
4243
"List all resources, including those without descriptions")
44+
flags.BoolVarP(
45+
&listOpts.groups, "groups", "g", false,
46+
"List resources sorted by their matching tags")
4347
flags.StringSliceVarP(
4448
&listOpts.tags, "tags", "t", nil,
4549
"List tasks matching the tag")
@@ -52,19 +56,54 @@ func runList(opts *dobiOptions, listOpts listOptions) error {
5256
return err
5357
}
5458

55-
resources := filterResources(conf, listOpts)
56-
descriptions := getDescriptions(resources)
59+
tags := getTags(conf.Resources)
60+
var descriptions []string
61+
if listOpts.groups {
62+
resources := filterResourcesTags(conf, listOpts)
63+
descriptions = getDescriptionsByTag(resources)
64+
} else {
65+
resources := filterResources(conf, listOpts)
66+
descriptions = getDescriptions(resources)
67+
}
68+
5769
if len(descriptions) == 0 {
5870
logging.Log.Warn("No resources found. Try --all or --tags.")
5971
return nil
6072
}
61-
62-
tags := getTags(conf.Resources)
63-
6473
fmt.Print(format(descriptions, tags))
6574
return nil
6675
}
6776

77+
func filterResourcesTags(conf *config.Config, listOpts listOptions) []resourceGroup {
78+
tags := []resourceGroup{}
79+
if listOpts.all {
80+
tags = append(tags, resourceGroup{tag: "none"})
81+
}
82+
for _, name := range conf.Sorted() {
83+
res := conf.Resources[name]
84+
if len(res.CategoryTags()) > 0 {
85+
for _, tagname := range res.CategoryTags() {
86+
currentGroupIndex := 0
87+
if i, found := findGroup(tags, tagname); found {
88+
currentGroupIndex = i
89+
} else {
90+
tags = append(tags, resourceGroup{
91+
tag: tagname,
92+
})
93+
currentGroupIndex = len(tags) - 1
94+
}
95+
tags[currentGroupIndex].resources = append(tags[currentGroupIndex].resources, namedResource{name: name, resource: res})
96+
}
97+
} else {
98+
if listOpts.all {
99+
tags[0].resources = append(tags[0].resources, namedResource{name: name, resource: res})
100+
}
101+
}
102+
}
103+
104+
return tags
105+
}
106+
68107
func filterResources(conf *config.Config, listOpts listOptions) []namedResource {
69108
resources := []namedResource{}
70109
for _, name := range conf.Sorted() {
@@ -76,6 +115,11 @@ func filterResources(conf *config.Config, listOpts listOptions) []namedResource
76115
return resources
77116
}
78117

118+
type resourceGroup struct {
119+
tag string
120+
resources []namedResource
121+
}
122+
79123
type namedResource struct {
80124
name string
81125
resource config.Resource
@@ -105,6 +149,15 @@ func getDescriptions(resources []namedResource) []string {
105149
return lines
106150
}
107151

152+
func getDescriptionsByTag(resources []resourceGroup) []string {
153+
lines := []string{}
154+
for _, tag := range resources {
155+
descriptions := getDescriptions(tag.resources)
156+
lines = append(lines, formatTags(tag.tag, descriptions))
157+
}
158+
return lines
159+
}
160+
108161
func getTags(resources map[string]config.Resource) []string {
109162
mapped := make(map[string]struct{})
110163
for _, res := range resources {
@@ -120,6 +173,15 @@ func getTags(resources map[string]config.Resource) []string {
120173
return tags
121174
}
122175

176+
func findGroup(slice []resourceGroup, tag string) (int, bool) {
177+
for i, item := range slice {
178+
if item.tag == tag {
179+
return i, true
180+
}
181+
}
182+
return -1, false
183+
}
184+
123185
func format(descriptions []string, tags []string) string {
124186
resources := strings.Join(descriptions, "\n ")
125187

@@ -129,3 +191,10 @@ func format(descriptions []string, tags []string) string {
129191
}
130192
return msg
131193
}
194+
195+
func formatTags(tag string, descriptions []string) string {
196+
msg := fmt.Sprintf("Tag: %s\n", tag)
197+
resources := strings.Join(descriptions, "\n ")
198+
msg += fmt.Sprintf(" %s\n", resources)
199+
return msg
200+
}

0 commit comments

Comments
 (0)