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 pathfunctions.go
246 lines (213 loc) · 6.1 KB
/
functions.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
package webhooks
import (
"errors"
"fmt"
"net/url"
"path"
"sort"
"strings"
"github.com/ParsePlatform/parse-cli/parsecli"
"github.com/facebookgo/stackerr"
"github.com/spf13/cobra"
)
type functionHook struct {
FunctionName string `json:"functionName,omitempty"`
URL string `json:"url,omitempty"`
Warning string `json:"warning,omitempty"`
}
func getConfirmation(message string, e *parsecli.Env) bool {
fmt.Fprintf(e.Out, message)
var confirm string
fmt.Fscanf(e.In, "%s\n", &confirm)
lower := strings.ToLower(confirm)
return lower != "" && strings.HasPrefix(lower, "y")
}
func (f functionHook) String() string {
if f.URL != "" {
return fmt.Sprintf("Function name: %q, URL: %q", f.FunctionName, f.URL)
}
return fmt.Sprintf("Function name: %q", f.FunctionName)
}
type functionHooksCmd struct {
All bool
Function *functionHook
interactive bool
}
func readFunctionName(e *parsecli.Env, params *functionHook) (*functionHook, error) {
if params != nil && params.FunctionName != "" {
return params, nil
}
var f functionHook
fmt.Fprintf(e.Out, "Please enter the function name: ")
fmt.Fscanf(e.In, "%s\n", &f.FunctionName)
if f.FunctionName == "" {
return nil, errors.New("Function name cannot be empty")
}
return &f, nil
}
func readFunctionParams(e *parsecli.Env, params *functionHook) (*functionHook, error) {
if params != nil && params.FunctionName != "" && params.URL != "" {
return params, nil
}
f, err := readFunctionName(e, params)
if err != nil {
return nil, err
}
fmt.Fprint(e.Out, "URL: https://")
fmt.Fscanf(e.In, "%s\n", &f.URL)
f.URL = "https://" + f.URL
if err := validateURL(f.URL); err != nil {
return nil, err
}
return f, nil
}
const defaultFunctionsURL = "/1/hooks/functions"
func (h *functionHooksCmd) functionHooksCreate(e *parsecli.Env, ctx *parsecli.Context) error {
params, err := readFunctionParams(e, h.Function)
if err != nil {
return err
}
var res functionHook
functionsURL, err := url.Parse(defaultFunctionsURL)
if err != nil {
return stackerr.Wrap(err)
}
_, err = e.ParseAPIClient.Post(functionsURL, params, &res)
if err != nil {
return stackerr.Wrap(err)
}
if res.Warning != "" {
fmt.Fprintf(e.Err, "WARNING: %s\n", res.Warning)
}
fmt.Fprintf(e.Out,
"Successfully created a webhook function %q pointing to %q\n",
res.FunctionName,
res.URL,
)
return nil
}
func (h *functionHooksCmd) functionHooksRead(e *parsecli.Env, ctx *parsecli.Context) error {
u := defaultFunctionsURL
var function *functionHook
if !h.All {
funct, err := readFunctionName(e, h.Function)
if err != nil {
return err
}
function = funct
u = path.Join(u, function.FunctionName)
}
functionsURL, err := url.Parse(u)
if err != nil {
return stackerr.Wrap(err)
}
var res struct {
Results []*functionHook `json:"results,omitempty"`
}
_, err = e.ParseAPIClient.Get(functionsURL, &res)
if err != nil {
return stackerr.Wrap(err)
}
var output []string
for _, function := range res.Results {
output = append(output, function.String())
}
sort.Strings(output)
if h.All {
fmt.Fprintln(e.Out, "The following cloudcode or webhook functions are associated with this app:")
} else {
if len(output) == 1 {
fmt.Fprintf(e.Out, "You have one function named: %q\n", function.FunctionName)
} else {
fmt.Fprintf(e.Out, "The following functions named: %q are associated with your app:\n", function.FunctionName)
}
}
fmt.Fprintln(e.Out, strings.Join(output, "\n"))
return nil
}
func (h *functionHooksCmd) functionHooksUpdate(e *parsecli.Env, ctx *parsecli.Context) error {
params, err := readFunctionParams(e, h.Function)
if err != nil {
return err
}
var res functionHook
functionsURL, err := url.Parse(path.Join(defaultFunctionsURL, params.FunctionName))
if err != nil {
return stackerr.Wrap(err)
}
_, err = e.ParseAPIClient.Put(functionsURL, &functionHook{URL: params.URL}, &res)
if err != nil {
return stackerr.Wrap(err)
}
if res.Warning != "" {
fmt.Fprintf(e.Err, "WARNING: %s\n", res.Warning)
}
fmt.Fprintf(e.Out,
"Successfully update the webhook function %q to point to %q\n",
res.FunctionName,
res.URL,
)
return nil
}
func (h *functionHooksCmd) functionHooksDelete(e *parsecli.Env, ctx *parsecli.Context) error {
params, err := readFunctionName(e, h.Function)
if err != nil {
return err
}
functionsURL, err := url.Parse(path.Join(defaultFunctionsURL, params.FunctionName))
if err != nil {
return stackerr.Wrap(err)
}
confirmMessage := fmt.Sprintf(
"Are you sure you want to delete webhook function: %q (y/n): ",
params.FunctionName,
)
var res functionHook
if !h.interactive || getConfirmation(confirmMessage, e) {
_, err = e.ParseAPIClient.Put(functionsURL, map[string]interface{}{"__op": "Delete"}, &res)
if err != nil {
return stackerr.Wrap(err)
}
fmt.Fprintf(e.Out, "Successfully deleted webhook function %q\n", params.FunctionName)
if res.FunctionName != "" {
fmt.Fprintf(e.Out, "Function %q defined in Cloud Code will be used henceforth\n", res.FunctionName)
}
}
return nil
}
func (h *functionHooksCmd) functionHooks(e *parsecli.Env, c *parsecli.Context) error {
hp := *h
hp.All = true
return hp.functionHooksRead(e, c)
}
func NewFunctionHooksCmd(e *parsecli.Env) *cobra.Command {
h := &functionHooksCmd{interactive: true}
c := &cobra.Command{
Use: "functions",
Short: "List Cloud Code functions and function webhooks",
Long: "List Cloud Code functions and function webhooks",
Run: parsecli.RunWithClient(e, h.functionHooks),
}
createCmd := &cobra.Command{
Use: "create",
Short: "Create a function webhook",
Long: "Create a function webhook",
Run: parsecli.RunWithClient(e, h.functionHooksCreate),
}
c.AddCommand(createCmd)
changeCmd := &cobra.Command{
Use: "edit",
Short: "Edit the URL of a function webhook",
Long: "Edit the URL of a function webhook",
Run: parsecli.RunWithClient(e, h.functionHooksUpdate),
}
c.AddCommand(changeCmd)
deleteCmd := &cobra.Command{
Use: "delete",
Short: "Delete a function webhook",
Long: "Delete a function webhook",
Run: parsecli.RunWithClient(e, h.functionHooksDelete),
}
c.AddCommand(deleteCmd)
return c
}