MCPcopy Create free account
hub / github.com/cli/cli / NewCmdCreate

Function NewCmdCreate

pkg/cmd/event/subscription/create/create.go:31–82  ·  view source on GitHub ↗
(f *cmdutil.Factory, runF func(*CreateOptions) error)

Source from the content-addressed store, hash-verified

29}
30
31func 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
84func createRun(ctx context.Context, opts *CreateOptions) error {
85 cfg, err := opts.Config()

Callers 1

TestNewCmdCreateFunction · 0.70

Calls 6

EventClientFuncFunction · 0.92
EventTypesHelpFunction · 0.92
FlagErrorfFunction · 0.92
AddJSONFlagsFunction · 0.92
createRunFunction · 0.70

Tested by 1

TestNewCmdCreateFunction · 0.56