-
Notifications
You must be signed in to change notification settings - Fork 390
/
global_flags.go
203 lines (168 loc) · 4.43 KB
/
global_flags.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
198
199
200
201
202
203
// Copyright (C) 2021 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"encoding/json"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/zeebo/clingy"
"github.com/zeebo/errs"
"github.com/zeebo/ini"
)
type globalFlags struct {
interactive bool
configDir string
oldConfigDir string
setup bool
migrated bool
configLoaded bool
config map[string][]string
accessesLoaded bool
accessDefault string
accesses map[string]string
}
func newGlobalFlags() *globalFlags {
return &globalFlags{}
}
func (g *globalFlags) Setup(f clingy.Flags) {
g.interactive = f.New(
"interactive", "Controls if interactive input is allowed", true,
clingy.Transform(strconv.ParseBool),
clingy.Advanced,
).(bool)
g.configDir = f.New(
"config-dir", "Directory that stores the configuration", appDir(false, "storj", "uplink"),
).(string)
g.oldConfigDir = f.New(
"old-config-dir", "Directory that stores legacy configuration. Only used during migration", appDir(true, "storj", "uplink"),
clingy.Advanced,
).(string)
g.setup = true
}
func (g *globalFlags) accessFile() string { return filepath.Join(g.configDir, "access.json") }
func (g *globalFlags) configFile() string { return filepath.Join(g.configDir, "config.ini") }
func (g *globalFlags) oldConfigFile() string { return filepath.Join(g.oldConfigDir, "config.yaml") }
func (g *globalFlags) Dynamic(name string) (vals []string, err error) {
if !g.setup {
return nil, nil
}
if err := g.migrate(); err != nil {
return nil, err
}
if err := g.loadConfig(); err != nil {
return nil, err
}
key := "UPLINK_" + strings.ToUpper(strings.ReplaceAll(name, "-", "_"))
if val, ok := os.LookupEnv(key); ok {
return []string{val}, nil
}
return g.config[name], nil
}
// loadConfig loads the configuration file from disk if it is not already loaded.
// This makes calls to loadConfig idempotent.
func (g *globalFlags) loadConfig() error {
if g.config != nil {
return nil
}
g.config = make(map[string][]string)
fh, err := os.Open(g.configFile())
if os.IsNotExist(err) {
return nil
} else if err != nil {
return errs.Wrap(err)
}
defer func() { _ = fh.Close() }()
err = ini.Read(fh, func(ent ini.Entry) error {
if ent.Section != "" {
ent.Key = ent.Section + "." + ent.Key
}
g.config[ent.Key] = append(g.config[ent.Key], ent.Value)
return nil
})
if err != nil {
return err
}
g.configLoaded = true
return nil
}
func (g *globalFlags) loadAccesses() error {
if g.accesses != nil {
return nil
}
fh, err := os.Open(g.accessFile())
if os.IsNotExist(err) {
return nil
} else if err != nil {
return errs.Wrap(err)
}
defer func() { _ = fh.Close() }()
var jsonInput struct {
Default string
Accesses map[string]string
}
if err := json.NewDecoder(fh).Decode(&jsonInput); err != nil {
return errs.Wrap(err)
}
g.accessDefault = jsonInput.Default
g.accesses = jsonInput.Accesses
g.accessesLoaded = true
return nil
}
func (g *globalFlags) GetAccessInfo() (string, map[string]string, error) {
if !g.accessesLoaded {
if err := g.loadAccesses(); err != nil {
return "", nil, err
}
if !g.accessesLoaded {
return "", nil, errs.New("must configure accesses")
}
}
// return a copy to avoid mutations messing things up
accesses := make(map[string]string)
for name, accessData := range g.accesses {
accesses[name] = accessData
}
return g.accessDefault, accesses, nil
}
// SaveAccessInfo writes out the access file using the provided values.
func (g *globalFlags) SaveAccessInfo(accessDefault string, accesses map[string]string) error {
// TODO(jeff): write it atomically
accessFh, err := os.OpenFile(g.accessFile(), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return errs.Wrap(err)
}
defer func() { _ = accessFh.Close() }()
var jsonOutput = struct {
Default string
Accesses map[string]string
}{
Default: accessDefault,
Accesses: accesses,
}
data, err := json.MarshalIndent(jsonOutput, "", "\t")
if err != nil {
return errs.Wrap(err)
}
if _, err := accessFh.Write(data); err != nil {
return errs.Wrap(err)
}
if err := accessFh.Sync(); err != nil {
return errs.Wrap(err)
}
if err := accessFh.Close(); err != nil {
return errs.Wrap(err)
}
return nil
}
func (g *globalFlags) Wrap(ctx clingy.Context, cmd clingy.Cmd) error {
if err := g.migrate(); err != nil {
return err
}
if !g.configLoaded {
// TODO(jeff): prompt for initial config setup
_ = false
}
return cmd.Execute(ctx)
}