/
config.go
60 lines (54 loc) · 1.39 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
package config
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/satori/go.uuid"
)
var configDirPath = filepath.Join(os.Getenv("HOME"), ".pachyderm")
var configPath = filepath.Join(configDirPath, "config.json")
// Read loads the Pachyderm config on this machine.
// If an existing configuration cannot be found, it sets up the defaults. Read
// returns a nil Config if and only if it returns a non-nil error.
func Read() (*Config, error) {
var c *Config
// Read json file
if raw, err := ioutil.ReadFile(configPath); err == nil {
err = json.Unmarshal(raw, &c)
if err != nil {
return nil, err
}
} else {
// File doesn't exist, so create a new config
fmt.Println("No config detected. Generating new config...")
c = &Config{}
}
if c.UserID == "" {
fmt.Printf("No UserID present in config. Generating new UserID and "+
"updating config at %s\n", configPath)
uuid, err := uuid.NewV4()
if err != nil {
return nil, err
}
c.UserID = uuid.String()
if err := c.Write(); err != nil {
return nil, err
}
}
return c, nil
}
// Write writes the configuration in 'c' to this machine's Pachyderm config
// file.
func (c *Config) Write() error {
rawConfig, err := json.MarshalIndent(c, "", " ")
if err != nil {
return err
}
err = os.MkdirAll(configDirPath, 0755)
if err != nil {
return err
}
return ioutil.WriteFile(configPath, rawConfig, 0644)
}