forked from robdimsdale/wl
/
webhook.go
111 lines (95 loc) · 2.63 KB
/
webhook.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
package commands
import (
"fmt"
"os"
"strconv"
"github.com/robdimsdale/wl"
"github.com/spf13/cobra"
)
const (
urlLongFlag = "url"
)
var (
// Flags
url string
// Commands
cmdWebhooks = &cobra.Command{
Use: "webhooks",
Short: "gets all webhooks",
Long: `webhooks gets the user's webhooks.
`,
Run: func(cmd *cobra.Command, args []string) {
// Currently sending completed=false is the same as not sending completed
// Checking for whether the flag has changed protects us from potentially
// breaking changes, i.e. if the webhooks endpoint decides to return all webhooks,
// not just non-completed ones.
if listID == 0 {
renderOutput(newClient(cmd).Webhooks())
} else {
renderOutput(newClient(cmd).WebhooksForListID(listID))
}
},
}
cmdWebhook = &cobra.Command{
Use: "webhook <webhook-id>",
Short: "gets the webhook for the provided webhook id",
Long: `webhook gets a webhook specified by <webhook-id>
`,
Run: func(cmd *cobra.Command, args []string) {
renderOutput(webhook(cmd, args))
},
}
cmdCreateWebhook = &cobra.Command{
Use: "create-webhook",
Short: "creates a webhook with the specified args",
Long: `create-webhook creates a webhook with the specified args
`,
Run: func(cmd *cobra.Command, args []string) {
renderOutput(newClient(cmd).CreateWebhook(
listID,
url,
"generic",
"",
))
},
}
cmdDeleteWebhook = &cobra.Command{
Use: "delete-webhook <webhook-id>",
Short: "deletes the webhook for the provided webhook id",
Long: `delete-webhook deletes the webhook specified by <webhook-id>
`,
Run: func(cmd *cobra.Command, args []string) {
webhook, err := webhook(cmd, args)
if err != nil {
fmt.Printf("error getting webhook: %v\n\n", err)
cmd.Usage()
os.Exit(2)
}
err = newClient(cmd).DeleteWebhook(webhook)
if err != nil {
handleError(err)
}
fmt.Printf("webhook %d deleted successfully\n", webhook.ID)
},
}
)
func init() {
cmdWebhooks.Flags().UintVarP(&listID, listIDLongFlag, listIDShortFlag, 0, "filter by listID")
cmdCreateWebhook.Flags().UintVarP(&listID, listIDLongFlag, listIDShortFlag, 0, "id of list to which webhook will belong")
cmdCreateWebhook.Flags().StringVar(&url, urlLongFlag, "", "url of webhook")
}
func webhook(cmd *cobra.Command, args []string) (wl.Webhook, error) {
if len(args) != 1 {
fmt.Printf("incorrect number of arguments provided\n\n")
cmd.Usage()
os.Exit(2)
}
idInt, err := strconv.Atoi(args[0])
if err != nil {
fmt.Printf("error parsing webhookID: %v\n\n", err)
cmd.Usage()
os.Exit(2)
}
id := uint(idInt)
return newClient(cmd).Webhook(id)
}