forked from forcedotcom/heroku-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopic.go
143 lines (127 loc) · 3.7 KB
/
topic.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
package main
import "sort"
// Topic represents a CLI topic.
// For example, in the command `heroku apps:create` the topic would be `apps`.
type Topic struct {
Name string `json:"name"`
Description string `json:"description"`
Hidden bool `json:"hidden"`
Namespace *Namespace `json:"namespace"`
Commands []*Command
}
func (t *Topic) String() string {
if t.Namespace == nil || t.Namespace.Name == getDefaultNamespace() {
return t.Name
}
return t.Namespace.Name + ":" + t.Name
}
// Topics are a list of topics
type Topics []*Topic
// ByName returns a topic in the set matching the name.
func (topics Topics) ByName(name string) *Topic {
for _, topic := range topics {
if topic.Name == name {
return topic
}
}
return nil
}
// Concat joins 2 topic sets together
func (topics Topics) Concat(more Topics) Topics {
for _, topic := range more {
if topics.ByName(topic.Name) == nil {
topics = append(topics, topic)
}
}
return topics
}
func (topics Topics) Len() int {
return len(topics)
}
func (topics Topics) Less(i, j int) bool {
return topics[i].Name < topics[j].Name
}
func (topics Topics) Swap(i, j int) {
topics[i], topics[j] = topics[j], topics[i]
}
// AllTopics gets all go/core/user topics
func AllTopics() Topics {
topics := CLITopics
topics = topics.Concat(CorePlugins.Topics())
topics = topics.Concat(UserPlugins.Topics())
return topics
}
// NonHidden returns a set of topics that are not hidden
func (topics Topics) NonHidden() Topics {
to := make(Topics, 0, len(topics))
for _, topic := range topics {
if !topic.Hidden {
to = append(to, topic)
}
}
return to
}
// Namespaces returns a set of namespace from the set of topics
func (topics Topics) Namespaces() Namespaces {
to := make(Namespaces, 0, len(topics))
unique := make(map[string]bool)
for _, topic := range topics {
if topic.Namespace != nil && !unique[topic.Namespace.Name] {
unique[topic.Namespace.Name] = true
to = append(to, topic.Namespace)
}
}
return to
}
// Namespace returns a set of topics that are part of the cli namespace
func (topics Topics) Namespace(name string) Topics {
to := make(Topics, 0, len(topics))
for _, topic := range topics {
matchedNoNamespace := topic.Namespace == nil && name == ""
matchedNamespace := topic.Namespace != nil && topic.Namespace.Name == name
matchedDefault := topic.Namespace != nil && topic.Namespace.Name == getDefaultNamespace() && name == ""
if matchedNoNamespace || matchedNamespace || matchedDefault {
to = append(to, topic)
}
}
return to
}
// NamespaceAndTopicDescriptions returns a map of namespace and namespaceless
// topic descriptions
// i.e. it will group all topics by namespace except for topics that don't have a
// namespace
func (topics Topics) NamespaceAndTopicDescriptions() map[string]string {
to := make(map[string]string)
for _, topic := range topics {
if topic.Namespace == nil || topic.Namespace.Name == getDefaultNamespace() {
if to[topic.Name] == "" {
to[topic.Name] = topic.Description
}
} else if desc, ok := to[topic.Namespace.Name]; ok || desc == "" {
to[topic.Namespace.Name] = topic.Namespace.Description
}
}
// Check for namespaces to be loaded
for _, namespace := range AllNamespaces() {
if desc, ok := to[namespace.Name]; ok || desc == "" {
to[namespace.Name] = namespace.Description
}
}
return to
}
// Sort sorts the set
func (topics Topics) Sort() Topics {
sort.Sort(topics)
return topics
}
// Commands returns all the commands of all the topics
func (topics Topics) Commands() Commands {
commands := Commands{}
for _, topic := range topics {
for _, command := range topic.Commands {
command.Topic = topic.Name
commands = append(commands, command)
}
}
return commands
}