Skip to content

Commit 709ec58

Browse files
committed
Tweak debug handler for unconfigured users
1 parent 5d58fd1 commit 709ec58

File tree

5 files changed

+36
-36
lines changed

5 files changed

+36
-36
lines changed

config/config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ func Read(file string) (*Config, error) {
109109
return nil, err
110110
}
111111

112+
if _, err = os.Stat(file); err != nil {
113+
if os.IsNotExist(err) {
114+
return New("", "", "")
115+
}
116+
return nil, err
117+
}
118+
112119
f, err := os.Open(file)
113120
if err != nil {
114121
return nil, err

config/config_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"fmt"
66
"io/ioutil"
7+
"strings"
78
"testing"
89

910
"github.com/stretchr/testify/assert"
@@ -68,6 +69,16 @@ func TestExpandsTildeInExercismDirectory(t *testing.T) {
6869
assert.NotContains(t, "~", expandedDir)
6970
}
7071

72+
func TestReadNonexistantConfig(t *testing.T) {
73+
c, err := Read("/no/such/config.json")
74+
assert.NoError(t, err)
75+
assert.Equal(t, c.APIKey, "")
76+
assert.Equal(t, c.Hostname, "http://exercism.io")
77+
if !strings.HasSuffix(c.Dir, "/exercism") {
78+
t.Fatal("Default unconfigured config should use home dir")
79+
}
80+
}
81+
7182
func TestReadingWritingConfig(t *testing.T) {
7283
tmpDir, err := ioutil.TempDir("", "")
7384
filename := fmt.Sprintf("%s/%s", tmpDir, File)

handlers/debug.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@ package handlers
33
import (
44
"fmt"
55
"log"
6+
"os"
67
"runtime"
78

89
"github.com/codegangsta/cli"
910
"github.com/exercism/cli/config"
1011
)
1112

1213
func Debug(ctx *cli.Context) {
14+
defer fmt.Printf("\nIf you are having any issues, please contact kytrinyx@exercism.io with this information.\n")
15+
1316
bail := func(err error) {
1417
if err != nil {
15-
fmt.Printf("\nIf you are having any issues, please contact kytrinyx@exercism.io with this information.\n")
1618
log.Fatal(err)
1719
}
1820
}
@@ -26,14 +28,25 @@ func Debug(ctx *cli.Context) {
2628
fmt.Printf("Home Dir: %s\n", dir)
2729

2830
file, err := config.FilePath(ctx.GlobalString("config"))
29-
bail(err)
31+
configured := true
32+
if _, err = os.Stat(file); err != nil {
33+
if os.IsNotExist(err) {
34+
configured = false
35+
} else {
36+
bail(err)
37+
}
38+
}
3039

3140
c, err := config.Read(file)
3241
bail(err)
3342

34-
fmt.Printf("Config file: %s\n", c.File())
43+
if configured {
44+
fmt.Printf("Config file: %s\n", c.File())
45+
fmt.Printf("API Key: %s\n", c.APIKey)
46+
} else {
47+
fmt.Println("Config file: <not configured>")
48+
fmt.Println("API Key: <not configured>")
49+
}
3550
fmt.Printf("API: %s\n", c.Hostname)
36-
fmt.Printf("API Key: %s\n", c.APIKey)
3751
fmt.Printf("Exercises Directory: %s\n", c.Dir)
38-
fmt.Println()
3952
}

handlers/info.go

Lines changed: 0 additions & 25 deletions
This file was deleted.

main.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,6 @@ func main() {
8383
},
8484
Action: handlers.Configure,
8585
},
86-
{
87-
Name: "info",
88-
ShortName: "i",
89-
Usage: "output config info",
90-
Action: handlers.Info,
91-
},
9286
{
9387
Name: "current",
9488
ShortName: "c",

0 commit comments

Comments
 (0)