forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
162 lines (131 loc) · 3.99 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
package main
import (
"fmt"
"os"
"runtime"
"strings"
"time"
"github.com/codegangsta/cli"
"github.com/cloudfoundry/cli/cf"
"github.com/cloudfoundry/cli/cf/api"
"github.com/cloudfoundry/cli/cf/app"
"github.com/cloudfoundry/cli/cf/command_factory"
"github.com/cloudfoundry/cli/cf/command_runner"
"github.com/cloudfoundry/cli/cf/configuration"
"github.com/cloudfoundry/cli/cf/errors"
"github.com/cloudfoundry/cli/cf/manifest"
"github.com/cloudfoundry/cli/cf/net"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/cloudfoundry/cli/cf/trace"
)
type cliDependencies struct {
termUI terminal.UI
configRepo configuration.Repository
manifestRepo manifest.ManifestRepository
apiRepoLocator api.RepositoryLocator
gateways map[string]net.Gateway
}
func setupDependencies() (deps *cliDependencies) {
deps = new(cliDependencies)
deps.termUI = terminal.NewUI(os.Stdin)
deps.manifestRepo = manifest.NewManifestDiskRepository()
deps.configRepo = configuration.NewRepositoryFromFilepath(configuration.DefaultFilePath(), func(err error) {
if err != nil {
deps.termUI.Failed(fmt.Sprintf("Config error: %s", err))
}
})
terminal.UserAskedForColors = deps.configRepo.ColorEnabled()
terminal.InitColorSupport()
if os.Getenv("CF_TRACE") != "" {
trace.Logger = trace.NewLogger(os.Getenv("CF_TRACE"))
} else {
trace.Logger = trace.NewLogger(deps.configRepo.Trace())
}
deps.gateways = map[string]net.Gateway{
"auth": net.NewUAAGateway(deps.configRepo),
"cloud-controller": net.NewCloudControllerGateway(deps.configRepo, time.Now),
"uaa": net.NewUAAGateway(deps.configRepo),
}
deps.apiRepoLocator = api.NewRepositoryLocator(deps.configRepo, deps.gateways)
return
}
func main() {
defer handlePanics()
deps := setupDependencies()
defer deps.configRepo.Close()
cmdFactory := command_factory.NewFactory(deps.termUI, deps.configRepo, deps.manifestRepo, deps.apiRepoLocator)
requirementsFactory := requirements.NewFactory(deps.termUI, deps.configRepo, deps.apiRepoLocator)
cmdRunner := command_runner.NewRunner(cmdFactory, requirementsFactory)
err := app.NewApp(cmdRunner, cmdFactory.CommandMetadatas()...).Run(os.Args)
if err != nil {
os.Exit(1)
}
gateways := gatewaySliceFromMap(deps.gateways)
net.NewWarningsCollector(deps.termUI, gateways...).PrintWarnings()
}
func gatewaySliceFromMap(gateway_map map[string]net.Gateway) []net.WarningProducer {
gateways := []net.WarningProducer{}
for _, gateway := range gateway_map {
gateways = append(gateways, gateway)
}
return gateways
}
func init() {
cli.CommandHelpTemplate = `NAME:
{{.Name}} - {{.Description}}
{{with .ShortName}}
ALIAS:
{{.}}
{{end}}
USAGE:
{{.Usage}}{{with .Flags}}
OPTIONS:
{{range .}} {{.}}
{{end}}{{else}}
{{end}}`
}
func handlePanics() {
err := recover()
if err != nil && err != terminal.QuietPanic {
switch err := err.(type) {
case errors.Exception:
if err.DisplayCrashDialog {
displayCrashDialog(err.Message)
} else {
fmt.Println(err.Message)
}
case error:
displayCrashDialog(err.Error())
case string:
displayCrashDialog(err)
default:
displayCrashDialog("An unexpected type of error")
}
}
if err != nil {
os.Exit(1)
}
}
func displayCrashDialog(errorMessage string) {
formattedString := `
Aww shucks.
Something completely unexpected happened. This is a bug in %s.
Please file this bug : https://github.com/cloudfoundry/cli/issues
Tell us that you ran this command:
%s
this error occurred:
%s
and this stack trace:
%s
`
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)
println(fmt.Sprintf(formattedString, cf.Name(), strings.Join(os.Args, " "), errorMessage, stackTrace))
}