-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathcheck.go
More file actions
381 lines (338 loc) · 11.7 KB
/
check.go
File metadata and controls
381 lines (338 loc) · 11.7 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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// Copyright 2022-2025 Salesforce, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package doctor
import (
"context"
"fmt"
"math/rand"
"runtime"
"strings"
"time"
"github.com/slackapi/slack-cli/internal/deputil"
"github.com/slackapi/slack-cli/internal/iostreams"
"github.com/slackapi/slack-cli/internal/pkg/version"
"github.com/slackapi/slack-cli/internal/shared"
"github.com/slackapi/slack-cli/internal/slackerror"
"github.com/slackapi/slack-cli/internal/style"
"github.com/slackapi/slack-cli/internal/update"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
type Section struct {
Label string
Value string
Subsections []Section
Errors []slackerror.Error
}
// HasError returns if errors exist in any section or subsection
func (s *Section) HasError() bool {
return s.SumErrors() > 0
}
// SumErrors returns the error count in sections and subsections
func (s *Section) SumErrors() int {
totalErrors := 0
if len(s.Errors) > 0 {
totalErrors += len(s.Errors)
}
for _, subsection := range s.Subsections {
totalErrors += subsection.SumErrors()
}
return totalErrors
}
// RenderLabel formats a label for use with optional values
func (s *Section) RenderLabel() string {
if len(s.Value) > 0 {
return s.Label + ":"
}
return s.Label
}
// checkOS returns the operating system information of the user's system
func checkOS(ctx context.Context, clients *shared.ClientFactory) Section {
osSection := Section{
Label: "Operating System",
Value: osDescription(clients.IO),
}
osVersion, osArch := runtime.GOOS, runtime.GOARCH
versionSection := Section{
Label: "Version",
Value: fmt.Sprintf("%s (%s)", osVersion, osArch),
}
switch osVersion {
case "windows":
case "darwin":
case "linux":
default:
osErr := slackerror.ErrorCodeMap[slackerror.ErrOSNotSupported]
versionSection.Errors = []slackerror.Error{osErr}
}
osSection.Subsections = []Section{versionSection}
return osSection
}
// osDescription returns a short and random definition of an operating system
func osDescription(io iostreams.IOStreamer) string {
descriptions := []string{
"the computer conductor",
"system management software",
"a processor of processes",
"the kernel and drivers",
"a user-computer interface",
"the computer command center",
"virtual machine orchestrator",
"program scheduler and such",
"resource allocation manager",
"the hardware guardian",
"digital infrastructure controller",
"hardware access mediator",
}
rand.New(rand.NewSource(time.Now().UnixNano()))
choice := rand.Intn(len(descriptions))
// Remove choice and chance in scripting
if !io.IsTTY() {
choice = 0
}
return descriptions[choice]
}
// checkCLIVersion returns the installed version of the user's Slack CLI
func checkCLIVersion(ctx context.Context, clients *shared.ClientFactory) (Section, error) {
cliVersion := version.Version
versionSection := Section{"Version", cliVersion, []Section{}, []slackerror.Error{}}
return Section{"CLI", "this tool for building Slack apps", []Section{versionSection}, []slackerror.Error{}}, nil
}
// checkProjectConfig returns details about the current project configurations
// or returns an empty section if not in a project directory
func checkProjectConfig(ctx context.Context, clients *shared.ClientFactory) Section {
section := Section{
Label: "Configurations",
Value: "your project's CLI settings",
}
projectConfig, err := clients.Config.ProjectConfig.ReadProjectConfigFile(ctx)
if err != nil {
if slackerror.ToSlackError(err).Code != slackerror.ErrInvalidAppDirectory {
section.Errors = append(section.Errors, *slackerror.ToSlackError(err))
}
return section
}
if projectConfig.Manifest != nil && projectConfig.Manifest.Source != "" {
section.Subsections = append(section.Subsections, Section{
Label: "Manifest source",
Value: projectConfig.Manifest.Source,
})
} else {
section.Errors = append(section.Errors,
*slackerror.New(slackerror.ErrProjectConfigManifestSource),
)
}
if projectConfig.ProjectID != "" {
section.Subsections = append(section.Subsections, Section{
Label: "Project ID",
Value: projectConfig.ProjectID,
})
} else {
section.Errors = append(section.Errors,
*slackerror.New(slackerror.ErrProjectConfigIDNotFound),
)
}
return section
}
// checkProjectDeps returns details about the current project's dependencies
func checkProjectDeps(ctx context.Context, clients *shared.ClientFactory) Section {
section := Section{
Label: "Dependencies",
Value: "requisites for development",
}
checkUpdateJSON, err := update.CheckUpdateHook(ctx, clients)
if err != nil {
slackErr := slackerror.ToSlackError(err)
if slackErr.Code == slackerror.ErrSDKHookInvocationFailed {
slackErr.Remediation = "Check that the check-update hook command is valid"
}
section.Errors = append(section.Errors, *slackErr)
return section
}
for _, release := range checkUpdateJSON.Releases {
dependencyText := release.Current
if release.Update {
latest := style.CommandText(fmt.Sprintf(`%s (update available)`, release.Latest))
dependencyText = fmt.Sprintf(`%s → %s`, release.Current, latest)
} else if release.Current != release.Latest {
// If there's no update but the versions don't match, warn of unsupported version
latest := style.CommandText(fmt.Sprintf(`%s (supported version)`, release.Latest))
dependencyText = fmt.Sprintf(`%s → %s`, release.Current, latest)
}
section.Subsections = append(section.Subsections, Section{
Label: release.Name,
Value: dependencyText,
})
}
return section
}
// checkCLIConfig reads the contents of config.json
// and outputs details of the configuration settings
func checkCLIConfig(ctx context.Context, clients *shared.ClientFactory) (Section, error) {
section := Section{"Configurations", "any adjustments to settings", []Section{}, []slackerror.Error{}}
userConfig, err := clients.Config.SystemConfig.UserConfig(ctx)
if err != nil {
return Section{}, slackerror.Wrap(err, "Failed to read system configuration")
}
// System ID
systemIDSubsection := Section{
"System ID",
userConfig.SystemID,
[]Section{},
[]slackerror.Error{},
}
if userConfig.SystemID == "" {
errSystemID := slackerror.ErrorCodeMap[slackerror.ErrSystemConfigIDNotFound]
systemIDSubsection.Errors = []slackerror.Error{errSystemID}
}
// Last Updated
lastUpdatedSubsection := Section{
"Last updated",
userConfig.LastUpdateCheckedAt.Format("2006-01-02 15:04:05 Z07:00"),
[]Section{},
[]slackerror.Error{},
}
// Experiments
allConfigExperiments := "None"
allExperiments := []string{}
for _, exp := range clients.Config.GetExperiments() {
allExperiments = append(allExperiments, string(exp))
}
if len(allExperiments) > 0 {
allConfigExperiments = strings.Join(allExperiments, ", ")
}
experimentsSubsection := Section{
"Experiments",
allConfigExperiments,
[]Section{},
[]slackerror.Error{},
}
// Build the list of subsections
subsection := []Section{
systemIDSubsection,
lastUpdatedSubsection,
experimentsSubsection,
}
section.Subsections = subsection
return section, nil
}
// checkCLICreds reads the contents of credentials.json
// and outputs information for each team listed
func checkCLICreds(ctx context.Context, clients *shared.ClientFactory) (Section, error) {
section := Section{"Credentials", "your Slack authentication", []Section{}, []slackerror.Error{}}
authList, err := clients.AuthInterface().Auths(ctx)
if err != nil {
return Section{}, slackerror.New(slackerror.ErrAuthToken).WithRootCause(err)
}
// No teams
if len(authList) == 0 {
section.Errors = []slackerror.Error{*slackerror.New(slackerror.ErrNotAuthed)}
}
// Teams
if len(authList) > 0 {
authSections := []Section{}
currentAPIHost := clients.Config.APIHostResolved
caser := cases.Title(language.English)
for _, authInfo := range authList {
checkDetails := []Section{
{"Team domain", authInfo.TeamDomain, []Section{}, []slackerror.Error{}},
{"Team ID", authInfo.TeamID, []Section{}, []slackerror.Error{}},
{"User ID", authInfo.UserID, []Section{}, []slackerror.Error{}},
{
"Last updated",
authInfo.LastUpdated.Format("2006-01-02 15:04:05 Z07:00"),
[]Section{},
[]slackerror.Error{},
},
{"Authorization level", caser.String(authInfo.AuthLevel()), []Section{}, []slackerror.Error{}},
}
if authInfo.APIHost != nil {
hostSection := Section{"API Host", *authInfo.APIHost, []Section{}, []slackerror.Error{}}
checkDetails = append(checkDetails, hostSection)
}
// Validate session token
validitySection := Section{"Token status", "Valid", []Section{}, []slackerror.Error{}}
// TODO :: .ValidateSession() utilizes the host (APIHost) assigned to the client making
// the call. This results in incorrectly deeming tokens invalid if using multiple workspaces
// with different API hosts. (cc: @mbrooks)
clients.Config.APIHostResolved = clients.AuthInterface().ResolveAPIHost(ctx, clients.Config.APIHostFlag, &authInfo)
_, err := clients.API().ValidateSession(ctx, authInfo.Token)
if err != nil {
validitySection.Value = "Invalid"
}
checkDetails = append(checkDetails, validitySection)
authSection := Section{"", "", checkDetails, []slackerror.Error{}}
authSections = append(authSections, authSection)
}
clients.Config.APIHostResolved = currentAPIHost
section.Subsections = authSections
}
return section, nil
}
// checkProjectTooling collects dependencies required for project execution
func checkProjectTooling(ctx context.Context, clients *shared.ClientFactory) Section {
toolingSections := []Section{}
toolingErrors := []slackerror.Error{}
doctorJSON, err := doctorHook(ctx, clients)
if err != nil {
slackErr := slackerror.ToSlackError(err)
if slackErr.Code == slackerror.ErrSDKHookInvocationFailed {
slackErr.Remediation = "Check that the doctor hook command is valid"
}
toolingErrors = append(toolingErrors, *slackerror.ToSlackError(err))
} else {
for _, version := range doctorJSON.Versions {
versionSections := []Section{}
versionErrors := []slackerror.Error{}
if version.Message != "" {
latest := fmt.Sprintf("Note: %s", version.Message)
versionSections = append(versionSections, Section{
Label: style.Secondary(latest),
})
}
if version.Error.Message != "" {
versionErrors = append(
versionErrors,
*slackerror.New(slackerror.ErrRuntimeNotSupported).
WithMessage("%s", version.Error.Message),
)
}
toolingSections = append(toolingSections, Section{
Label: version.Name,
Value: version.Current,
Subsections: versionSections,
Errors: versionErrors,
})
}
}
section := Section{
Label: "Runtime",
Value: "foundations for the application",
Subsections: toolingSections,
Errors: toolingErrors,
}
return section
}
// CheckGit checks for the version of an installed Git on the user machine
func CheckGit(ctx context.Context) (Section, error) {
gitSection := Section{"Git", "a version control system", []Section{}, []slackerror.Error{}}
version, err := deputil.GetGitVersion()
if err != nil {
gitSection.Errors = []slackerror.Error{*slackerror.ToSlackError(err)}
return gitSection, nil
}
versionSection := Section{"Version", string(version), []Section{}, []slackerror.Error{}}
gitSection.Subsections = []Section{versionSection}
return gitSection, nil
}