/
config.go
153 lines (133 loc) · 3.82 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
package config
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sync"
uuid "github.com/satori/go.uuid"
)
const configEnvVar = "PACH_CONFIG"
const contextEnvVar = "PACH_CONTEXT"
var defaultConfigDir = filepath.Join(os.Getenv("HOME"), ".pachyderm")
var defaultConfigPath = filepath.Join(defaultConfigDir, "config.json")
var readerOnce sync.Once
var value *Config
var readErr error
func configPath() string {
if env, ok := os.LookupEnv(configEnvVar); ok {
return env
}
return defaultConfigPath
}
// ActiveContext gets the active context in the config
func (c *Config) ActiveContext() (string, *Context, error) {
if env, ok := os.LookupEnv(contextEnvVar); ok {
context := c.V2.Contexts[env]
if context == nil {
return "", nil, fmt.Errorf("`%s` refers to a context that does not exist", contextEnvVar)
}
return env, context, nil
}
context := c.V2.Contexts[c.V2.ActiveContext]
if context == nil {
return "", nil, fmt.Errorf("the active context references one that does exist; set the active context first like so: pachctl config set active-context [value]")
}
return c.V2.ActiveContext, context, nil
}
// 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) {
readerOnce.Do(func() {
// Read json file
p := configPath()
if raw, err := ioutil.ReadFile(p); err == nil {
err = json.Unmarshal(raw, &value)
if err != nil {
readErr = err
return
}
} else if os.IsNotExist(err) {
// File doesn't exist, so create a new config
fmt.Fprintf(os.Stderr, "No config detected at %q. Generating new config...\n", p)
value = &Config{}
} else {
readErr = fmt.Errorf("fatal: could not read config at %q: %v", p, err)
return
}
updated := false
if value.UserID == "" {
updated = true
fmt.Fprintln(os.Stderr, "No UserID present in config - generating new one.")
uuid := uuid.NewV4()
value.UserID = uuid.String()
}
if value.V2 == nil {
updated = true
fmt.Fprintln(os.Stderr, "No config V2 present in config - generating a new one.")
if err := value.initV2(); err != nil {
readErr = err
return
}
}
if updated {
fmt.Fprintf(os.Stderr, "Rewriting config at %q.\n", p)
if err := value.Write(); err != nil {
readErr = fmt.Errorf("could not rewrite config at %q: %v", p, err)
return
}
}
})
return value, readErr
}
func (c *Config) initV2() error {
c.V2 = &ConfigV2{
ActiveContext: "default",
Contexts: map[string]*Context{},
Metrics: true,
}
if c.V1 != nil {
c.V2.Contexts["default"] = &Context{
Source: ContextSource_CONFIG_V1,
PachdAddress: c.V1.PachdAddress,
ServerCAs: c.V1.ServerCAs,
SessionToken: c.V1.SessionToken,
ActiveTransaction: c.V1.ActiveTransaction,
}
c.V1 = nil
} else {
c.V2.Contexts["default"] = &Context{
Source: ContextSource_NONE,
}
}
return nil
}
// Write writes the configuration in 'c' to this machine's Pachyderm config
// file.
func (c *Config) Write() error {
if c.V1 != nil {
panic("config V1 included (this is a bug)")
}
rawConfig, err := json.MarshalIndent(c, "", " ")
if err != nil {
return err
}
// If we're not using a custom config path, create the default config path
p := configPath()
if _, ok := os.LookupEnv(configEnvVar); ok {
// using overridden config path -- just make sure the parent dir exists
d := filepath.Dir(p)
if _, err := os.Stat(d); err != nil {
return fmt.Errorf("cannot use config at %s: could not stat parent directory (%v)", p, err)
}
} else {
// using the default config path, create the config directory
err = os.MkdirAll(defaultConfigDir, 0755)
if err != nil {
return err
}
}
return ioutil.WriteFile(p, rawConfig, 0644)
}