Skip to content

Commit

Permalink
Merge pull request sequelize#70 from Tonkpils/less-panicky
Browse files Browse the repository at this point in the history
Return an error from askForConfigInfo instead of using panic
  • Loading branch information
kytrinyx committed Mar 23, 2014
2 parents 621f41d + f9ca48d commit 2730615
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
17 changes: 9 additions & 8 deletions exercism.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,35 @@ func absolutePath(path string) (string, error) {
return filepath.EvalSymlinks(path)
}

func askForConfigInfo() (c configuration.Config) {
func askForConfigInfo() (c configuration.Config, err error) {
var un, key, dir string
delim := "\r\n"

bio := bufio.NewReader(os.Stdin)

currentDir, err := os.Getwd()
if err != nil {
panic(err)
return
}

fmt.Print("Your GitHub username: ")
un, err = bio.ReadString('\n')
if err != nil {
panic(err)
return
}

fmt.Print("Your exercism.io API key: ")
key, err = bio.ReadString('\n')
if err != nil {
panic(err)
return
}

fmt.Println("What is your exercism exercises project path?")
fmt.Printf("Press Enter to select the default (%s):\n", currentDir)
fmt.Print("> ")
dir, err = bio.ReadString('\n')
if err != nil && err.Error() != "unexpected newline" {
panic(err)
if err != nil {
return
}

key = strings.TrimRight(key, delim)
Expand All @@ -70,8 +70,9 @@ func askForConfigInfo() (c configuration.Config) {

dir, err = absolutePath(dir)
if err != nil {
panic(err)
return
}

return configuration.Config{GithubUsername: un, ApiKey: key, ExercismDirectory: dir, Hostname: "http://exercism.io"}
c = configuration.Config{GithubUsername: un, ApiKey: key, ExercismDirectory: dir, Hostname: "http://exercism.io"}
return
}
5 changes: 4 additions & 1 deletion exercism_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ func TestAskForConfigInfoAllowsSpaces(t *testing.T) {

os.Stdin = file

c := askForConfigInfo()
c, err := askForConfigInfo()
if err != nil {
t.Errorf("Error asking for configuration info [%v]", err)
}
os.Stdin = oldStdin
absoluteDirName, _ := absolutePath(dirName)
_, err = os.Stat(absoluteDirName)
Expand Down
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,12 @@ func main() {
ShortName: "l",
Usage: "Save exercism.io api credentials",
Action: func(c *cli.Context) {
configuration.ToFile(c.GlobalString("config"), askForConfigInfo())
config, err := askForConfigInfo()
if err != nil {
fmt.Println(err)
return
}
configuration.ToFile(c.GlobalString("config"), config)
},
},
{
Expand Down

0 comments on commit 2730615

Please sign in to comment.