Skip to content

Commit

Permalink
Implement troubleshoot command.
Browse files Browse the repository at this point in the history
  • Loading branch information
robphoenix authored and aldraco committed Nov 2, 2017
1 parent d22a9b7 commit 77f68de
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 17 deletions.
25 changes: 15 additions & 10 deletions cli/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ func (status *Status) Check() (string, error) {
status.Version = newVersionStatus(status.cli)
status.System = newSystemStatus()
status.Configuration = newConfigurationStatus(status)
status.APIReachability = newAPIReachabilityStatus()
ar, err := newAPIReachabilityStatus()
if err != nil {
return "", err
}
status.APIReachability = ar

return status.compile()
}
Expand All @@ -85,12 +89,15 @@ func (status *Status) compile() (string, error) {
return bb.String(), nil
}

func newAPIReachabilityStatus() apiReachabilityStatus {
func newAPIReachabilityStatus() (apiReachabilityStatus, error) {
apiCfg, err := config.NewAPIConfig()
if err != nil {
return apiReachabilityStatus{}, nil
}
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"},
{Service: "Exercism", URL: apiCfg.URL("ping")},
},
}
var wg sync.WaitGroup
Expand All @@ -99,7 +106,7 @@ func newAPIReachabilityStatus() apiReachabilityStatus {
go service.Call(&wg)
}
wg.Wait()
return ar
return ar, nil
}

func newVersionStatus(cli *CLI) versionStatus {
Expand Down Expand Up @@ -166,9 +173,8 @@ func redactToken(token string) string {
}

const tmplSelfTest = `
Debug Information
=================
Troubleshooting Information
===========================
Version
----------------
Expand Down Expand Up @@ -212,5 +218,4 @@ https://github.com/exercism/exercism.io/issues and include
this information.
{{ if not .Censor }}
Don't share your API key. Keep that private.
{{ end }}
`
{{ end }}`
24 changes: 24 additions & 0 deletions cli/status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cli

import (
"testing"

"github.com/exercism/cli/config"
"github.com/stretchr/testify/assert"
)

func TestCensor(t *testing.T) {
fakeToken := "1a11111aaaa111aa1a11111a11111aa1"
expected := "1a11*************************aa1"

c := New("")

cfg := config.NewEmptyUserConfig()
cfg.Token = fakeToken

status := NewStatus(c, *cfg)
status.Censor = true
status.Check()

assert.Equal(t, expected, status.Configuration.Token)
}
29 changes: 23 additions & 6 deletions cmd/troubleshoot.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,43 @@ package cmd

import (
"fmt"
"net/http"
"time"

"github.com/exercism/cli/cli"
"github.com/exercism/cli/config"
"github.com/spf13/cobra"
)

// fullAPIKey flag for troubleshoot command.
var fullAPIKey bool

// troubleshootCmd does a diagnostic self-check.
var troubleshootCmd = &cobra.Command{
Use: "troubleshoot",
Aliases: []string{"t"},
Short: "Troubleshoot does a diagnostic self-check.",
Long: `Troubleshoot provides output to help with troubleshooting.
Long: `Provides output to help with troubleshooting.
If you're running into trouble, then run the troubleshoot command
and copy and paste the output into a GitHub issue so we can help
figure out what's going on.
`,
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.
`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("troubleshoot called")
cli.HTTPClient = &http.Client{Timeout: 20 * time.Second}
c := cli.New(Version)

cfg, err := config.NewUserConfig()
BailOnError(err)

status := cli.NewStatus(c, *cfg)
status.Censor = !fullAPIKey
s, err := status.Check()
BailOnError(err)
fmt.Printf("%s", s)
},
}

func init() {
RootCmd.AddCommand(troubleshootCmd)
troubleshootCmd.Flags().BoolVarP(&fullAPIKey, "full-api-key", "f", false, "display the user's full API key, censored by default")
}
2 changes: 1 addition & 1 deletion config/api_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var (
"download": "/solutions/%s",
"submit": "/solutions/%s",
"prepare-track": "/tracks/%s/settings",
"ping": "/ping",
}
)

Expand Down Expand Up @@ -41,7 +42,6 @@ func (cfg *APIConfig) SetDefaults() {
if cfg.BaseURL == "" {
cfg.BaseURL = defaultBaseURL
}

if cfg.Endpoints == nil {
cfg.Endpoints = defaultEndpoints
return
Expand Down

0 comments on commit 77f68de

Please sign in to comment.