forked from exercism/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatus.go
More file actions
214 lines (188 loc) · 4.47 KB
/
Copy pathstatus.go
File metadata and controls
214 lines (188 loc) · 4.47 KB
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
package cli
import (
"bytes"
"fmt"
"html/template"
"runtime"
"strings"
"sync"
"time"
"github.com/exercism/cli/config"
)
// Status represents the results of a CLI self test.
type Status struct {
Censor bool
Version versionStatus
System systemStatus
Configuration configurationStatus
APIReachability apiReachabilityStatus
cli *CLI
cfg config.UserConfig
}
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
File string
Token string
TokenURL string
}
type apiReachabilityStatus struct {
Services []*apiPing
}
type apiPing struct {
Service string
URL string
Status string
Latency time.Duration
}
func NewStatus(c *CLI, uc config.UserConfig) Status {
status := Status{
cli: c,
cfg: uc,
}
return status
}
func (status *Status) Check() (string, error) {
status.Version = newVersionStatus(status.cli)
status.System = newSystemStatus()
status.Configuration = newConfigurationStatus(status)
status.APIReachability = newAPIReachabilityStatus()
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() apiReachabilityStatus {
ar := apiReachabilityStatus{
Services: []*apiPing{
{Service: "GitHub", URL: "https://api.github.com"},
{Service: "Exercism", URL: "http://exercism.io/api/v1"},
{Service: "X-API", URL: "http://x.exercism.io"},
},
}
var wg sync.WaitGroup
wg.Add(len(ar.Services))
for _, service := range ar.Services {
go service.Call(&wg)
}
wg.Wait()
return ar
}
func newVersionStatus(cli *CLI) versionStatus {
vs := versionStatus{
Current: cli.Version,
}
ok, err := cli.IsUpToDate()
if err == nil {
vs.Latest = cli.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 BuildOS != "" && BuildARCH != "" {
ss.Build = fmt.Sprintf("%s/%s", BuildOS, BuildARCH)
}
if BuildARM != "" {
ss.Build = fmt.Sprintf("%s ARMv%s", ss.Build, BuildARM)
}
return ss
}
func newConfigurationStatus(status *Status) configurationStatus {
cs := configurationStatus{
Home: status.cfg.Home,
Workspace: status.cfg.Workspace,
File: status.cfg.Path,
Token: status.cfg.Token,
TokenURL: "http://exercism.io/account/key",
}
if status.Censor {
cs.Token = redactToken(status.cfg.Token)
}
return cs
}
func (ping *apiPing) Call(wg *sync.WaitGroup) {
defer wg.Done()
now := time.Now()
res, err := 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 redactToken(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 = `
Debug 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.File }}
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 }}
`