forked from heroku/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopic.go
59 lines (50 loc) · 1.31 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
package main
// 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"`
}
func (t *Topic) String() string {
return t.Name
}
// TopicSet is a slice of Topic structs with some helper methods.
type TopicSet []*Topic
// ByName returns a topic in the set matching the name.
func (topics TopicSet) ByName(name string) *Topic {
for _, topic := range topics {
if topic.Name == name {
return topic
}
}
return nil
}
// Commands returns all of the commands under the topic.
func (t *Topic) Commands() []*Command {
commands := make([]*Command, 0, len(cli.Commands))
for _, c := range cli.Commands {
if c.Topic == t.Name {
commands = append(commands, c)
}
}
return commands
}
// Merge will replace empty data on the topic with data from the passed topic.
func (t *Topic) Merge(other *Topic) {
if t.Name == "" {
t.Name = other.Name
}
if t.Description == "" {
t.Description = other.Description
}
}
func (topics TopicSet) Len() int {
return len(topics)
}
func (topics TopicSet) Less(i, j int) bool {
return topics[i].Name < topics[j].Name
}
func (topics TopicSet) Swap(i, j int) {
topics[i], topics[j] = topics[j], topics[i]
}