-
Notifications
You must be signed in to change notification settings - Fork 7
/
conf.go
80 lines (72 loc) · 2.53 KB
/
conf.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
//Package conf gives the CLI access to the nerd config file. By default this config file is
//~/.nerd/config.json, but the location can be changed using SetLocation().
//
//All read and write operation to the config file should go through the Read() and Write() functions.
//This way we can keep an in-memory representation of the config (in the global conf variable) for fast read.
package conf
import (
"encoding/json"
"io/ioutil"
"path/filepath"
homedir "github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
)
//Config is the structure that describes how the config file looks.
type Config struct {
Auth AuthConfig `json:"auth"`
EnableLogging bool `json:"enable_logging"`
NerdAPIEndpoint string `json:"nerd_api_endpoint"`
}
//AuthConfig contains config details with respect to the authentication server.
type AuthConfig struct {
APIEndpoint string `json:"api_endpoint"`
PublicKey string `json:"public_key"`
ClientID string `json:"client_id"`
OAuthSuccessURL string `json:"oauth_success_url"`
OAuthLocalServer string `json:"oauth_localserver"`
}
//Defaults provides the default for when the config file misses certain fields.
func Defaults() *Config {
return &Config{
Auth: AuthConfig{
APIEndpoint: "https://auth.nerdalize.com/v1/",
OAuthLocalServer: "localhost:9876",
OAuthSuccessURL: "https://cloud.nerdalize.com",
ClientID: "Po2PHR7YOIEQojfHy3zfOMulby9QGVSvcD9bO3S3",
PublicKey: `-----BEGIN PUBLIC KEY-----
MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEBthEmchVCtA3ZPXqiCXdj+7/ZFuhxRgx
grTxIHK+b0vEqKqA3O++ggD1GgjqtTfNLGUjLCE3KxyIN78TsK+HU4VVexTjlWXy
WPtidD68xGD0JVPU1cSfu8iP0XzwgttG
-----END PUBLIC KEY-----
`,
},
EnableLogging: false,
NerdAPIEndpoint: "https://batch.nerdalize.com/v1/",
}
}
//GetDefaultConfigLocation sets the location to ~/.nerd/config.json
func GetDefaultConfigLocation() (string, error) {
dir, err := homedir.Dir()
if err != nil {
return "", errors.Wrap(err, "failed to find home dir")
}
return filepath.Join(dir, ".nerd", "config.json"), nil
}
//Read reads the config file
func Read(location string) (*Config, error) {
content, err := ioutil.ReadFile(location)
if err != nil {
return nil, errors.Wrap(err, "failed to open config file")
}
conf := Defaults()
err = json.Unmarshal(content, conf)
if err != nil {
return nil, errors.Wrap(err, "failed to parse config file")
}
return conf, nil
}
//FromJSON returns a Config object from a JSON string
func FromJSON(in string) (*Config, error) {
v := Defaults()
return v, json.Unmarshal([]byte(in), v)
}