@@ -11,7 +11,7 @@ import (
1111const (
1212 // File is the default name of the JSON file where the config written.
1313 // The user can pass an alternate filename when using the CLI.
14- File = ". exercism.json"
14+ File = "exercism.json"
1515 // DirExercises is the default name of the directory for active users.
1616 // Make this non-exported when handlers.Login is deleted.
1717 DirExercises = "exercism"
@@ -20,31 +20,37 @@ const (
2020var (
2121 // Home by default will contact the location of your home directory.
2222 Home string
23+ // ConfigHome will contain $XDG_CONFIG_HOME if it is set or default config home directory.
24+ ConfigHome string
25+ // DefaultConfig will contain default path to config, according to Home
26+ DefaultConfig string
2327
24- // XDGConfigHome will contain $XDG_CONFIG_HOME if it exists.
25- XDGConfigHome string
2628 errHomeNotFound = errors .New ("unable to locate home directory" )
2729)
2830
2931func init () {
30- // on startup set default values
31- Recalculate ()
32+ var err error
33+ Home , err = findHome ()
34+ if err != nil {
35+ panic (err )
36+ }
37+ ConfigHome = os .Getenv ("XDG_CONFIG_HOME" )
38+ if ConfigHome == "" {
39+ ConfigHome = filepath .Join (Home , ".config" )
40+ }
41+ DefaultConfig = filepath .Join (Home , "." + File )
3242}
3343
3444// Config will return the correct input path given any input.
35- // Blank input will return the default configuration location.
45+ // Blank input will return the default configuration location based
46+ // on ConfigHome.
3647// Non-blank input will expand home to be an absolute path.
3748// If the target is known to be a directory, the config filename
3849// will be appended.
3950func Config (path string ) string {
4051 if path == "" {
41- if XDGConfigHome == "" {
42- return filepath .Join (Home , File )
43- }
44-
45- return filepath .Join (XDGConfigHome , File )
52+ return filepath .Join (ConfigHome , File )
4653 }
47-
4854 expandedPath := expandPath (path )
4955 if IsDir (path ) {
5056 expandedPath = filepath .Join (expandedPath , File )
@@ -62,18 +68,6 @@ func Exercises(path string) string {
6268 return expandPath (path )
6369}
6470
65- // Recalculate sets exercism paths based on Home.
66- func Recalculate () {
67- if Home == "" {
68- home , err := findHome ()
69- if err != nil {
70- panic (err )
71- }
72- Home = home
73- }
74- XDGConfigHome = os .Getenv ("XDG_CONFIG_HOME" )
75- }
76-
7771// IsDir determines whether the given path is a valid directory path.
7872func IsDir (path string ) bool {
7973 fi , _ := os .Stat (path )
@@ -114,7 +108,7 @@ func makeAbsolute(path string) string {
114108}
115109
116110func expandHome (path string ) string {
117- if path [: 2 ] == "~" + string (os .PathSeparator ) {
111+ if strings . HasPrefix ( path , "~" + string (os .PathSeparator ) ) {
118112 return strings .Replace (path , "~" , Home , 1 )
119113 }
120114 return path
0 commit comments