MCPcopy
hub / github.com/cli/cli / NewCmdCreate

Function NewCmdCreate

pkg/cmd/discussion/create/create.go:32–96  ·  view source on GitHub ↗

NewCmdCreate returns a cobra command for creating a GitHub Discussion.

(f *cmdutil.Factory, runF func(*CreateOptions) error)

Source from the content-addressed store, hash-verified

30
31// NewCmdCreate returns a cobra command for creating a GitHub Discussion.
32func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Command {
33 opts := &CreateOptions{
34 IO: f.IOStreams,
35 Prompter: f.Prompter,
36 Client: shared.DiscussionClientFunc(f),
37 }
38
39 cmd := &cobra.Command{
40 Use: "create [flags]",
41 Short: "Create a new discussion (preview)",
42 Long: heredoc.Docf(`
43 Create a new GitHub Discussion in a repository.
44
45 With %[1]s--title%[1]s, %[1]s--body%[1]s (or %[1]s--body-file%[1]s), and %[1]s--category%[1]s, a discussion is created non-interactively.
46 Omitting any of these flags triggers interactive prompts when connected to a terminal.
47 `, "`"),
48 Example: heredoc.Doc(`
49 # Create interactively
50 $ gh discussion create
51
52 # Create non-interactively
53 $ gh discussion create --title "My question" --category "Q&A" --body "Details here"
54 `),
55 Args: cmdutil.NoArgsQuoteReminder,
56 RunE: func(cmd *cobra.Command, args []string) error {
57 opts.BaseRepo = f.BaseRepo
58
59 if err := cmdutil.MutuallyExclusive("specify only one of --body or --body-file",
60 cmd.Flags().Changed("body"), cmd.Flags().Changed("body-file")); err != nil {
61 return err
62 }
63
64 if opts.Title != "" && strings.TrimSpace(opts.Title) == "" {
65 return cmdutil.FlagErrorf("title cannot be blank")
66 }
67 if opts.Body != "" && strings.TrimSpace(opts.Body) == "" {
68 return cmdutil.FlagErrorf("body cannot be blank")
69 }
70 if opts.Category != "" && strings.TrimSpace(opts.Category) == "" {
71 return cmdutil.FlagErrorf("category cannot be blank")
72 }
73
74 bodyProvided := cmd.Flags().Changed("body") || cmd.Flags().Changed("body-file")
75 needsInput := opts.Title == "" || opts.Category == "" || !bodyProvided
76 if needsInput && !opts.IO.CanPrompt() {
77 return cmdutil.FlagErrorf("--title, --body (or --body-file), and --category are required when not running interactively")
78 }
79
80 if runF != nil {
81 return runF(opts)
82 }
83 return createRun(opts)
84 },
85 }
86
87 cmdutil.EnableRepoOverride(cmd, f)
88
89 cmd.Flags().StringVarP(&opts.Title, "title", "t", "", "Title for the discussion")

Callers 1

TestNewCmdCreateFunction · 0.70

Calls 6

DiscussionClientFuncFunction · 0.92
MutuallyExclusiveFunction · 0.92
FlagErrorfFunction · 0.92
EnableRepoOverrideFunction · 0.92
CanPromptMethod · 0.80
createRunFunction · 0.70

Tested by 1

TestNewCmdCreateFunction · 0.56