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 137
Expand file tree
/
Copy pathheroku_config.go
More file actions
258 lines (226 loc) · 5.93 KB
/
heroku_config.go
File metadata and controls
258 lines (226 loc) · 5.93 KB
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
package parsecli
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"regexp"
"sort"
"github.com/facebookgo/jsonpipe"
"github.com/facebookgo/stackerr"
)
type HerokuAppConfig struct {
ParseAppID string `json:"parseAppId,omitempty"`
MasterKey string `json:"masterKey,omitempty"`
HerokuAppID string `json:"herokuAppId,omitempty"`
HerokuAccessToken string `json:"herokuAccessToken,omitempty"`
Link string `json:"link,omitempty"`
masterKey string
herokuAccessToken string
}
func (c *HerokuAppConfig) WithHiddenMasterKey(masterKey string) *HerokuAppConfig {
c.masterKey = masterKey
return c
}
func (c *HerokuAppConfig) WithHiddenAccessToken(accessToken string) *HerokuAppConfig {
c.herokuAccessToken = accessToken
return c
}
func (c *HerokuAppConfig) GetApplicationID() string {
return c.ParseAppID
}
func (c *HerokuAppConfig) 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 c.masterKey, nil
}
func (c *HerokuAppConfig) GetApplicationAuth(e *Env) (string, error) {
if c.herokuAccessToken != "" {
return c.herokuAccessToken, nil
}
var l Login
_, err := l.AuthUserWithToken(e, true)
if err != nil {
return "", err
}
req, err := http.NewRequest(
"POST",
"herokuToken",
ioutil.NopCloser(
jsonpipe.Encode(
map[string]string{
"id": c.HerokuAppID,
},
),
),
)
if err != nil {
return "", stackerr.Wrap(err)
}
req.Header = make(http.Header)
req.Header.Set("X-Parse-Application-Id", c.ParseAppID)
req.Header.Set("X-Parse-Account-Key", l.Credentials.Token)
resp, err := e.ParseAPIClient.RoundTrip(req)
if err != nil {
return "", stackerr.Wrap(err)
}
result := &struct {
Token string `json:"token"`
}{}
if err := json.NewDecoder(resp.Body).Decode(result); err != nil {
return "", stackerr.Wrap(err)
}
c.herokuAccessToken = result.Token
return result.Token, nil
}
func (c *HerokuAppConfig) GetLink() string {
return c.Link
}
type HerokuConfig struct {
Applications map[string]*HerokuAppConfig `json:"applications,omityempty"`
ProjectConfig *ProjectConfig `json:"-"`
}
func (c *HerokuConfig) 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] = &HerokuAppConfig{Link: link}
return nil
}
func (c *HerokuConfig) SetDefaultApp(name string) error {
delete(c.Applications, DefaultKey)
return c.AddAlias(DefaultKey, name)
}
func (c *HerokuConfig) 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 *HerokuConfig) GetProjectConfig() *ProjectConfig {
return c.ProjectConfig
}
func (c *HerokuConfig) GetDefaultApp() string {
var defaultApp string
if defaultKeyLink, ok := c.Applications[DefaultKey]; ok {
defaultApp = defaultKeyLink.Link
}
return defaultApp
}
func (c *HerokuConfig) GetNumApps() int {
return len(c.Applications)
}
var herokuAppNotFoundRegex = regexp.MustCompile("App not found")
func HerokuAppNotFound(err error) bool {
return herokuAppNotFoundRegex.MatchString(err.Error())
}
func FetchHerokuAppName(id string, e *Env) (string, error) {
req, err := http.NewRequest(
"GET",
fmt.Sprintf("https://api.heroku.com/apps/%s", id),
nil,
)
if err != nil {
return "", stackerr.Wrap(err)
}
resp, err := e.ParseAPIClient.RoundTrip(req)
if err != nil {
return "", stackerr.Wrap(err)
}
app := struct {
Name string `json:"name"`
}{}
if err := json.NewDecoder(resp.Body).Decode(&app); err != nil {
return "", stackerr.Wrap(err)
}
return app.Name, nil
}
func (c *HerokuConfig) 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)
config, _ := apps[appName]
if config.GetLink() != "" {
fmt.Fprintf(e.Out, " -> %s", config.GetLink())
}
herokuAppName, err := FetchHerokuAppName(config.HerokuAppID, e)
if err != nil {
if stackerr.HasUnderlying(err, stackerr.MatcherFunc(HerokuAppNotFound)) {
herokuAppName = ""
} else {
herokuAppName = config.HerokuAppID
}
}
fmt.Fprintf(e.Out, " (%q)\n", herokuAppName)
}
}
func readHerokuConfigFile(path string) (*HerokuConfig, error) {
f, err := os.Open(path)
if err != nil {
return nil, stackerr.Wrap(err)
}
defer f.Close()
var c HerokuConfig
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 SetHerokuDefault(e *Env, newDefault, defaultApp string, herokuConfig *HerokuConfig) error {
apps := herokuConfig.Applications
if _, ok := apps[newDefault]; !ok {
herokuConfig.PrettyPrintApps(e)
return stackerr.Newf("Invalid application name \"%s\". Please select from the valid applications printed above.", newDefault)
}
if defaultApp == "" {
apps[DefaultKey] = &HerokuAppConfig{Link: newDefault}
} else {
apps[DefaultKey].Link = newDefault
}
if err := StoreConfig(e, herokuConfig); err != nil {
return err
}
fmt.Fprintf(e.Out, "Default app set to %s.\n", newDefault)
return nil
}