Skip to content

Commit

Permalink
Clean up usage of homedir and removing ~.
Browse files Browse the repository at this point in the history
Closes #2
  • Loading branch information
mikegehard committed Aug 26, 2013
1 parent d72a01f commit bf9a052
Showing 6 changed files with 54 additions and 73 deletions.
34 changes: 8 additions & 26 deletions src/exercism/config.go
Original file line number Diff line number Diff line change
@@ -3,12 +3,17 @@ package exercism
import (
"encoding/json"
"io/ioutil"
"os/user"
"strings"
)

const FILENAME = ".exercism.go"

type Config struct {
GithubUsername string `json:"githubUsername"`
ApiKey string `json:"apiKey"`
ExercismDirectory string `json:"exercismDirectory"`
Hostname string `json:"hostname"`
}

func ConfigFromFile(dir string) (c Config, err error) {
bytes, err := ioutil.ReadFile(configFilename(dir))
if err != nil {
@@ -19,39 +24,16 @@ func ConfigFromFile(dir string) (c Config, err error) {
return
}

func ConfigToFile(u user.User, dir string, c Config) (err error) {
expandedPath, err := replaceTilde(u, c.ExercismDirectory)
if err != nil {
return
}

c.ExercismDirectory = expandedPath
func ConfigToFile(dir string, c Config) (err error) {
bytes, err := json.Marshal(c)
if err != nil {
return
}

err = ioutil.WriteFile(configFilename(dir), bytes, 0644)
if err != nil {
return
}

return
}

type Config struct {
GithubUsername string `json:"githubUsername"`
ApiKey string `json:"apiKey"`
ExercismDirectory string `json:"exercismDirectory"`
Hostname string `json:"hostname"`
}

func configFilename(dir string) string {
return dir + "/" + FILENAME
}

func replaceTilde(u user.User, oldPath string) (newPath string, err error) {
dir := u.HomeDir
newPath = strings.Replace(oldPath, "~/", dir+"/", 1)
return
}
23 changes: 1 addition & 22 deletions src/exercism/config_test.go
Original file line number Diff line number Diff line change
@@ -3,7 +3,6 @@ package exercism
import (
"github.com/stretchr/testify/assert"
"io/ioutil"
"os/user"
"testing"
)

@@ -17,30 +16,10 @@ func TestReadingWritingConfig(t *testing.T) {
ExercismDirectory: "/exercism/directory",
}

u := user.User{HomeDir: "/Users/foo"}
ConfigToFile(u, tmpDir, writtenConfig)
ConfigToFile(tmpDir, writtenConfig)

loadedConfig, err := ConfigFromFile(tmpDir)
assert.NoError(t, err)

assert.Equal(t, writtenConfig, loadedConfig)
}

func TestExpandsTildeInExercismDirectory(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "")
assert.NoError(t, err)

writtenConfig := Config{
GithubUsername: "user",
ApiKey: "MyKey",
ExercismDirectory: "~/exercism/directory",
}

u := user.User{HomeDir: "/Users/foo"}
ConfigToFile(u, tmpDir, writtenConfig)

loadedConfig, err := ConfigFromFile(tmpDir)
assert.NoError(t, err)

assert.Equal(t, loadedConfig.ExercismDirectory, "/Users/foo/exercism/directory")
}
4 changes: 1 addition & 3 deletions src/exercism/exercism_test.go
Original file line number Diff line number Diff line change
@@ -4,7 +4,6 @@ import (
"github.com/stretchr/testify/assert"
"io/ioutil"
"os"
"os/user"
"testing"
)

@@ -22,8 +21,7 @@ func TestLogoutDeletesConfigFile(t *testing.T) {

c := Config{}

user := user.User{HomeDir: "/Users/foo"}
ConfigToFile(user, tmpDir, c)
ConfigToFile(tmpDir, c)

Logout(tmpDir)

25 changes: 25 additions & 0 deletions src/exercism/fileutils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package exercism

import (
"os"
"runtime"
"strings"
)

func ReplaceTilde(oldPath string) string {
return strings.Replace(oldPath, "~/", HomeDir()+"/", 1)
}

// See: http://stackoverflow.com/questions/7922270/obtain-users-home-directory
// we can't cross compile using cgo and use user.Current()
func HomeDir() string {
if runtime.GOOS == "windows" {
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if home == "" {
home = os.Getenv("USERPROFILE")
}
return home
}

return os.Getenv("HOME")
}
11 changes: 11 additions & 0 deletions src/exercism/fileutils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package exercism

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestExpandsTildeInExercismDirectory(t *testing.T) {
expandedDir := ReplaceTilde("~/exercism/directory")
assert.NotContains(t, "~", expandedDir)
}
30 changes: 8 additions & 22 deletions src/main/exercism.go
Original file line number Diff line number Diff line change
@@ -5,7 +5,6 @@ import (
"fmt"
"github.com/codegangsta/cli"
"os"
"os/user"
)

func main() {
@@ -19,7 +18,7 @@ func main() {
ShortName: "d",
Usage: "Fetch first assignment for each language from exercism.io",
Action: func(c *cli.Context) {
config, err := exercism.ConfigFromFile(homeDir())
config, err := exercism.ConfigFromFile(exercism.HomeDir())
if err != nil {
fmt.Println("Are you sure you are logged in? Please login again.")
return
@@ -44,7 +43,7 @@ func main() {
ShortName: "f",
Usage: "Fetch current assignment from exercism.io",
Action: func(c *cli.Context) {
config, err := exercism.ConfigFromFile(homeDir())
config, err := exercism.ConfigFromFile(exercism.HomeDir())
if err != nil {
fmt.Println("Are you sure you are logged in? Please login again.")
return
@@ -69,27 +68,23 @@ func main() {
ShortName: "l",
Usage: "Save exercism.io api credentials",
Action: func(c *cli.Context) {
usr, err := user.Current()
if err != nil {
panic(nil)
}
exercism.ConfigToFile(*usr, homeDir(), askForConfigInfo())
exercism.ConfigToFile(exercism.HomeDir(), askForConfigInfo())
},
},
{
Name: "logout",
ShortName: "o",
Usage: "Clear exercism.io api credentials",
Action: func(c *cli.Context) {
exercism.Logout(homeDir())
exercism.Logout(exercism.HomeDir())
},
},
{
Name: "peek",
ShortName: "p",
Usage: "Fetch upcoming assignment from exercism.io",
Action: func(c *cli.Context) {
config, err := exercism.ConfigFromFile(homeDir())
config, err := exercism.ConfigFromFile(exercism.HomeDir())
if err != nil {
fmt.Println("Are you sure you are logged in? Please login again.")
return
@@ -114,7 +109,7 @@ func main() {
ShortName: "s",
Usage: "Submit code to exercism.io on your current assignment",
Action: func(c *cli.Context) {
config, err := exercism.ConfigFromFile(homeDir())
config, err := exercism.ConfigFromFile(exercism.HomeDir())
if err != nil {
fmt.Println("Are you sure you are logged in? Please login again.")
return
@@ -148,7 +143,7 @@ func main() {
ShortName: "w",
Usage: "Get the github username that you are logged in as",
Action: func(c *cli.Context) {
config, err := exercism.ConfigFromFile(homeDir())
config, err := exercism.ConfigFromFile(exercism.HomeDir())
if err != nil {
fmt.Println("Are you sure you are logged in? Please login again.")
return
@@ -161,15 +156,6 @@ func main() {
app.Run(os.Args)
}

func homeDir() string {
user, err := user.Current()
if err != nil {
panic(err)
}

return user.HomeDir
}

func askForConfigInfo() (c exercism.Config) {
var un, key, dir string

@@ -202,5 +188,5 @@ func askForConfigInfo() (c exercism.Config) {
dir = currentDir
}

return exercism.Config{un, key, dir, "http://exercism.io"}
return exercism.Config{un, key, exercism.ReplaceTilde(dir), "http://exercism.io"}
}

0 comments on commit bf9a052

Please sign in to comment.