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 pathapps.go
272 lines (241 loc) · 6.75 KB
/
apps.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
package parsecli
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"path"
"sort"
"strconv"
"strings"
"text/tabwriter"
"github.com/facebookgo/jsonpipe"
"github.com/facebookgo/stackerr"
)
type App struct {
Name string `json:"appName"`
DashboardURL string `json:"dashboardURL"`
ApplicationID string `json:"applicationId"`
ClientKey string `json:"clientKey"`
JavaScriptKey string `json:"javascriptKey"`
WindowsKey string `json:"windowsKey"`
WebhookKey string `json:"webhookKey"`
RestKey string `json:"restKey"`
MasterKey string `json:"masterKey"`
ClientPushEnabled bool `json:"clientPushEnabled"`
ClientClassCreationEnabled bool `json:"clientClassCreationEnabled"`
RequireRevocableSessions bool `json:"requireRevocableSessions"`
RevokeSessionOnPasswordChange bool `json:"revokeSessionOnPasswordChange"`
}
type Apps struct {
Login Login
}
const numRetries = 3
func allApps(apps []*App) []string {
var appNames []string
for _, app := range apps {
appNames = append(appNames, app.Name)
}
sort.Strings(appNames)
return appNames
}
func selectionString(appNames []string) string {
var buffer bytes.Buffer
for i, appName := range appNames {
buffer.WriteString(fmt.Sprintf("%d:\t%s\n", i+1, appName))
}
return buffer.String()
}
func (a *Apps) PrintApp(e *Env, params *App) error {
fmt.Fprintf(e.Out, "Properties of the app %q:\n", params.Name)
w := new(tabwriter.Writer)
w.Init(e.Out, 0, 8, 0, '\t', 0)
fmt.Fprintf(w, "Name\t%s\n", params.Name)
fmt.Fprintf(w, "DashboardURL\t%s\n", params.DashboardURL)
fmt.Fprintf(w, "ApplicationID\t%s\n", params.ApplicationID)
fmt.Fprintf(w, "ClientKey\t%s\n", params.ClientKey)
fmt.Fprintf(w, "JavaScriptKey\t%s\n", params.JavaScriptKey)
fmt.Fprintf(w, "WindowsKey\t%s\n", params.WindowsKey)
fmt.Fprintf(w, "WebhookKey\t%s\n", params.WebhookKey)
fmt.Fprintf(w, "RestKey\t%s\n", params.RestKey)
fmt.Fprintf(w, "MasterKey\t%s\n", params.MasterKey)
fmt.Fprintf(w, "ClientPushEnabled\t%t\n", params.ClientPushEnabled)
fmt.Fprintf(w, "ClientClassCreationEnabled\t%t\n", params.ClientClassCreationEnabled)
fmt.Fprintf(w, "RequireRevocableSessions\t%t\n", params.RequireRevocableSessions)
fmt.Fprintf(w, "RevokeSessionOnPasswordChange\t%t\n", params.RevokeSessionOnPasswordChange)
return stackerr.Wrap(w.Flush())
}
func getAuthHeaders(credentials *Credentials, headers http.Header) http.Header {
if headers == nil {
headers = make(http.Header)
}
if credentials.Token != "" {
headers.Add("X-Parse-Account-Key", credentials.Token)
} else {
headers.Add("X-Parse-Email", credentials.Email)
headers.Add("X-Parse-Password", credentials.Password)
}
return headers
}
func FetchAppKeys(e *Env, appID string) (*App, error) {
l := &Login{}
err := l.AuthUser(e, false)
if err != nil {
return nil, err
}
credentials := &l.Credentials
req := &http.Request{
Method: "GET",
URL: &url.URL{Path: path.Join("apps", appID)},
Header: getAuthHeaders(credentials, nil),
}
res := &App{}
if response, err := e.ParseAPIClient.Do(req, nil, res); err != nil {
if response.StatusCode == http.StatusUnauthorized {
return nil, errAuth
}
return nil, err
}
if res == nil {
return nil, stackerr.Newf("Unable to fetch keys for %s.", appID)
}
return res, nil
}
func (a *Apps) RestFetchApps(e *Env) ([]*App, error) {
req := &http.Request{
Method: "GET",
URL: &url.URL{Path: "apps"},
Header: getAuthHeaders(&a.Login.Credentials, nil),
}
var res struct {
Results []*App `json:"results"`
}
if response, err := e.ParseAPIClient.Do(req, nil, &res); err != nil {
if response.StatusCode == http.StatusUnauthorized {
return nil, errAuth
}
return nil, err
}
return res.Results, nil
}
func (a *Apps) SelectApp(apps []*App, msg string, e *Env) (*App, error) {
appNames := allApps(apps)
appCount := len(apps)
pos := -1
var err error
for {
fmt.Fprintf(e.Out, "%s%s", selectionString(appNames), msg)
var selected string
fmt.Fscanf(e.In, "%s\n", &selected)
if pos, err = strconv.Atoi(strings.TrimSpace(selected)); err != nil {
pos = -1
break
}
if pos > 0 && pos <= appCount {
break
} else {
fmt.Fprintf(e.Out, "Invalid selection %d: must be between 1 and %d\n", pos, appCount)
pos = -1
}
}
if pos != -1 {
appName := appNames[pos-1]
for _, app := range apps {
if app.Name == appName {
return app, nil
}
}
}
return nil, stackerr.Newf("Please try again. Please select from among the listed apps.")
}
func (a *Apps) ShowApps(e *Env, appName string) error {
apps, err := a.RestFetchApps(e)
if err != nil {
return err
}
if appName != "" {
for _, app := range apps {
if app.Name == appName {
return a.PrintApp(e, app)
}
}
}
fmt.Fprintf(
e.Out,
"These are the apps you currently have access to:\n%s",
selectionString(allApps(apps)),
)
return nil
}
func (a *Apps) getAppName(e *Env) (string, error) {
var appName string
fmt.Fprint(e.Out, `Please choose a name for your Parse app.
Note that this name will appear on the Parse website,
but it does not have to be the same as your mobile app's public name.
Name: `)
fmt.Fscanf(e.In, "%s\n", &appName)
if appName == "" {
return "", errors.New("App name cannot be empty")
}
return appName, nil
}
func (a *Apps) restCreateApp(e *Env, appName string) (*App, error) {
req := &http.Request{
Method: "POST",
URL: &url.URL{Path: "apps"},
Header: getAuthHeaders(&a.Login.Credentials, nil),
Body: ioutil.NopCloser(jsonpipe.Encode(map[string]string{"appName": appName})),
}
var res App
if response, err := e.ParseAPIClient.Do(req, nil, &res); err != nil {
if response != nil && response.StatusCode == http.StatusUnauthorized {
return nil, errAuth
}
return nil, err
}
return &res, nil
}
func (a *Apps) CreateApp(e *Env, givenName string, retries int) (*App, error) {
apps, err := a.RestFetchApps(e)
if err != nil {
return nil, err
}
appNames := make(map[string]struct{})
for _, app := range apps {
appNames[app.Name] = struct{}{}
}
if retries <= 0 {
retries = numRetries
}
for i := 0; i < retries; i++ {
var appName string
if givenName != "" {
appName = givenName
} else {
appName, err = a.getAppName(e)
if err != nil {
return nil, err
}
}
if _, ok := appNames[appName]; ok {
fmt.Fprintf(e.Err,
`Hey! you already created an app named %q.
The apps associated with your account:
%s
Please try creating an app with a different name next time.
`,
appName,
selectionString(allApps(apps)),
)
continue
}
app, err := a.restCreateApp(e, appName)
if err != nil {
return nil, err
}
return app, nil
}
return nil, errors.New("could not create new app")
}