forked from parse-community/parse-cli
/
config.go
88 lines (77 loc) · 2.07 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
package main
import (
"os"
"path/filepath"
"github.com/facebookgo/stackerr"
)
const (
defaultKey = "_default"
legacy = iota + 1
)
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
pprintApps(*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"`
}
func getConfigFile(e *env) string {
if e.Type == legacy {
return filepath.Join(e.Root, legacyConfigFile)
}
return ""
}
func configFromDir(dir string) (config, error) {
l, err := readLegacyConfigFile(filepath.Join(dir, legacyConfigFile))
if err != nil {
if stackerr.HasUnderlying(err, stackerr.MatcherFunc(os.IsNotExist)) {
return nil, stackerr.New("Command must be run in a directory containing a Parse project.")
}
return nil, err
}
projectConfig := &projectConfig{
Type: legacy,
Parse: &parseProjectConfig{JSSDK: l.Global.ParseVersion},
}
applications := l.Applications
if applications == nil {
applications = make(map[string]*parseAppConfig)
}
return &parseConfig{
Applications: applications,
projectConfig: projectConfig,
}, nil
}
func storeProjectConfig(e *env, c config) error {
projectType := c.getProjectConfig().Type
if projectType == legacy {
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
return writeLegacyConfigFile(
lconf,
filepath.Join(e.Root, legacyConfigFile),
)
}
return stackerr.Newf("Unknown project type: %s.", projectType)
}