Skip to content
Merged
Show file tree
Hide file tree
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
62 changes: 41 additions & 21 deletions cmd/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import (
"net/http"
"os"
"runtime"
"strings"
"sync"
"time"

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

type pingResult struct {
Expand Down Expand Up @@ -53,27 +54,9 @@ func Debug(ctx *cli.Context) error {
log.Fatal(err)
}

configured := true
if _, err = os.Stat(c.File); err != nil {
if os.IsNotExist(err) {
configured = false
} else {
log.Fatal(err)
}
}

if configured {
fmt.Printf("Config file: %s\n", c.File)
if c.APIKey != "" {
fmt.Printf("API Key: %s\n", c.APIKey)
} else {
fmt.Println("API Key: Please set your API Key to access all of the CLI features")
}
} else {
fmt.Printf("Config file: %s (not configured)\n", c.File)
fmt.Println("API Key: Please set your API Key to access all of the CLI features")
if err := printConfigFileData(ctx, c); err != nil {
log.Fatal(err)
}
fmt.Printf("Exercises Directory: %s\n", c.Dir)

fmt.Println("Testing API endpoints reachability")

Expand Down Expand Up @@ -131,3 +114,40 @@ func Debug(ctx *cli.Context) error {

return nil
}

func printConfigFileData(ctx *cli.Context, cfg *config.Config) error {
configured := true
if _, err := os.Stat(cfg.File); err != nil {
if os.IsNotExist(err) {
configured = false
} else {
return err
}
}

apiKey := "Please set your API key to access all of the CLI features"
configFile := fmt.Sprintf("%s (not configured)", cfg.File)

if configured {
configFile = cfg.File
if cfg.APIKey != "" {
if ctx.Bool("full-api-key") {
apiKey = cfg.APIKey
} else {
apiKey = redactAPIKey(cfg.APIKey)
}
}
}

fmt.Printf("Config File: %s\n", configFile)
fmt.Printf("API Key: %s\n", apiKey)
fmt.Printf("Exercises Directory: %s\n", cfg.Dir)

return nil
}

func redactAPIKey(apiKey string) string {
str := apiKey[4 : len(apiKey)-3]
redaction := strings.Repeat("*", len(str))
return string(apiKey[:4]) + redaction + string(apiKey[len(apiKey)-3:])
}
10 changes: 8 additions & 2 deletions exercism/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,14 @@ func main() {
Action: cmd.Configure,
},
{
Name: "debug",
Usage: descDebug,
Name: "debug",
Usage: descDebug,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "full-api-key",
Usage: "Displays the full API key without obfuscating it",
},
},
Action: cmd.Debug,
},
{
Expand Down