forked from heroku/cli
/
main.go
155 lines (143 loc) · 3.04 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
package main
import (
"errors"
"fmt"
"net/http"
"net/url"
"os"
"runtime"
"runtime/debug"
"strings"
"github.com/heroku/heroku-cli/Godeps/_workspace/src/github.com/stvp/rollbar"
)
// Version is the version of the v4 cli.
// This is set by a build flag in the `Rakefile`.
// If it is set to `dev` it will not autoupdate.
var Version = "dev"
// Channel is the git branch the code was compiled on.
// This is set by a build flag in the `Rakefile` based on the git branch.
var Channel = "?"
var cli = &Cli{}
// BuiltinPlugins are the core plugins that will be autoinstalled
var BuiltinPlugins = []string{
"heroku-apps",
"heroku-cli-addons",
"heroku-fork",
"heroku-git",
"heroku-local",
"heroku-pipelines",
"heroku-run",
"heroku-spaces",
"heroku-status",
}
func init() {
cli.Topics = TopicSet{
authTopic,
commandsTopic,
debugTopic,
loginTopic,
pluginsTopic,
twoFactorTopic,
twoFactorTopicAlias,
updateTopic,
versionTopic,
whichTopic,
}
cli.Commands = CommandSet{
authLoginCmd,
authTokenCmd,
commandsListCmd,
debugErrlogCmd,
loginCmd,
pluginsInstallCmd,
pluginsLinkCmd,
pluginsListCmd,
pluginsUninstallCmd,
twoFactorCmd,
twoFactorCmdAlias,
twoFactorDisableCmd,
twoFactorDisableCmdAlias,
twoFactorGenerateCmd,
twoFactorGenerateCmdAlias,
updateCmd,
versionCmd,
whichCmd,
whoamiCmd,
}
rollbar.Platform = "client"
rollbar.Token = "b40226d5e8a743cf963ca320f7be17bd"
rollbar.Environment = Channel
rollbar.ErrorWriter = nil
}
func main() {
defer handlePanic()
runtime.GOMAXPROCS(1) // more procs causes runtime: failed to create new OS thread on Ubuntu
ShowDebugInfo()
Update(Channel, "block")
SetupNode()
err := cli.Run(os.Args)
SetupBuiltinPlugins()
TriggerBackgroundUpdate()
if err == ErrHelp {
// Command wasn't found so load the plugins and try again
cli.LoadPlugins(GetPlugins())
err = cli.Run(os.Args)
}
if err == ErrHelp {
help()
}
if err != nil {
PrintError(err, false)
os.Exit(2)
}
}
func handlePanic() {
if rec := recover(); rec != nil {
err, ok := rec.(error)
if !ok {
err = errors.New(rec.(string))
}
Errln("ERROR:", err)
if Channel == "?" {
debug.PrintStack()
} else {
rollbar.Error(rollbar.ERR, err, rollbarFields()...)
rollbar.Wait()
}
Exit(1)
}
}
func rollbarFields() []*rollbar.Field {
var cmd string
if len(os.Args) > 1 {
cmd = os.Args[1]
}
return []*rollbar.Field{
{"Version", Version},
{"GOOS", runtime.GOOS},
{"GOARCH", runtime.GOARCH},
{"command", cmd},
}
}
// ShowDebugInfo prints debugging information if HEROKU_DEBUG=1
func ShowDebugInfo() {
if !isDebugging() {
return
}
info := []string{version(), binPath}
if len(os.Args) > 1 {
info = append(info, fmt.Sprintf("cmd: %s", os.Args[1]))
}
proxy := getProxy()
if proxy != nil {
info = append(info, fmt.Sprintf("proxy: %s", proxy))
}
Debugln(strings.Join(info, " "))
}
func getProxy() *url.URL {
req, err := http.NewRequest("GET", "https://api.heroku.com", nil)
PrintError(err, false)
proxy, err := http.ProxyFromEnvironment(req)
PrintError(err, false)
return proxy
}