-
Notifications
You must be signed in to change notification settings - Fork 215
/
help.go
161 lines (145 loc) · 3.86 KB
/
help.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
package main
import (
"fmt"
"path/filepath"
"strconv"
"strings"
"github.com/texttheater/golang-levenshtein/levenshtein"
)
func init() {
CLITopics = append(CLITopics, &Topic{
Name: "help",
Hidden: true,
Commands: Commands{
&Command{
Hidden: true,
Run: func(ctx *Context) {
Args = []string{"heroku", "help"}
help()
},
},
},
})
}
// HELP is "help"
const HELP = "help"
func help() {
cmd := Args[1]
switch Args[1] {
case "help", "--help", "-h":
if len(Args) >= 3 {
cmd = Args[2]
} else {
cmd = ""
}
}
topic := AllTopics().ByName(strings.SplitN(cmd, ":", 2)[0])
command := AllCommands().Find(cmd)
switch {
case topic == nil:
helpShowTopics()
case command == nil:
helpShowTopic(topic)
default:
helpShowCommand(topic, command)
}
}
func helpShowTopics() {
Printf("Usage: heroku COMMAND [--app APP] [command-specific-options]\n\n")
Printf("Help topics, type \"heroku help TOPIC\" for more details:\n\n")
topics := AllTopics().NonHidden().Sort()
longestTopic := 0
for _, topic := range topics {
if len(topic.Name) > longestTopic {
longestTopic = len(topic.Name)
}
}
for _, topic := range topics {
Printf(" heroku %-"+strconv.Itoa(longestTopic+1)+"s# %s\n", topic.Name, topic.Description)
}
Println()
Exit(0)
}
func helpShowTopic(topic *Topic) {
Printf("Usage: heroku %s:COMMAND [--app APP] [command-specific-options]\n\n", topic.Name)
printTopicCommandsHelp(topic)
Println()
Exit(0)
}
func helpShowCommand(topic *Topic, command *Command) {
Printf("Usage: heroku %s\n\n", CommandUsage(command))
Println(command.buildFullHelp())
if command.Command == "" || command.Default {
printTopicCommandsHelp(topic)
}
Println()
Exit(0)
}
func printTopicCommandsHelp(topic *Topic) {
topicCommands := Commands{}
for _, cur := range AllCommands().NonHidden() {
if topic != nil && cur.Topic == topic.Name {
topicCommands = append(topicCommands, cur)
}
}
topicCommands.loadUsages()
if len(topicCommands) > 0 {
Printf("\nCommands for %s, type \"heroku help %s:COMMAND\" for more details:\n\n", topic.Name, topic.Name)
for _, command := range topicCommands.Sort() {
Printf(" heroku %-30s # %s\n", command.Usage, command.Description)
}
}
}
func helpInvalidCommand() {
checkIfKnownTopic(Args[1])
var closest string
currentAnalyticsCommand.Valid = false
guess, distance := findClosestCommand(AllCommands(), Args[1])
if len(Args[1]) > 2 || distance < 2 {
newcmd := strings.TrimSpace(fmt.Sprintf("heroku %s %s", guess, strings.Join(Args[2:], " ")))
WarnIfError(saveJSON(&Guess{guess.String(), Args[2:]}, guessPath()))
closest = fmt.Sprintf("Perhaps you meant %s?\nRun %s to run %s.\n", yellow(guess.String()), cyan("heroku _"), cyan(newcmd))
}
Error(fmt.Sprintf(`%s is not a heroku command.
%sRun %s for a list of available commands.
`, yellow(Args[1]), closest, cyan("heroku help")))
Exit(127)
}
func checkIfKnownTopic(cmd string) {
knownTopics := map[string]string{
"redis": "heroku-redis",
"kafka": "heroku-kafka",
}
topic := strings.Split(cmd, ":")[0]
plugin := knownTopics[topic]
if plugin != "" {
ExitWithMessage("Use %s commands by installing the %s plugin.\n%s", topic, yellow(plugin), cyan("heroku plugins:install "+plugin))
}
}
func findClosestCommand(from Commands, a string) (*Command, int) {
var top *Command
var val int
for _, b := range from {
if cur := stringDistance(a, b.String()); cur < val || top == nil {
top = b
val = cur
}
}
return top, val
}
func stringDistance(a, b string) int {
return levenshtein.DistanceForStrings([]rune(a), []rune(b), levenshtein.DefaultOptions)
}
// Guess is used with `heroku _`
type Guess struct {
Guess string `json:"guess"`
Args []string `json:"args"`
}
func guessPath() string {
return filepath.Join(CacheHome, "guess.json")
}
func loadLastCommandGuess() (guess *Guess) {
err := readJSON(&guess, guessPath())
LogIfError(err)
return guess
}