forked from heroku/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
216 lines (207 loc) · 6.19 KB
/
context.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"errors"
"os"
"strings"
)
// Context is a struct that is passed to a Command's Run function.
// It contains information about the user's command arguments
// as well as Heroku information like the auth token and app name (if requested).
type Context struct {
Topic *Topic `json:"topic"`
Command *Command `json:"command"`
App string `json:"app"`
Org string `json:"org,omitempty"`
Args interface{} `json:"args"`
Flags map[string]interface{} `json:"flags"`
Cwd string `json:"cwd"`
HerokuDir string `json:"herokuDir"`
Debug bool `json:"debug"`
DebugHeaders bool `json:"debugHeaders"`
Dev bool `json:"dev"`
SupportsColor bool `json:"supportsColor"`
Version string `json:"version"`
APIToken string `json:"apiToken"`
APIHost string `json:"apiHost"` // deprecated in favor of apiUrl
APIURL string `json:"apiUrl"`
GitHost string `json:"gitHost"`
HTTPGitHost string `json:"httpGitHost"`
Auth struct {
Password string `json:"password"`
} `json:"auth"`
}
var errHelp = errors.New(HELP)
// BuildContext builds a context object based on a command and args
func BuildContext(command *Command, args []string) (*Context, error) {
var err error
if command == nil {
return nil, errHelp
}
ctx := &Context{}
ctx.Command = command
if ctx.Command.VariableArgs {
ctx.Args, ctx.Flags, err = parseVarArgs(ctx.Command, args[2:])
} else {
ctx.Args, ctx.Flags, err = parseArgs(ctx.Command, args[2:])
}
if err != nil {
return nil, err
}
if ctx.Command.NeedsApp || ctx.Command.WantsApp {
var err error
ctx.App, err = app(ctx.Flags)
if err != nil && ctx.Command.NeedsApp {
ExitWithMessage(err.Error())
}
if ctx.App == "" && ctx.Command.NeedsApp {
ctx.Command.appNeededErr()
}
}
if ctx.Command.NeedsOrg || ctx.Command.WantsOrg {
if org, ok := ctx.Flags["org"].(string); ok {
ctx.Org = org
} else {
ctx.Org = os.Getenv("HEROKU_ORGANIZATION")
}
if ctx.Org == "" && ctx.Command.NeedsOrg {
ExitWithMessage("No org specified.\nRun this command with --org or by setting HEROKU_ORGANIZATION")
}
}
if ctx.Command.NeedsAuth {
ctx.APIToken = auth()
ctx.Auth.Password = ctx.APIToken
}
ctx.Cwd, _ = os.Getwd()
ctx.HerokuDir = CacheHome
ctx.Debug = Debugging
ctx.DebugHeaders = DebuggingHeaders
ctx.Version = version()
ctx.SupportsColor = supportsColor()
ctx.APIHost = apiHost()
ctx.APIURL = apiURL()
ctx.GitHost = gitHost()
ctx.HTTPGitHost = httpGitHost()
return ctx, nil
}
func parseVarArgs(command *Command, args []string) (result []string, flags map[string]interface{}, err error) {
result = make([]string, 0, len(args))
flags = map[string]interface{}{}
parseFlags := true
possibleFlags := []*Flag{}
populateFlagsFromEnvVars(command.Flags, flags)
for _, flag := range command.Flags {
f := flag
possibleFlags = append(possibleFlags, &f)
}
if command.NeedsApp || command.WantsApp {
possibleFlags = append(possibleFlags, AppFlag, RemoteFlag)
}
if command.NeedsOrg || command.WantsOrg {
possibleFlags = append(possibleFlags, OrgFlag)
}
warnAboutDuplicateFlags(possibleFlags)
for i := 0; i < len(args); i++ {
switch {
case parseFlags && (args[i] == "--"):
parseFlags = false
case parseFlags && (args[i] == "--help" || args[i] == "-h"):
return nil, nil, errHelp
case parseFlags && (args[i] == "--no-color"):
continue
case parseFlags && strings.HasPrefix(args[i], "-"):
flag, val, err := ParseFlag(args[i], possibleFlags)
if err != nil && strings.HasSuffix(err.Error(), "needs a value") {
i++
if len(args) == i {
ExitWithMessage(err.Error())
}
flag, val, err = ParseFlag(args[i-1]+"="+args[i], possibleFlags)
}
if flag != nil {
if flag.HasValue {
flags[flag.Name] = val
} else {
flags[flag.Name] = true
}
}
switch {
case err != nil:
ExitWithMessage(err.Error())
case flag == nil && command.VariableArgs:
result = append(result, args[i])
case flag == nil:
command.unexpectedFlagErr(args[i])
}
default:
result = append(result, args[i])
}
}
for _, flag := range command.Flags {
if flag.Required && flags[flag.Name] == nil {
ExitWithMessage("Required flag: %s", flag.String())
}
}
return result, flags, nil
}
func parseArgs(command *Command, args []string) (result map[string]string, flags map[string]interface{}, err error) {
result = map[string]string{}
args, flags, err = parseVarArgs(command, args)
if err != nil {
return nil, nil, err
}
if len(args) > len(command.Args) {
command.unexpectedArgumentsErr(args[len(command.Args):])
}
for i, arg := range args {
result[command.Args[i].Name] = arg
}
for _, arg := range command.Args {
if !arg.Optional && result[arg.Name] == "" {
ExitWithMessage("Missing argument: %s", strings.ToUpper(arg.Name))
}
}
return result, flags, nil
}
func app(flags map[string]interface{}) (string, error) {
if flags["app"] != nil {
return flags["app"].(string), nil
}
if flags["remote"] != nil {
app, err := appFromGitRemote(flags["remote"].(string))
if err != nil {
return "", err
}
return app, nil
}
if flags["confirm"] != nil {
return flags["confirm"].(string), nil
}
app := os.Getenv("HEROKU_APP")
if app != "" {
return app, nil
}
return appFromGitRemote(remoteFromGitConfig())
}
func populateFlagsFromEnvVars(flagDefinitons []Flag, flags map[string]interface{}) {
for _, flag := range flagDefinitons {
if strings.ToLower(flag.Name) == "user" && os.Getenv("HEROKU_USER") != "" {
flags[flag.Name] = os.Getenv("HEROKU_USER")
}
if strings.ToLower(flag.Name) == "force" && os.Getenv("HEROKU_FORCE") == "1" {
flags[flag.Name] = true
}
}
}
func warnAboutDuplicateFlags(flags []*Flag) {
for _, a := range flags {
for _, b := range flags {
if a == b {
continue
}
if (a.Char != "" && a.Char == b.Char) ||
(a.Name != "" && a.Name == b.Name) {
Errf("Flag conflict: %s conflicts with %s\n", a, b)
}
}
}
}