forked from exercism/cli
/
troubleshoot.go
272 lines (236 loc) · 6.15 KB
/
troubleshoot.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package cmd
import (
"bytes"
"fmt"
"html/template"
"runtime"
"strings"
"sync"
"time"
"github.com/exercism/cli/cli"
"github.com/exercism/cli/config"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// fullAPIKey flag for troubleshoot command.
var fullAPIKey bool
// troubleshootCmd does a diagnostic self-check.
var troubleshootCmd = &cobra.Command{
Use: "troubleshoot",
Aliases: []string{"t", "debug"},
Short: "Troubleshoot does a diagnostic self-check.",
Long: `Provides output to help with troubleshooting.
If you're running into trouble, copy and paste the output from the troubleshoot
command into a GitHub issue so we can help figure out what's going on.
`,
RunE: func(cmd *cobra.Command, args []string) error {
cli.TimeoutInSeconds = cli.TimeoutInSeconds * 2
c := cli.New(Version)
cfg := config.NewConfig()
v := viper.New()
v.AddConfigPath(cfg.Dir)
v.SetConfigName("user")
v.SetConfigType("json")
// Ignore error. If the file doesn't exist, that is fine.
_ = v.ReadInConfig()
cfg.UserViperConfig = v
status := newStatus(c, cfg)
status.Censor = !fullAPIKey
s, err := status.check()
if err != nil {
return err
}
fmt.Printf("%s", s)
return nil
},
}
// Status represents the results of a CLI self test.
type Status struct {
Censor bool
Version versionStatus
System systemStatus
Configuration configurationStatus
APIReachability apiReachabilityStatus
cfg config.Config
cli *cli.CLI
}
type versionStatus struct {
Current string
Latest string
Status string
Error error
UpToDate bool
}
type systemStatus struct {
OS string
Architecture string
Build string
}
type configurationStatus struct {
Home string
Workspace string
Dir string
Token string
TokenURL string
}
type apiReachabilityStatus struct {
Services []*apiPing
}
type apiPing struct {
Service string
URL string
Status string
Latency time.Duration
}
// newStatus prepares a value to perform a diagnostic self-check.
func newStatus(cli *cli.CLI, cfg config.Config) Status {
status := Status{
cfg: cfg,
cli: cli,
}
return status
}
// check runs the CLI's diagnostic self-check.
func (status *Status) check() (string, error) {
status.Version = newVersionStatus(status.cli)
status.System = newSystemStatus()
status.Configuration = newConfigurationStatus(status)
status.APIReachability = newAPIReachabilityStatus(status.cfg)
return status.compile()
}
func (status *Status) compile() (string, error) {
t, err := template.New("self-test").Parse(tmplSelfTest)
if err != nil {
return "", err
}
var bb bytes.Buffer
t.Execute(&bb, status)
return bb.String(), nil
}
func newAPIReachabilityStatus(cfg config.Config) apiReachabilityStatus {
baseURL := cfg.UserViperConfig.GetString("apibaseurl")
if baseURL == "" {
baseURL = cfg.DefaultBaseURL
}
ar := apiReachabilityStatus{
Services: []*apiPing{
{Service: "GitHub", URL: "https://api.github.com"},
{Service: "Exercism", URL: fmt.Sprintf("%s/ping", baseURL)},
},
}
var wg sync.WaitGroup
wg.Add(len(ar.Services))
for _, service := range ar.Services {
go service.Call(&wg)
}
wg.Wait()
return ar
}
func newVersionStatus(c *cli.CLI) versionStatus {
vs := versionStatus{
Current: c.Version,
}
ok, err := c.IsUpToDate()
if err == nil {
vs.Latest = c.LatestRelease.Version()
} else {
vs.Error = fmt.Errorf("Error: %s", err)
}
vs.UpToDate = ok
return vs
}
func newSystemStatus() systemStatus {
ss := systemStatus{
OS: runtime.GOOS,
Architecture: runtime.GOARCH,
}
if cli.BuildOS != "" && cli.BuildARCH != "" {
ss.Build = fmt.Sprintf("%s/%s", cli.BuildOS, cli.BuildARCH)
}
if cli.BuildARM != "" {
ss.Build = fmt.Sprintf("%s ARMv%s", ss.Build, cli.BuildARM)
}
return ss
}
func newConfigurationStatus(status *Status) configurationStatus {
v := status.cfg.UserViperConfig
workspace := v.GetString("workspace")
if workspace == "" {
workspace = fmt.Sprintf("%s (default)", config.DefaultWorkspaceDir(status.cfg))
}
cs := configurationStatus{
Home: status.cfg.Home,
Workspace: workspace,
Dir: status.cfg.Dir,
Token: v.GetString("token"),
TokenURL: config.SettingsURL(v.GetString("apibaseurl")),
}
if status.Censor && cs.Token != "" {
cs.Token = redact(cs.Token)
}
return cs
}
func (ping *apiPing) Call(wg *sync.WaitGroup) {
defer wg.Done()
now := time.Now()
res, err := cli.HTTPClient.Get(ping.URL)
delta := time.Since(now)
ping.Latency = delta
if err != nil {
ping.Status = err.Error()
return
}
res.Body.Close()
ping.Status = "connected"
}
func redact(token string) string {
str := token[4 : len(token)-3]
redaction := strings.Repeat("*", len(str))
return string(token[:4]) + redaction + string(token[len(token)-3:])
}
const tmplSelfTest = `
Troubleshooting Information
===========================
Version
----------------
Current: {{ .Version.Current }}
Latest: {{ with .Version.Latest }}{{ . }}{{ else }}<unknown>{{ end }}
{{ with .Version.Error }}
{{ . }}
{{ end -}}
{{ if not .Version.UpToDate }}
Call 'exercism upgrade' to get the latest version.
See the release notes at https://github.com/exercism/cli/releases/tag/{{ .Version.Latest }} for details.
{{ end }}
Operating System
----------------
OS: {{ .System.OS }}
Architecture: {{ .System.Architecture }}
{{ with .System.Build }}
Build: {{ . }}
{{ end }}
Configuration
----------------
Home: {{ .Configuration.Home }}
Workspace: {{ .Configuration.Workspace }}
Config: {{ .Configuration.Dir }}
API key: {{ with .Configuration.Token }}{{ . }}{{ else }}<not configured>
Find your API key at {{ .Configuration.TokenURL }}{{ end }}
API Reachability
----------------
{{ range .APIReachability.Services }}
{{ .Service }}:
* {{ .URL }}
* [{{ .Status }}]
* {{ .Latency }}
{{ end }}
If you are having trouble please file a GitHub issue at
https://github.com/exercism/exercism.io/issues and include
this information.
{{ if not .Censor }}
Don't share your API key. Keep that private.
{{ end }}`
func init() {
RootCmd.AddCommand(troubleshootCmd)
troubleshootCmd.Flags().BoolVarP(&fullAPIKey, "full-api-key", "f", false, "display the user's full API key, censored by default")
}