-
-
Notifications
You must be signed in to change notification settings - Fork 344
/
config.go
197 lines (164 loc) · 3.79 KB
/
config.go
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
package config
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/url"
"os"
"strings"
"github.com/exercism/cli/paths"
)
const (
// 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"
)
// 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"`
File string `json:"-"` // full path to config file
}
// New returns a configuration struct with content from the exercism.json file
func New(path string) (*Config, error) {
configPath := paths.Config(path)
_, err := os.Stat(configPath)
if err != nil && os.IsNotExist(err) {
if path == "" {
configPath = paths.DefaultConfig
}
} else if err != nil {
return nil, err
}
c := &Config{
File: configPath,
}
err = c.load()
return c, err
}
// Update sets new values where given.
func (c *Config) Update(key, host, dir, xapi string) error {
key = strings.TrimSpace(key)
if key != "" {
c.APIKey = key
}
host = strings.TrimSpace(host)
if host != "" {
c.API = host
}
if dir != "" {
c.Dir = paths.Exercises(dir)
}
xapi = strings.TrimSpace(xapi)
if xapi != "" {
c.XAPI = xapi
}
return nil
}
// Write saves the config as JSON.
func (c *Config) Write() error {
// truncates existing file if it exists
f, err := os.Create(c.File)
if err != nil {
return err
}
defer f.Close()
b, err := json.MarshalIndent(c, "", "\t")
if err != nil {
return err
}
if _, err := f.Write(b); err != nil {
return err
}
return nil
}
func (c *Config) load() error {
if err := c.read(); err != nil {
return err
}
// in case people manually update the config file
// with weird formatting
c.APIKey = strings.TrimSpace(c.APIKey)
c.Dir = strings.TrimSpace(c.Dir)
c.API = strings.TrimSpace(c.API)
c.XAPI = strings.TrimSpace(c.XAPI)
return c.setDefaults()
}
func (c *Config) read() error {
if _, err := os.Stat(c.File); err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
f, err := os.Open(c.File)
if err != nil {
return err
}
defer f.Close()
if err := json.NewDecoder(f).Decode(&c); err != nil {
var extra string
if serr, ok := err.(*json.SyntaxError); ok {
if _, serr := f.Seek(0, os.SEEK_SET); serr != nil {
log.Fatalf("seek error: %v", serr)
}
line, str := findInvalidJSON(f, serr.Offset)
extra = fmt.Sprintf(":\ninvalid JSON syntax at line %d:\n%s",
line, str)
}
return fmt.Errorf("error parsing JSON in the config file %s%s\n%s", f.Name(), extra, err)
}
return nil
}
func findInvalidJSON(f io.Reader, pos int64) (int, string) {
var (
col int
line int
errLine []byte
)
buf := new(bytes.Buffer)
fb := bufio.NewReader(f)
for c := int64(0); c < pos; {
b, err := fb.ReadBytes('\n')
if err != nil {
log.Fatalf("read error: %v", err)
}
c += int64(len(b))
col = len(b) - int(c-pos)
line++
errLine = b
}
if len(errLine) != 0 {
buf.WriteString(fmt.Sprintf("%5d: %s <~", line, errLine[:col]))
}
return line, buf.String()
}
// 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 != ""
}
func (c *Config) setDefaults() error {
if c.API == "" {
c.API = hostAPI
}
if c.XAPI == "" {
c.XAPI = hostXAPI
}
if _, err := url.Parse(c.API); err != nil {
return fmt.Errorf("invalid API URL %s", err)
}
if _, err := url.Parse(c.XAPI); err != nil {
return fmt.Errorf("invalid xAPI URL %s", err)
}
c.Dir = paths.Exercises(c.Dir)
return nil
}