-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
216 lines (188 loc) · 5.55 KB
/
main.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"
"fmt"
"os"
"reflect"
"strings"
"code.cloudfoundry.org/cli/cf/cmd"
"code.cloudfoundry.org/cli/command"
"code.cloudfoundry.org/cli/command/common"
"code.cloudfoundry.org/cli/command/translatableerror"
"code.cloudfoundry.org/cli/command/v2"
"code.cloudfoundry.org/cli/util/configv3"
"code.cloudfoundry.org/cli/util/panichandler"
"code.cloudfoundry.org/cli/util/ui"
"github.com/jessevdk/go-flags"
log "github.com/sirupsen/logrus"
)
type UI interface {
DisplayError(err error)
DisplayWarning(template string, templateValues ...map[string]interface{})
}
type DisplayUsage interface {
DisplayUsage()
}
var ErrFailed = errors.New("command failed")
var ParseErr = errors.New("incorrect type for arg")
func main() {
defer panichandler.HandlePanic()
parse(os.Args[1:])
}
func parse(args []string) {
parser := flags.NewParser(&common.Commands, flags.HelpFlag)
parser.CommandHandler = executionWrapper
extraArgs, err := parser.ParseArgs(args)
if err == nil {
return
}
if flagErr, ok := err.(*flags.Error); ok {
switch flagErr.Type {
case flags.ErrHelp, flags.ErrUnknownFlag, flags.ErrExpectedArgument, flags.ErrInvalidChoice:
_, found := reflect.TypeOf(common.Commands).FieldByNameFunc(
func(fieldName string) bool {
field, _ := reflect.TypeOf(common.Commands).FieldByName(fieldName)
return parser.Active != nil && parser.Active.Name == field.Tag.Get("command")
},
)
if found && flagErr.Type == flags.ErrUnknownFlag && parser.Active.Name == "set-env" {
newArgs := []string{}
for _, arg := range args {
if arg[0] == '-' {
newArgs = append(newArgs, fmt.Sprintf("%s%s", v2.WorkAroundPrefix, arg))
} else {
newArgs = append(newArgs, arg)
}
}
parse(newArgs)
return
}
if flagErr.Type == flags.ErrUnknownFlag || flagErr.Type == flags.ErrExpectedArgument || flagErr.Type == flags.ErrInvalidChoice {
fmt.Fprintf(os.Stderr, "Incorrect Usage: %s\n\n", flagErr.Error())
}
if found {
parse([]string{"help", parser.Active.Name})
} else {
switch len(extraArgs) {
case 0:
parse([]string{"help"})
case 1:
if !isOption(extraArgs[0]) || (len(args) > 1 && extraArgs[0] == "-a") {
parse([]string{"help", extraArgs[0]})
} else {
parse([]string{"help"})
}
default:
if isCommand(extraArgs[0]) {
parse([]string{"help", extraArgs[0]})
} else {
parse(extraArgs[1:])
}
}
}
if flagErr.Type == flags.ErrUnknownFlag || flagErr.Type == flags.ErrExpectedArgument || flagErr.Type == flags.ErrInvalidChoice {
os.Exit(1)
}
case flags.ErrRequired:
fmt.Fprintf(os.Stderr, "Incorrect Usage: %s\n\n", flagErr.Error())
parse([]string{"help", args[0]})
os.Exit(1)
case flags.ErrMarshal:
errMessage := strings.Split(flagErr.Message, ":")
fmt.Fprintf(os.Stderr, "Incorrect Usage: %s\n\n", errMessage[0])
parse([]string{"help", args[0]})
os.Exit(1)
case flags.ErrUnknownCommand:
cmd.Main(os.Getenv("CF_TRACE"), os.Args)
case flags.ErrCommandRequired:
if common.Commands.VerboseOrVersion {
parse([]string{"version"})
} else {
parse([]string{"help"})
}
default:
fmt.Fprintf(os.Stderr, "Unexpected flag error\ntype: %s\nmessage: %s\n", flagErr.Type, flagErr.Error())
}
} else if err == ErrFailed {
os.Exit(1)
} else if err == ParseErr {
fmt.Println()
parse([]string{"help", args[0]})
os.Exit(1)
} else {
fmt.Fprintf(os.Stderr, "Unexpected error: %s\n", err.Error())
os.Exit(1)
}
}
func isCommand(s string) bool {
_, found := reflect.TypeOf(common.Commands).FieldByNameFunc(
func(fieldName string) bool {
field, _ := reflect.TypeOf(common.Commands).FieldByName(fieldName)
return s == field.Tag.Get("command") || s == field.Tag.Get("alias")
})
return found
}
func isOption(s string) bool {
return strings.HasPrefix(s, "-")
}
func executionWrapper(cmd flags.Commander, args []string) error {
cfConfig, configErr := configv3.LoadConfig(configv3.FlagOverride{
Verbose: common.Commands.VerboseOrVersion,
})
if configErr != nil {
if _, ok := configErr.(translatableerror.EmptyConfigError); !ok {
return configErr
}
}
commandUI, err := ui.NewUI(cfConfig)
if err != nil {
return err
}
// TODO: when the line in the old code under `cf` which calls
// configv3.LoadConfig() is finally removed, then we should replace the code
// path above with the following:
//
// var configErrTemplate string
// if configErr != nil {
// if ce, ok := configErr.(translatableerror.EmptyConfigError); ok {
// configErrTemplate = ce.Error()
// } else {
// return configErr
// }
// }
// commandUI, err := ui.NewUI(cfConfig)
// if err != nil {
// return err
// }
// if configErr != nil {
// commandUI.DisplayWarning(configErrTemplate, map[string]interface{}{
// "FilePath": configv3.ConfigFilePath(),
// })
// }
defer func() {
configWriteErr := configv3.WriteConfig(cfConfig)
if configWriteErr != nil {
fmt.Fprintf(os.Stderr, "Error writing config: %s", configWriteErr.Error())
}
}()
if extendedCmd, ok := cmd.(command.ExtendedCommander); ok {
log.SetOutput(os.Stderr)
log.SetLevel(log.Level(cfConfig.LogLevel()))
err = extendedCmd.Setup(cfConfig, commandUI)
if err != nil {
return handleError(err, commandUI)
}
return handleError(extendedCmd.Execute(args), commandUI)
}
return fmt.Errorf("command does not conform to ExtendedCommander")
}
func handleError(err error, commandUI UI) error {
if err == nil {
return nil
}
commandUI.DisplayError(err)
if _, ok := err.(DisplayUsage); ok {
return ParseErr
}
return ErrFailed
}