forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
134 lines (108 loc) · 3.47 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
package main
import (
"fmt"
"os"
"runtime"
"strings"
"time"
"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/manifest"
"github.com/cloudfoundry/cli/cf/net"
"github.com/cloudfoundry/cli/cf/panic_printer"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/cloudfoundry/cli/cf/trace"
"github.com/codegangsta/cli"
)
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)
warningsCollector := net.NewWarningsCollector(deps.termUI, gateways...)
warningsCollector.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() {
panic_printer.UI = terminal.NewUI(os.Stdin)
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
}