-
-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathconfig.go
More file actions
247 lines (210 loc) · 5.31 KB
/
config.go
File metadata and controls
247 lines (210 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package config
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
)
const (
// File is the default name of the JSON file where the config written.
// The user can pass an alternate filename when using the CLI.
File = ".exercism.json"
// LegacyFile is the name of the original config file.
// It is a misnomer, since the config was in json, not go.
LegacyFile = ".exercism.go"
// hostAPI is the endpoint to submit solutions to, and to get personalized data
hostAPI = "http://exercism.io"
// hostXAPI is the endpoint to fetch problems from
hostXAPI = "http://x.exercism.io"
// DirExercises is the default name of the directory for active users.
// Make this non-exported when handlers.Login is deleted.
DirExercises = "exercism"
)
var (
errHomeNotFound = errors.New("unable to locate home directory")
)
// Config represents the settings for particular user.
// This defines both the auth for talking to the API, as well as
// where to put problems that get downloaded.
type Config struct {
APIKey string `json:"apiKey"`
Dir string `json:"dir"`
API string `json:"api"`
XAPI string `json:"xapi"`
home string // cache user's home directory
file string // full path to config file
// deprecated, get rid of them when nobody uses 1.7.0 anymore
ExercismDirectory string `json:"exercismDirectory,omitempty"`
Hostname string `json:"hostname,omitempty"`
ProblemsHost string `json:"problemsHost,omitempty"`
}
// Home returns the user's canonical home directory.
// See: http://stackoverflow.com/questions/7922270/obtain-users-home-directory
// we can't cross compile using cgo and use user.Current()
func Home() (string, error) {
var dir string
if runtime.GOOS == "windows" {
dir = os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if dir == "" {
dir = os.Getenv("USERPROFILE")
}
} else {
dir = os.Getenv("HOME")
}
if dir == "" {
return dir, errHomeNotFound
}
return dir, nil
}
// Read loads the config from the stored JSON file.
func Read(file string) (*Config, error) {
c := &Config{}
err := c.Read(file)
return c, err
}
// New returns a new config.
// It will attempt to set defaults where no value is passed in.
func New(key, host, dir string) (*Config, error) {
c := &Config{
APIKey: key,
API: host,
Dir: dir,
}
return c.configure()
}
// Read loads the config from the stored JSON file.
func (c *Config) Read(file string) error {
renameLegacy()
if file == "" {
home, err := c.homeDir()
if err != nil {
return err
}
file = filepath.Join(home, File)
}
if _, err := os.Stat(file); err != nil {
if os.IsNotExist(err) {
c.configure()
return nil
}
return err
}
f, err := os.Open(file)
if err != nil {
return err
}
defer f.Close()
d := json.NewDecoder(f)
err = d.Decode(&c)
if err != nil {
return err
}
c.SavePath(file)
c.configure()
return nil
}
// SavePath allows the user to customize the location of the JSON file.
func (c *Config) SavePath(file string) {
if file != "" {
c.file = file
}
}
// File represents the path to the config file.
func (c *Config) File() string {
return c.file
}
// Write() saves the config as JSON.
func (c *Config) Write() error {
renameLegacy()
c.ExercismDirectory = ""
c.Hostname = ""
c.ProblemsHost = ""
// truncates existing file if it exists
f, err := os.Create(c.file)
if err != nil {
return err
}
defer f.Close()
e := json.NewEncoder(f)
return e.Encode(c)
}
func (c *Config) configure() (*Config, error) {
c.sanitize()
if c.Hostname != "" {
c.API = c.Hostname
}
if c.API == "" {
c.API = hostAPI
}
if c.ProblemsHost != "" {
c.XAPI = c.ProblemsHost
}
if c.XAPI == "" {
c.XAPI = hostXAPI
}
dir, err := c.homeDir()
if err != nil {
return c, err
}
c.file = filepath.Join(dir, File)
// use legacy value, if it exists
if c.ExercismDirectory != "" {
c.Dir = c.ExercismDirectory
}
// fall back to default value
if c.Dir == "" {
c.Dir = filepath.Join(dir, DirExercises)
}
c.Dir = strings.Replace(c.Dir, "~/", fmt.Sprintf("%s/", dir), 1)
return c, nil
}
// FilePath returns the path to the config file.
func FilePath(file string) (string, error) {
if file != "" {
return file, nil
}
dir, err := Home()
if err != nil {
return "", err
}
return filepath.Join(dir, File), nil
}
// IsAuthenticated returns true if the config contains an API key.
// This does not check whether or not that key is valid.
func (c *Config) IsAuthenticated() bool {
return c.APIKey != ""
}
// See: http://stackoverflow.com/questions/7922270/obtain-users-home-directory
// we can't cross compile using cgo and use user.Current()
func (c *Config) homeDir() (string, error) {
if c.home != "" {
return c.home, nil
}
return Home()
}
func (c *Config) sanitize() {
c.APIKey = strings.TrimSpace(c.APIKey)
c.Dir = strings.TrimSpace(c.Dir)
c.API = strings.TrimSpace(c.API)
c.XAPI = strings.TrimSpace(c.XAPI)
c.Hostname = strings.TrimSpace(c.Hostname)
c.ProblemsHost = strings.TrimSpace(c.ProblemsHost)
}
// renameLegacy normalizes the default config file name.
// This function will bail silently if any error occurs.
func renameLegacy() {
dir, err := Home()
if err != nil {
return
}
legacyPath := filepath.Join(dir, LegacyFile)
if _, err = os.Stat(legacyPath); err != nil {
return
}
correctPath := filepath.Join(dir, File)
os.Rename(legacyPath, correctPath)
return
}