This repository was archived by the owner on Jan 16, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 140
/
Copy pathparse_config.go
175 lines (148 loc) · 3.92 KB
/
parse_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
package parsecli
import (
"encoding/json"
"fmt"
"os"
"sort"
"github.com/facebookgo/stackerr"
)
type ParseProjectConfig struct {
JSSDK string `json:"jssdk,omitempty"`
}
type ParseAppConfig struct {
ApplicationID string `json:"applicationId,omitempty"`
MasterKey string `json:"masterKey,omitempty"`
Link string `json:"link,omitempty"`
masterKey string
}
func (c *ParseAppConfig) WithInternalMasterKey(masterKey string) *ParseAppConfig {
c.masterKey = masterKey
return c
}
func (c *ParseAppConfig) GetApplicationID() string {
return c.ApplicationID
}
func (c *ParseAppConfig) GetMasterKey(e *Env) (string, error) {
if c.MasterKey != "" {
return c.MasterKey, nil
}
if c.masterKey != "" {
return c.masterKey, nil
}
app, err := FetchAppKeys(e, c.GetApplicationID())
if err != nil {
return "", err
}
c.masterKey = app.MasterKey
return app.MasterKey, nil
}
func (c *ParseAppConfig) GetApplicationAuth(e *Env) (string, error) {
return c.GetMasterKey(e)
}
func (c *ParseAppConfig) GetLink() string {
return c.Link
}
type ParseConfig struct {
Applications map[string]*ParseAppConfig `json:"applications,omitempty"`
ProjectConfig *ProjectConfig `json:"-"`
}
func (c *ParseConfig) AddAlias(name, link string) error {
if _, found := c.Applications[name]; found {
return stackerr.Newf("App %q has already been added.", name)
}
if _, found := c.Applications[link]; !found {
return stackerr.Newf("App %q wasn't found.", link)
}
c.Applications[name] = &ParseAppConfig{Link: link}
return nil
}
func (c *ParseConfig) SetDefaultApp(name string) error {
delete(c.Applications, DefaultKey)
return c.AddAlias(DefaultKey, name)
}
func (c *ParseConfig) App(name string) (AppConfig, error) {
ac, found := c.Applications[name]
if !found {
if name == DefaultKey {
return nil, stackerr.Newf("No default app configured.")
}
return nil, stackerr.Newf("App %q wasn't found.", name)
}
if ac.Link != "" {
return c.App(ac.Link)
}
return ac, nil
}
func (c *ParseConfig) GetProjectConfig() *ProjectConfig {
return c.ProjectConfig
}
func (c *ParseConfig) GetDefaultApp() string {
var defaultApp string
if DefaultKeyLink, ok := c.Applications[DefaultKey]; ok {
defaultApp = DefaultKeyLink.Link
}
return defaultApp
}
func (c *ParseConfig) GetNumApps() int {
return len(c.Applications)
}
func (c *ParseConfig) PrettyPrintApps(e *Env) {
apps := c.Applications
defaultApp := c.GetDefaultApp()
var appNames []string
for appName := range apps {
appNames = append(appNames, appName)
}
sort.Strings(appNames)
if len(appNames) == 0 {
return
}
fmt.Fprintln(
e.Out,
"The following apps are associated with Cloud Code in the current directory:",
)
for _, appName := range appNames {
if appName == DefaultKey {
continue
}
if defaultApp == appName {
fmt.Fprint(e.Out, "* ")
} else {
fmt.Fprint(e.Out, " ")
}
fmt.Fprintf(e.Out, "%s", appName)
if config, _ := apps[appName]; config.GetLink() != "" {
fmt.Fprintf(e.Out, " -> %s", config.GetLink())
}
fmt.Fprintln(e.Out, "")
}
}
func readParseConfigFile(path string) (*ParseConfig, error) {
f, err := os.Open(path)
if err != nil {
return nil, stackerr.Wrap(err)
}
defer f.Close()
var c ParseConfig
if err := json.NewDecoder(f).Decode(&c); err != nil {
return nil, stackerr.Newf("Config file %q is not valid JSON.", path)
}
return &c, nil
}
func setParseDefault(e *Env, newDefault, defaultApp string, parseConfig *ParseConfig) error {
apps := parseConfig.Applications
if _, ok := apps[newDefault]; !ok {
parseConfig.PrettyPrintApps(e)
return stackerr.Newf("Invalid application name \"%s\". Please select from the valid applications printed above.", newDefault)
}
if defaultApp == "" {
apps[DefaultKey] = &ParseAppConfig{Link: newDefault}
} else {
apps[DefaultKey].Link = newDefault
}
if err := StoreConfig(e, parseConfig); err != nil {
return err
}
fmt.Fprintf(e.Out, "Default app set to %s.\n", newDefault)
return nil
}