-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
151 lines (121 loc) · 4.05 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
package main
import (
"fmt"
"os"
"runtime"
"strings"
"github.com/cloudfoundry/cli/cf"
"github.com/cloudfoundry/cli/cf/command_registry"
"github.com/cloudfoundry/cli/cf/configuration/plugin_config"
"github.com/cloudfoundry/cli/cf/help"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/cli/cf/panic_printer"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/cloudfoundry/cli/commands_loader"
"github.com/cloudfoundry/cli/plugin/rpc"
"github.com/simonleung8/flags"
)
var deps = command_registry.NewDependency()
var cmdRegistry = command_registry.Commands
func main() {
commands_loader.Load()
defer handlePanics(deps.TeePrinter)
defer deps.Config.Close()
//handles `cf` | `cf -h` || `cf -help`
if len(os.Args) == 1 || os.Args[1] == "--help" || os.Args[1] == "-help" ||
os.Args[1] == "--h" || os.Args[1] == "-h" {
help.ShowHelp(help.GetHelpTemplate())
os.Exit(0)
}
//handle `cf -v` for cf version
if len(os.Args) == 2 && (os.Args[1] == "-v" || os.Args[1] == "--version") {
deps.Ui.Say(os.Args[0] + " version " + cf.Version + "-" + cf.BuiltOnDate)
os.Exit(0)
}
//handle `cf --build`
if len(os.Args) == 2 && (os.Args[1] == "--build" || os.Args[1] == "-b") {
deps.Ui.Say(T("{{.CFName}} was built with Go version: {{.GoVersion}}",
map[string]interface{}{
"CFName": os.Args[0],
"GoVersion": runtime.Version(),
}))
os.Exit(0)
}
//handles `cf [COMMAND] -h ...`
//rearrage args to `cf help COMMAND` and let `command help` to print out usage
if requestHelp(os.Args[2:]) {
os.Args[2] = os.Args[1]
os.Args[1] = "help"
}
//run core command
cmd := os.Args[1]
if cmdRegistry.CommandExists(cmd) {
meta := cmdRegistry.FindCommand(os.Args[1]).MetaData()
fc := flags.NewFlagContext(meta.Flags)
fc.SkipFlagParsing(meta.SkipFlagParsing)
err := fc.Parse(os.Args[2:]...)
if err != nil {
deps.Ui.Failed("Incorrect Usage\n\n" + err.Error() + "\n\n" + cmdRegistry.CommandUsage(cmd))
}
cmdRegistry.SetCommand(cmdRegistry.FindCommand(cmd).SetDependency(deps, false))
cfCmd := cmdRegistry.FindCommand(cmd)
reqs, err := cfCmd.Requirements(requirements.NewFactory(deps.Ui, deps.Config, deps.RepoLocator), fc)
if err != nil {
deps.Ui.Failed(err.Error())
}
for _, r := range reqs {
if !r.Execute() {
os.Exit(1)
}
}
cfCmd.Execute(fc)
os.Exit(0)
}
//non core command, try plugin command
rpcService := newCliRpcServer(deps.TeePrinter, deps.TeePrinter)
pluginsConfig := plugin_config.NewPluginConfig(func(err error) { deps.Ui.Failed(fmt.Sprintf("Error read/writing plugin config: %s, ", err.Error())) })
pluginList := pluginsConfig.Plugins()
ran := rpc.RunMethodIfExists(rpcService, os.Args[1:], pluginList)
if !ran {
deps.Ui.Say("'" + os.Args[1] + T("' is not a registered command. See 'cf help'"))
os.Exit(1)
}
}
func handlePanics(printer terminal.Printer) {
panic_printer.UI = terminal.NewUI(os.Stdin, printer)
commandArgs := strings.Join(os.Args, " ")
stackTrace := generateBacktrace()
err := recover()
panic_printer.DisplayCrashDialog(err, commandArgs, stackTrace)
if err != nil {
os.Exit(1)
}
}
func generateBacktrace() string {
stackByteCount := 0
STACK_SIZE_LIMIT := 1024 * 1024
var bytes []byte
for stackSize := 1024; (stackByteCount == 0 || stackByteCount == stackSize) && stackSize < STACK_SIZE_LIMIT; stackSize = 2 * stackSize {
bytes = make([]byte, stackSize)
stackByteCount = runtime.Stack(bytes, true)
}
stackTrace := "\t" + strings.Replace(string(bytes), "\n", "\n\t", -1)
return stackTrace
}
func requestHelp(args []string) bool {
for _, v := range args {
if v == "-h" || v == "--help" || v == "--h" {
return true
}
}
return false
}
func newCliRpcServer(outputCapture terminal.OutputCapture, terminalOutputSwitch terminal.TerminalOutputSwitch) *rpc.CliRpcService {
cliServer, err := rpc.NewRpcService(outputCapture, terminalOutputSwitch, deps.Config, deps.RepoLocator, rpc.NewNonCodegangstaRunner())
if err != nil {
deps.Ui.Say(T("Error initializing RPC service: ") + err.Error())
os.Exit(1)
}
return cliServer
}