Skip to content

Commit 41999e7

Browse files
Katrina Owennywilken
authored andcommitted
Tweak output of troubleshoot command when the CLI is unconfigured (#675)
* Pass full configuration value to troubleshooting status Instead of passing just the viper config, which could be empty, pass the full config.Configuration value, which has access to defaults. This will let us give better debugging output. * Fix output for 'home' in troubleshoot command The home directory is never written to the viper config, we get it from the environment, which is stored in the config.Configuration value. * Output default workspace in troubleshoot if unconfigured Rather than providing an empty string for the workspace in the troubleshoot command when the CLI is unconfigured, this now says Workspace: /the/actual/path (default) * Use config dir, not file, in troubleshoot command In the old CLI we had a single config file. Now we potentially have several. They will all live in the config directory. The troubleshoot command now outputs the directory instead of the file. If the troubleshoot command is unconfigured, this would previously have given an empty string for the config file. Now it will always print the default config directory, unless the user has specifically defined an override using environment variables. * Inline token in troubleshoot config status It was unnecessary to have an additional local variable. * Use proper helper method for settings URL in troubleshoot command The config package has logic for the settings URL, we shouldn't be hard-coding it here. * Fix ping when troubleshoot is unconfigured
1 parent 37f0507 commit 41999e7

1 file changed

Lines changed: 29 additions & 17 deletions

File tree

cmd/troubleshoot.go

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ command into a GitHub issue so we can help figure out what's going on.
4242
// Ignore error. If the file doesn't exist, that is fine.
4343
_ = v.ReadInConfig()
4444

45-
status := newStatus(c, v)
45+
cfg.UserViperConfig = v
46+
47+
status := newStatus(c, cfg)
4648
status.Censor = !fullAPIKey
4749
s, err := status.check()
4850
if err != nil {
@@ -61,8 +63,8 @@ type Status struct {
6163
System systemStatus
6264
Configuration configurationStatus
6365
APIReachability apiReachabilityStatus
66+
cfg config.Configuration
6467
cli *cli.CLI
65-
cfg *viper.Viper
6668
}
6769

6870
type versionStatus struct {
@@ -82,7 +84,7 @@ type systemStatus struct {
8284
type configurationStatus struct {
8385
Home string
8486
Workspace string
85-
File string
87+
Dir string
8688
Token string
8789
TokenURL string
8890
}
@@ -99,10 +101,10 @@ type apiPing struct {
99101
}
100102

101103
// newStatus prepares a value to perform a diagnostic self-check.
102-
func newStatus(c *cli.CLI, v *viper.Viper) Status {
104+
func newStatus(cli *cli.CLI, cfg config.Configuration) Status {
103105
status := Status{
104-
cli: c,
105-
cfg: v,
106+
cfg: cfg,
107+
cli: cli,
106108
}
107109
return status
108110
}
@@ -112,7 +114,7 @@ func (status *Status) check() (string, error) {
112114
status.Version = newVersionStatus(status.cli)
113115
status.System = newSystemStatus()
114116
status.Configuration = newConfigurationStatus(status)
115-
status.APIReachability = newAPIReachabilityStatus(status.cfg.GetString("apibaseurl"))
117+
status.APIReachability = newAPIReachabilityStatus(status.cfg)
116118

117119
return status.compile()
118120
}
@@ -127,7 +129,11 @@ func (status *Status) compile() (string, error) {
127129
return bb.String(), nil
128130
}
129131

130-
func newAPIReachabilityStatus(baseURL string) apiReachabilityStatus {
132+
func newAPIReachabilityStatus(cfg config.Configuration) apiReachabilityStatus {
133+
baseURL := cfg.UserViperConfig.GetString("apibaseurl")
134+
if baseURL == "" {
135+
baseURL = cfg.DefaultBaseURL
136+
}
131137
ar := apiReachabilityStatus{
132138
Services: []*apiPing{
133139
{Service: "GitHub", URL: "https://api.github.com"},
@@ -172,16 +178,22 @@ func newSystemStatus() systemStatus {
172178
}
173179

174180
func newConfigurationStatus(status *Status) configurationStatus {
175-
token := status.cfg.GetString("token")
181+
v := status.cfg.UserViperConfig
182+
183+
workspace := v.GetString("workspace")
184+
if workspace == "" {
185+
workspace = fmt.Sprintf("%s (default)", config.DefaultWorkspaceDir(status.cfg))
186+
}
187+
176188
cs := configurationStatus{
177-
Home: status.cfg.GetString("home"),
178-
Workspace: status.cfg.GetString("workspace"),
179-
File: status.cfg.ConfigFileUsed(),
180-
Token: token,
181-
TokenURL: config.InferSiteURL(status.cfg.GetString("apibaseurl")) + "/my/settings",
189+
Home: status.cfg.Home,
190+
Workspace: workspace,
191+
Dir: status.cfg.Dir,
192+
Token: v.GetString("token"),
193+
TokenURL: config.SettingsURL(v.GetString("apibaseurl")),
182194
}
183-
if status.Censor && token != "" {
184-
cs.Token = redact(token)
195+
if status.Censor && cs.Token != "" {
196+
cs.Token = redact(cs.Token)
185197
}
186198
return cs
187199
}
@@ -235,7 +247,7 @@ Configuration
235247
----------------
236248
Home: {{ .Configuration.Home }}
237249
Workspace: {{ .Configuration.Workspace }}
238-
Config: {{ .Configuration.File }}
250+
Config: {{ .Configuration.Dir }}
239251
API key: {{ with .Configuration.Token }}{{ . }}{{ else }}<not configured>
240252
Find your API key at {{ .Configuration.TokenURL }}{{ end }}
241253

0 commit comments

Comments
 (0)