|
| 1 | +package webhooks |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + atomickeys "atomicgo.dev/keyboard/keys" |
| 7 | + "github.com/pterm/pterm" |
| 8 | + "github.com/urfave/cli/v2" |
| 9 | + |
| 10 | + "github.com/NodeOps-app/createos-cli/internal/api" |
| 11 | + "github.com/NodeOps-app/createos-cli/internal/output" |
| 12 | + "github.com/NodeOps-app/createos-cli/internal/terminal" |
| 13 | +) |
| 14 | + |
| 15 | +func newWebhooksCreateCommand() *cli.Command { |
| 16 | + return &cli.Command{ |
| 17 | + Name: "create", |
| 18 | + Usage: "Create a new webhook endpoint", |
| 19 | + Description: `Create a webhook endpoint that receives event notifications via HTTP POST. |
| 20 | +
|
| 21 | +Examples: |
| 22 | + # Subscribe to all events: |
| 23 | + createos webhooks create --url https://example.com/webhook |
| 24 | +
|
| 25 | + # Subscribe to specific events: |
| 26 | + createos webhooks create --url https://example.com/webhook \ |
| 27 | + --event sandbox.create --event sandbox.destroy |
| 28 | +
|
| 29 | + # Interactive mode (TTY only): |
| 30 | + createos webhooks create`, |
| 31 | + Flags: []cli.Flag{ |
| 32 | + &cli.StringFlag{Name: "url", Usage: "HTTPS URL to receive webhook events"}, |
| 33 | + &cli.StringSliceFlag{Name: "event", Usage: "Event to subscribe to (repeatable, omit for all events)"}, |
| 34 | + }, |
| 35 | + Action: func(c *cli.Context) error { |
| 36 | + client, ok := c.App.Metadata[api.ClientKey].(*api.APIClient) |
| 37 | + if !ok { |
| 38 | + return fmt.Errorf("you're not signed in — run 'createos login' to get started") |
| 39 | + } |
| 40 | + |
| 41 | + webhookURL := c.String("url") |
| 42 | + events := c.StringSlice("event") |
| 43 | + |
| 44 | + if !terminal.IsInteractive() { |
| 45 | + if webhookURL == "" { |
| 46 | + return fmt.Errorf("please provide a URL with --url") |
| 47 | + } |
| 48 | + } else { |
| 49 | + if webhookURL == "" { |
| 50 | + var err error |
| 51 | + webhookURL, err = pterm.DefaultInteractiveTextInput. |
| 52 | + WithDefaultText("Webhook URL"). |
| 53 | + Show() |
| 54 | + if err != nil { |
| 55 | + return fmt.Errorf("could not read URL: %w", err) |
| 56 | + } |
| 57 | + } |
| 58 | + if len(events) == 0 { |
| 59 | + actions, err := client.ListWebhookActions() |
| 60 | + if err != nil { |
| 61 | + return fmt.Errorf("could not load available events: %w", err) |
| 62 | + } |
| 63 | + |
| 64 | + fmt.Println("Select events to subscribe to (leave empty for all events):") |
| 65 | + selected, selErr := pterm.DefaultInteractiveMultiselect. |
| 66 | + WithOptions(actions). |
| 67 | + WithDefaultText("Events"). |
| 68 | + WithFilter(false). |
| 69 | + WithKeySelect(atomickeys.Space). |
| 70 | + WithKeyConfirm(atomickeys.Enter). |
| 71 | + WithCheckmark(&pterm.Checkmark{Checked: "x", Unchecked: " "}). |
| 72 | + Show() |
| 73 | + if selErr != nil { |
| 74 | + return fmt.Errorf("could not read selection: %w", selErr) |
| 75 | + } |
| 76 | + events = selected |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + if webhookURL == "" { |
| 81 | + return fmt.Errorf("URL cannot be empty") |
| 82 | + } |
| 83 | + |
| 84 | + req := api.CreateWebhookEndpointRequest{ |
| 85 | + URL: webhookURL, |
| 86 | + Events: events, |
| 87 | + } |
| 88 | + |
| 89 | + result, err := client.CreateWebhookEndpoint(req) |
| 90 | + if err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + |
| 94 | + if output.IsJSON(c) { |
| 95 | + output.Render(c, result, func() {}) |
| 96 | + return nil |
| 97 | + } |
| 98 | + |
| 99 | + pterm.Success.Printf("Webhook endpoint created. ID: %s\n", result.ID) |
| 100 | + fmt.Println() |
| 101 | + label := pterm.NewStyle(pterm.FgCyan) |
| 102 | + fmt.Printf("%s %s\n", label.Sprint("Secret:"), result.Secret) |
| 103 | + fmt.Println() |
| 104 | + pterm.Println(pterm.Gray(" Save this secret — it won't be shown again.")) |
| 105 | + pterm.Println(pterm.Gray(" Use it to verify webhook signatures (X-Webhook-Signature header).")) |
| 106 | + |
| 107 | + return nil |
| 108 | + }, |
| 109 | + } |
| 110 | +} |
0 commit comments