@@ -11,22 +11,6 @@ import (
11
11
)
12
12
13
13
// CreateResponse represents a response to the create request in Webflow.
14
- //
15
- // The following are available trigger types:
16
- //
17
- // - form_submission
18
- // - site_publish
19
- // - ecomm_new_order
20
- // - ecomm_order_changed
21
- // - ecomm_inventory_changed
22
- // - memberships_user_account_added
23
- // - memberships_user_account_updated
24
- // - memberships_user_account_deleted
25
- // - collection_item_created
26
- // - collection_item_changed
27
- // - collection_item_deleted
28
- // - collection_item_unpublished
29
- //
30
14
// See: https://developers.webflow.com/reference/create-webhook.
31
15
type CreateResponse struct {
32
16
CreatedOn string
@@ -36,19 +20,71 @@ type CreateResponse struct {
36
20
TriggerType string
37
21
}
38
22
23
+ // TriggerTypes is a list of all available trigger types that can be created in Webflow.
24
+ var TriggerTypes = []string {
25
+ "form_submission" ,
26
+ "site_publish" ,
27
+ "ecomm_new_order" ,
28
+ "ecomm_order_changed" ,
29
+ "ecomm_inventory_changed" ,
30
+ "memberships_user_account_added" ,
31
+ "memberships_user_account_updated" ,
32
+ "memberships_user_account_deleted" ,
33
+ "collection_item_created" ,
34
+ "collection_item_changed" ,
35
+ "collection_item_deleted" ,
36
+ "collection_item_unpublished" ,
37
+ }
38
+
39
39
// createCmd represents the command to create a webhook for a site in Webflow.
40
40
var createCmd = & cobra.Command {
41
41
Use : "create [site_id] [trigger_type] [url]" ,
42
42
Short : "create webhooks for a site" ,
43
43
Args : cobra .ExactArgs (3 ),
44
+ ValidArgsFunction : func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
45
+ if len (args ) == 1 {
46
+ var candidates = []string {}
47
+ // for autocompletion, we will suggest anything that contains the string
48
+ // we are typing regardless of where in the string we match.
49
+ // For example, if someone types: "item"
50
+ // We will suggest:
51
+ // - "collection_item_created"
52
+ // - "collection_item_changed"
53
+ // - "collection_item_deleted"
54
+ // - "collection_item_unpublished"
55
+ for _ , value := range TriggerTypes {
56
+ if strings .Contains (value , toComplete ) {
57
+ candidates = append (candidates , value )
58
+ }
59
+ }
60
+ // if there are no valid suggestions, suggest all available options
61
+ if len (candidates ) == 0 {
62
+ candidates = TriggerTypes
63
+ }
64
+ return candidates , cobra .ShellCompDirectiveNoFileComp
65
+ }
66
+ return nil , cobra .ShellCompDirectiveError
67
+ },
68
+ PreRunE : func (cmd * cobra.Command , args []string ) error {
69
+ triggerType := args [1 ]
70
+ for _ , b := range TriggerTypes {
71
+ if b == triggerType {
72
+ return nil
73
+ }
74
+ }
75
+ return fmt .Errorf ("unknown Trigger Type: '%s'.\n trigger_type must be one of: %+q\n " , triggerType , TriggerTypes )
76
+ },
44
77
Run : func (cmd * cobra.Command , args []string ) {
45
78
siteId := args [0 ]
79
+ triggerType := args [1 ]
80
+ url := args [2 ]
81
+
46
82
c := internal .NewClient (ApiToken )
47
83
48
84
payload := strings .NewReader (fmt .Sprintf (`{
49
85
"triggerType": "%s",
50
86
"url": "%s"
51
- }` , args [ 1 ], args [ 2 ] ))
87
+ }` , triggerType , url ))
52
88
53
89
body , err := c .Post ([]string {"sites" , siteId , "webhooks" }, payload )
54
90
0 commit comments