-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
160 lines (141 loc) · 3.97 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
package main
import (
"errors"
"fmt"
"os"
"reflect"
"strings"
"code.cloudfoundry.org/cli/cf/cmd"
"code.cloudfoundry.org/cli/commands"
"code.cloudfoundry.org/cli/commands/v2"
"code.cloudfoundry.org/cli/utils/configv3"
"code.cloudfoundry.org/cli/utils/panichandler"
"code.cloudfoundry.org/cli/utils/ui"
"github.com/jessevdk/go-flags"
)
type UI interface {
DisplayError(err ui.TranslatableError)
DisplayErrorMessage(err string, keys ...map[string]interface{})
}
var ErrFailed = errors.New("command failed")
func main() {
defer panichandler.HandlePanic()
parse(os.Args[1:])
}
func parse(args []string) {
parser := flags.NewParser(&v2.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:
_, found := reflect.TypeOf(v2.Commands).FieldByNameFunc(
func(fieldName string) bool {
field, _ := reflect.TypeOf(v2.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 {
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 {
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.ErrUnknownCommand:
cmd.Main(os.Getenv("CF_TRACE"), os.Args)
case flags.ErrCommandRequired:
if v2.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 {
fmt.Fprintf(os.Stderr, "Unexpected error: %s\n", err.Error())
os.Exit(1)
}
}
func isCommand(s string) bool {
_, found := reflect.TypeOf(v2.Commands).FieldByNameFunc(
func(fieldName string) bool {
field, _ := reflect.TypeOf(v2.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, err := configv3.LoadConfig()
if err != nil {
return err
}
defer configv3.WriteConfig(cfConfig)
if extendedCmd, ok := cmd.(commands.ExtendedCommander); ok {
commandUI, err := ui.NewUI(cfConfig)
if err != nil {
return err
}
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
}
if e, ok := err.(ui.TranslatableError); ok {
commandUI.DisplayError(e)
} else {
commandUI.DisplayErrorMessage(err.Error())
}
return ErrFailed
}