@@ -4,36 +4,20 @@ import (
44 "bufio"
55 "bytes"
66 "encoding/json"
7- "errors"
87 "fmt"
98 "io"
109 "log"
1110 "os"
12- "path/filepath"
13- "runtime"
1411 "strings"
12+
13+ "github.com/exercism/cli/paths"
1514)
1615
1716const (
18- // File is the default name of the JSON file where the config written.
19- // The user can pass an alternate filename when using the CLI.
20- File = ".exercism.json"
21- // LegacyFile is the name of the original config file.
22- // It is a misnomer, since the config was in json, not go.
23- LegacyFile = ".exercism.go"
24-
2517 // hostAPI is the endpoint to submit solutions to, and to get personalized data
2618 hostAPI = "http://exercism.io"
2719 // hostXAPI is the endpoint to fetch problems from
2820 hostXAPI = "http://x.exercism.io"
29-
30- // DirExercises is the default name of the directory for active users.
31- // Make this non-exported when handlers.Login is deleted.
32- DirExercises = "exercism"
33- )
34-
35- var (
36- errHomeNotFound = errors .New ("unable to locate home directory" )
3721)
3822
3923// Config represents the settings for particular user.
@@ -45,33 +29,12 @@ type Config struct {
4529 API string `json:"api"`
4630 XAPI string `json:"xapi"`
4731 File string `json:"-"` // full path to config file
48- home string // cache user's home directory
49- }
50-
51- // Home returns the user's canonical home directory.
52- // See: http://stackoverflow.com/questions/7922270/obtain-users-home-directory
53- // we can't cross compile using cgo and use user.Current()
54- func Home () (string , error ) {
55- var dir string
56- if runtime .GOOS == "windows" {
57- dir = os .Getenv ("USERPROFILE" )
58- if dir == "" {
59- dir = os .Getenv ("HOMEDRIVE" ) + os .Getenv ("HOMEPATH" )
60- }
61- } else {
62- dir = os .Getenv ("HOME" )
63- }
64-
65- if dir == "" {
66- return dir , errHomeNotFound
67- }
68- return dir , nil
6932}
7033
7134// New returns a configuration struct with content from the exercism.json file
7235func New (path string ) (* Config , error ) {
7336 c := & Config {}
74- err := c .load (path )
37+ err := c .load (paths . Config ( path ) )
7538 return c , err
7639}
7740
@@ -87,11 +50,8 @@ func (c *Config) Update(key, host, dir, xapi string) error {
8750 c .API = host
8851 }
8952
90- dir = strings .TrimSpace (dir )
9153 if dir != "" {
92- if err := c .SetDir (dir ); err != nil {
93- return err
94- }
54+ c .Dir = paths .Exercises (dir )
9555 }
9656
9757 xapi = strings .TrimSpace (xapi )
@@ -124,11 +84,8 @@ func (c *Config) Write() error {
12484}
12585
12686func (c * Config ) load (argPath string ) error {
127- path , err := c .resolvePath (argPath )
128- if err != nil {
129- return err
130- }
131- c .File = path
87+ fmt .Println ("resolved path:" , argPath )
88+ c .File = argPath
13289
13390 if err := c .read (); err != nil {
13491 return err
@@ -207,33 +164,6 @@ func (c *Config) IsAuthenticated() bool {
207164 return c .APIKey != ""
208165}
209166
210- // homeDir caches the lookup of the user's home directory.
211- func (c * Config ) homeDir () (string , error ) {
212- if c .home != "" {
213- return c .home , nil // only set during testing
214- }
215- return Home ()
216- }
217-
218- func (c * Config ) resolvePath (argPath string ) (string , error ) {
219- path := argPath
220- if path == "" {
221- path = filepath .Join ("~" , File )
222- }
223- h , err := c .homeDir ()
224- if err != nil {
225- return "" , err
226- }
227- path = expandHome (path , h )
228-
229- fi , _ := os .Stat (path )
230- if fi != nil && fi .IsDir () {
231- path = filepath .Join (path , File )
232- }
233-
234- return path , nil
235- }
236-
237167func (c * Config ) setDefaults () error {
238168 if c .API == "" {
239169 c .API = hostAPI
@@ -243,49 +173,7 @@ func (c *Config) setDefaults() error {
243173 c .XAPI = hostXAPI
244174 }
245175
246- if err := c .SetDir (c .Dir ); err != nil {
247- return err
248- }
176+ c .Dir = paths .Exercises (c .Dir )
249177
250178 return nil
251179}
252-
253- // SetDir sets the configuration directory to the given path
254- // or defaults to the home exercism directory
255- func (c * Config ) SetDir (path string ) error {
256- home , err := c .homeDir ()
257- if err != nil {
258- return err
259- }
260-
261- var dir string
262-
263- if path == "" {
264- dir = filepath .Join (home , DirExercises )
265- } else {
266- dir = path
267- }
268-
269- dir = expandHome (dir , home )
270-
271- // if the user has provided us with a relative path, make it absolute so
272- // it will always work
273- if ! filepath .IsAbs (dir ) {
274- wd , err := os .Getwd ()
275- if err != nil {
276- return err
277- }
278- dir = filepath .Join (wd , dir )
279- }
280-
281- c .Dir = dir
282-
283- return nil
284- }
285-
286- func expandHome (path , home string ) string {
287- if path [:2 ] == "~" + string (os .PathSeparator ) {
288- return strings .Replace (path , "~" , home , 1 )
289- }
290- return path
291- }
0 commit comments