-
Notifications
You must be signed in to change notification settings - Fork 12
/
hooks.go
118 lines (101 loc) · 2.17 KB
/
hooks.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
package hooks
import (
"encoding/json"
"fmt"
"net/http"
"github.com/AlecAivazis/survey/v2"
"github.com/MakeNowJust/heredoc"
"github.com/loginradius/lr-cli/api"
"github.com/loginradius/lr-cli/prompt"
"github.com/spf13/cobra"
)
var Name string
var Event string
var eventOption string
var TargetUrl string
var defaultEvents = []string{
"Login",
"Register",
"ResetPassword",
"UpdateProfile",
}
var proEvents = []string{
"BlockAccount",
"DeleteAccount",
}
func NewHooksCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "hooks",
Short: "Adds hooks",
Long: heredoc.Doc(`
Use this command to select a webhook event and then configure a URL to receive the payload.
`),
Example: heredoc.Doc(`
$ lr add hooks
Enter Name: <hook-name>
? Select a plan [Use arrows to move, type to filter]
> Login
Register
ResetPassword
UpdateProfile
Enter TargetUrl: <url>
Webhook has been added.
`),
RunE: func(cmd *cobra.Command, args []string) error {
return addHooks()
},
}
return cmd
}
func addHooks() error {
checkInput := input()
if !checkInput {
fmt.Println("Please enter the input paramaters properly.")
return nil
}
err := add()
if err != nil {
return err
}
fmt.Println("Webhook has been added.")
return nil
}
func input() bool {
prompt.SurveyAskOne(&survey.Input{
Message: "Enter Name:",
}, &Name, survey.WithValidator(survey.Required))
res, err := api.GetSites()
if err != nil {
return false
}
var options = defaultEvents
if res.Productplan.Name == "business" {
options = append(proEvents, options...)
}
//Currently supports only Developer plan event options.
var eventChoice int
err = prompt.SurveyAskOne(&survey.Select{
Message: "Select a plan",
Options: options,
}, &eventChoice)
if err != nil {
return false
}
Event = options[eventChoice]
prompt.SurveyAskOne(&survey.Input{
Message: "Enter TargetUrl: ",
}, &TargetUrl, survey.WithValidator(survey.Required))
return true
}
func add() error {
body, _ := json.Marshal(map[string]string{
"Event": Event,
"Name": Name,
"TargetUrl": TargetUrl,
})
_, err := api.Hooks(http.MethodPost, string(body))
if err != nil {
return err
}
return nil
}