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
5 changes: 5 additions & 0 deletions cmd/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ func Demo(ctx *cli.Context) {
log.Fatal(err)
}

if dirOpt := ctx.String("dir"); dirOpt != "" {
c.SetDir(dirOpt)
}

fmt.Printf("Your exercises will be saved at: %s\n", c.Dir)
hw := user.NewHomework(problems, c)
if err := hw.Save(); err != nil {
log.Fatal(err)
Expand Down
32 changes: 25 additions & 7 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func (c *Config) IsAuthenticated() bool {
// homeDir caches the lookup of the user's home directory.
func (c *Config) homeDir() (string, error) {
if c.home != "" {
return c.home, nil
return c.home, nil // only set during testing
}
return Home()
}
Expand All @@ -220,7 +220,7 @@ func (c *Config) resolvePath(argPath string) (string, error) {
if err != nil {
return "", err
}
path = strings.Replace(path, "~", h, 1)
path = expandHome(path, h)

fi, _ := os.Stat(path)
if fi != nil && fi.IsDir() {
Expand All @@ -239,15 +239,33 @@ func (c *Config) setDefaults() error {
c.XAPI = hostXAPI
}

h, err := c.homeDir()
if err != nil {
if err := c.SetDir(c.Dir); err != nil {
return err
}

if c.Dir == "" {
c.Dir = filepath.Join(h, DirExercises)
return nil
}

// SetDir sets the configuration directory to the given path
// or defaults to the home exercism directory
func (c *Config) SetDir(path string) error {
home, err := c.homeDir()
if err != nil {
return err
}
if path == "" {
c.Dir = filepath.Join(home, DirExercises)
} else {
c.Dir = path
}
c.Dir = strings.Replace(c.Dir, "~", h, 1)

c.Dir = expandHome(c.Dir, home)
return nil
}

func expandHome(path, home string) string {
if path[:2] == "~/" {
return strings.Replace(path, "~", home, 1)
}
return path
}
19 changes: 19 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,25 @@ import (
"github.com/stretchr/testify/assert"
)

func TestSetDir(t *testing.T) {
testCases := []struct {
givenPath string
expectedPath string
}{
{"", "/test/home/exercism"},
{"~/foobar", "/test/home/foobar"},
{"/foobar/~/noexpand", "/foobar/~/noexpand"},
{"/no/modification", "/no/modification"},
{"nomodification", "nomodification"},
}

for _, testCase := range testCases {
config := &Config{home: "/test/home"}
config.SetDir(testCase.givenPath)
assert.Equal(t, testCase.expectedPath, config.Dir)
}
}

func TestLoad(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "")
if err != nil {
Expand Down
8 changes: 7 additions & 1 deletion exercism/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,13 @@ func main() {
Name: "demo",
ShortName: "d",
Usage: descDemo,
Action: cmd.Demo,
Flags: []cli.Flag{
cli.StringFlag{
Name: "dir, d",
Usage: "path to use for the demo exercises",
},
},
Action: cmd.Demo,
},
{
Name: "fetch",
Expand Down