Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 61 additions & 12 deletions cmd/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,31 @@ import (
"net/http"
"os"
"runtime"
"sync"
"time"

"github.com/codegangsta/cli"
"github.com/exercism/cli/config"
"github.com/exercism/cli/paths"
)

type pingResult struct {
URL string
Service string
Status string
Latency time.Duration
}

// Debug provides information about the user's environment and configuration.
func Debug(ctx *cli.Context) {
defer fmt.Printf("\nIf you are having trouble and need to file a GitHub issue (https://github.com/exercism/exercism.io/issues) please include this information (except your API key. Keep that private).\n")

client := http.Client{Timeout: 5 * time.Second}
client := &http.Client{Timeout: 20 * time.Second}

fmt.Printf("\n**** Debug Information ****\n")
fmt.Printf("Exercism CLI Version: %s\n", ctx.App.Version)

rel, err := fetchLatestRelease(client)
rel, err := fetchLatestRelease(*client)
if err != nil {
log.Println("unable to fetch latest release: " + err.Error())
} else {
Expand Down Expand Up @@ -61,18 +69,59 @@ func Debug(ctx *cli.Context) {
fmt.Println("Config file: <not configured>")
fmt.Println("API Key: <not configured>")
}

fmt.Printf("API: %s [%s]\n", c.API, pingURL(client, c.API))
fmt.Printf("XAPI: %s [%s]\n", c.XAPI, pingURL(client, c.XAPI))
fmt.Printf("Exercises Directory: %s\n", c.Dir)
}

func pingURL(client http.Client, url string) string {
res, err := client.Get(url)
if err != nil {
return err.Error()
fmt.Println("Testing API endpoints reachability")

endpoints := map[string]string{
"API": c.API,
"XAPI": c.XAPI,
"GitHub API": "https://api.github.com/",
}
defer res.Body.Close()

return "connected"
var wg sync.WaitGroup
results := make(chan pingResult)
defer close(results)

wg.Add(len(endpoints))

for service, url := range endpoints {
go func(service, url string) {
now := time.Now()
res, err := client.Get(url)
delta := time.Since(now)
if err != nil {
results <- pingResult{
URL: url,
Service: service,
Status: err.Error(),
Latency: delta,
}
return
}
defer res.Body.Close()

results <- pingResult{
URL: url,
Service: service,
Status: "connected",
Latency: delta,
}
}(service, url)
}

go func() {
for r := range results {
fmt.Printf(
"\t* %s: %s [%s] %s\n",
r.Service,
r.URL,
r.Status,
r.Latency,
)
wg.Done()
}
}()

wg.Wait()
}