This repository has been archived by the owner on Jan 16, 2021. It is now read-only.
/
config.go
276 lines (245 loc) · 6.63 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package parsecli
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/facebookgo/stackerr"
)
const (
LegacyParseFormat = iota
ParseFormat
HerokuFormat
ParseLocal = ".parse.local"
ParseProject = ".parse.project"
DefaultKey = "_default"
)
type AppConfig interface {
GetApplicationID() string
GetMasterKey(e *Env) (string, error)
GetApplicationAuth(e *Env) (string, error)
GetLink() string
}
type Config interface {
App(string) (AppConfig, error)
AddAlias(string, string) error
SetDefaultApp(string) error
GetProjectConfig() *ProjectConfig
GetDefaultApp() string
GetNumApps() int
PrettyPrintApps(*Env)
}
type ProjectConfig struct {
// Type indicates the type of config.
// Some examples are legacy, parse, etc.
Type int `json:"project_type,omitempty"`
// Parse stores the project config for Parse type.
// Currently jssdk version is the only
// project level config for Parse type.
Parse *ParseProjectConfig `json:"parse,omitempty"`
// ParserEmail is an email id of the Parse developer.
// It is associated with this project.
// It is used to fetch appropriate credentials from netrc.
ParserEmail string `json:"email,omitempty"`
}
func GetConfigFile(e *Env) string {
switch e.Type {
case LegacyParseFormat:
return filepath.Join(e.Root, LegacyConfigFile)
case ParseFormat:
return filepath.Join(e.Root, ParseLocal)
}
return ""
}
func ConfigFromDir(dir string) (Config, error) {
l, err := readLegacyConfigFile(filepath.Join(dir, LegacyConfigFile))
if err != nil && !stackerr.HasUnderlying(err, stackerr.MatcherFunc(os.IsNotExist)) {
return nil, err
}
if l != nil { // legacy config format
projectConfig := &ProjectConfig{
Type: LegacyParseFormat,
Parse: &ParseProjectConfig{
JSSDK: l.Global.ParseVersion,
},
ParserEmail: l.Global.ParserEmail,
}
applications := l.Applications
if applications == nil {
applications = make(map[string]*ParseAppConfig)
}
return &ParseConfig{
Applications: applications,
ProjectConfig: projectConfig,
}, nil
}
canonicalize := func(err error) error {
if err == nil {
return nil
}
if stackerr.HasUnderlying(err, stackerr.MatcherFunc(os.IsNotExist)) {
return stackerr.New("Command must be run inside a Parse project.")
}
return err
}
// current config format
p, err := readProjectConfigFile(filepath.Join(dir, ParseProject))
if err != nil {
return nil, canonicalize(err)
}
configFile := filepath.Join(dir, ParseLocal)
switch p.Type {
case ParseFormat:
c, err := readParseConfigFile(configFile)
if err != nil {
return nil, canonicalize(err)
}
if c.Applications == nil {
c.Applications = make(map[string]*ParseAppConfig)
}
c.ProjectConfig = p
return c, nil
case HerokuFormat:
c, err := readHerokuConfigFile(configFile)
if err != nil {
return nil, canonicalize(err)
}
if c.Applications == nil {
c.Applications = make(map[string]*HerokuAppConfig)
}
c.ProjectConfig = p
return c, nil
}
return nil, stackerr.Newf("Unknown project type: %d.", p.Type)
}
func writeConfigFile(c Config, path string) error {
b, err := json.MarshalIndent(c, "", " ")
if err != nil {
return stackerr.Wrap(err)
}
return stackerr.Wrap(ioutil.WriteFile(path, b, 0600))
}
func StoreConfig(e *Env, c Config) error {
projectType := c.GetProjectConfig().Type
switch projectType {
case LegacyParseFormat:
pc, ok := (c).(*ParseConfig)
if !ok {
return stackerr.Newf("Incorrect project type: 'legacy'.")
}
lconf := &legacyConfig{Applications: pc.Applications}
lconf.Global.ParseVersion = pc.ProjectConfig.Parse.JSSDK
return writeLegacyConfigFile(
lconf,
filepath.Join(e.Root, LegacyConfigFile),
)
case ParseFormat, HerokuFormat:
return writeConfigFile(c, filepath.Join(e.Root, ParseLocal))
}
return stackerr.Newf("Unknown project type: %d.", projectType)
}
func readProjectConfigFile(path string) (*ProjectConfig, error) {
f, err := os.Open(path)
if err != nil {
return nil, stackerr.Wrap(err)
}
defer f.Close()
var p ProjectConfig
if err := json.NewDecoder(f).Decode(&p); err != nil {
return nil, stackerr.Newf("Config file %q is not valid JSON.", path)
}
return &p, err
}
func writeProjectConfig(p *ProjectConfig, path string) error {
b, err := json.MarshalIndent(p, "", " ")
if err != nil {
return stackerr.Wrap(err)
}
return stackerr.Wrap(ioutil.WriteFile(path, b, 0600))
}
func StoreProjectConfig(e *Env, c Config) error {
p := c.GetProjectConfig()
switch p.Type {
case LegacyParseFormat:
p, ok := (c).(*ParseConfig)
if !ok {
return stackerr.New("Invalid Cloud Code config.")
}
lconf := &legacyConfig{Applications: p.Applications}
lconf.Global.ParseVersion = p.ProjectConfig.Parse.JSSDK
lconf.Global.ParserEmail = p.ProjectConfig.ParserEmail
return writeLegacyConfigFile(
lconf,
filepath.Join(e.Root, LegacyConfigFile),
)
case ParseFormat, HerokuFormat:
return writeProjectConfig(p, filepath.Join(e.Root, ParseProject))
}
return stackerr.Newf("Unknown project type: %d.", p.Type)
}
func IsProjectDir(cur string) bool {
_, err := os.Stat(filepath.Join(cur, ParseProject))
if err != nil {
return false
}
_, err = os.Stat(filepath.Join(cur, ParseLocal))
if err != nil {
return false
}
return true
}
func GetProjectRoot(e *Env, cur string) string {
if IsProjectDir(cur) {
return cur
}
root := cur
base := filepath.Base(root)
for base != "." && base != string(filepath.Separator) {
base = filepath.Base(root)
root = filepath.Dir(root)
if IsProjectDir(root) {
return root
}
}
return cur
}
func PrintDefault(e *Env, defaultApp string) error {
if defaultApp == "" {
return stackerr.New("No app is set as default app")
}
fmt.Fprintf(e.Out, "Current default app is %s\n", defaultApp)
return nil
}
func SetDefault(e *Env, newDefault, defaultApp string, c Config) error {
projectType := c.GetProjectConfig().Type
switch projectType {
case LegacyParseFormat, ParseFormat:
p, ok := c.(*ParseConfig)
if !ok {
return stackerr.New("Invalid Cloud Code config.")
}
return setParseDefault(e, newDefault, defaultApp, p)
case HerokuFormat:
h, ok := c.(*HerokuConfig)
if !ok {
return stackerr.New("Invalid Cloud Code config.")
}
return SetHerokuDefault(e, newDefault, defaultApp, h)
}
return stackerr.Newf("Unknown project type: %d.", projectType)
}
func SetParserEmail(e *Env, email string) error {
config, err := ConfigFromDir(e.Root)
if err != nil {
return err
}
config.GetProjectConfig().ParserEmail = email
err = StoreProjectConfig(e, config)
if err != nil {
fmt.Fprintln(e.Err, "Could not set parser email for project.")
return err
}
fmt.Fprintf(e.Out, "Successfully configured email for current project to: %q\n", email)
return nil
}