Skip to content

Commit 697fc39

Browse files
authored
Merge pull request #1 from joinflux/feature/validate-webhook-types
Add validation and autocompletion for Trigger Types
2 parents 1a05fe3 + 3edcb8f commit 697fc39

File tree

1 file changed

+53
-17
lines changed

1 file changed

+53
-17
lines changed

cmd/create.go

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,6 @@ import (
1111
)
1212

1313
// 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-
//
3014
// See: https://developers.webflow.com/reference/create-webhook.
3115
type CreateResponse struct {
3216
CreatedOn string
@@ -36,19 +20,71 @@ type CreateResponse struct {
3620
TriggerType string
3721
}
3822

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+
3939
// createCmd represents the command to create a webhook for a site in Webflow.
4040
var createCmd = &cobra.Command{
4141
Use: "create [site_id] [trigger_type] [url]",
4242
Short: "create webhooks for a site",
4343
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'.\ntrigger_type must be one of: %+q\n", triggerType, TriggerTypes)
76+
},
4477
Run: func(cmd *cobra.Command, args []string) {
4578
siteId := args[0]
79+
triggerType := args[1]
80+
url := args[2]
81+
4682
c := internal.NewClient(ApiToken)
4783

4884
payload := strings.NewReader(fmt.Sprintf(`{
4985
"triggerType": "%s",
5086
"url": "%s"
51-
}`, args[1], args[2]))
87+
}`, triggerType, url))
5288

5389
body, err := c.Post([]string{"sites", siteId, "webhooks"}, payload)
5490

0 commit comments

Comments
 (0)