forked from heroku/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopic.go
88 lines (75 loc) · 1.83 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
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"`
Commands []*Command
}
func (t *Topic) String() string {
return 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
}
// 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
}