| 29 | } |
| 30 | |
| 31 | func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Command { |
| 32 | opts := &CreateOptions{ |
| 33 | IO: f.IOStreams, |
| 34 | Config: f.Config, |
| 35 | EventClient: shared.EventClientFunc(f), |
| 36 | Now: time.Now, |
| 37 | } |
| 38 | |
| 39 | cmd := &cobra.Command{ |
| 40 | Use: "create", |
| 41 | Short: "Create an event subscription (preview)", |
| 42 | Long: heredoc.Docf(` |
| 43 | Create a subscription to events of the given types on a resource, without listening to it. |
| 44 | |
| 45 | Pass the resource URL with %[1]s--resource%[1]s and the event types with %[1]s--event%[1]s, repeating the flag |
| 46 | or separating types with commas: |
| 47 | |
| 48 | %[2]s |
| 49 | |
| 50 | Creating a subscription is idempotent: if an active subscription for the same resource and |
| 51 | event types exists, it is returned instead. |
| 52 | |
| 53 | Once successful, the command prints the subscription ID to stdout. Use |
| 54 | %[1]sgh event listen --subscription%[1]s to receive its events. To create a subscription and listen |
| 55 | to it in one step, use %[1]sgh event listen --resource%[1]s instead. |
| 56 | `, "`", shared.EventTypesHelp()), |
| 57 | Example: heredoc.Doc(` |
| 58 | # Subscribe to reviews and state changes of a pull request |
| 59 | $ gh event subscription create --resource https://github.com/OWNER/REPO/pull/123 --event com.github.pull_request.review.submitted.v1,com.github.pull_request.state.changed.v1 |
| 60 | `), |
| 61 | Args: cobra.NoArgs, |
| 62 | RunE: func(cmd *cobra.Command, args []string) error { |
| 63 | if opts.Resource == "" { |
| 64 | return cmdutil.FlagErrorf("`--resource` is required") |
| 65 | } |
| 66 | if len(opts.Events) == 0 { |
| 67 | return cmdutil.FlagErrorf("`--event` is required") |
| 68 | } |
| 69 | |
| 70 | if runF != nil { |
| 71 | return runF(opts) |
| 72 | } |
| 73 | return createRun(cmd.Context(), opts) |
| 74 | }, |
| 75 | } |
| 76 | |
| 77 | cmd.Flags().StringVar(&opts.Resource, "resource", "", "URL of the resource to subscribe to") |
| 78 | cmd.Flags().StringSliceVar(&opts.Events, "event", nil, "Event `type` to subscribe to; repeat or separate with commas for more than one") |
| 79 | shared.RegisterEventTypesCompletion(cmd, "event") |
| 80 | cmdutil.AddJSONFlags(cmd, &opts.Exporter, client.SubscriptionFields) |
| 81 | return cmd |
| 82 | } |
| 83 | |
| 84 | func createRun(ctx context.Context, opts *CreateOptions) error { |
| 85 | cfg, err := opts.Config() |