NewCmdEdit returns a cobra command for editing a GitHub Discussion.
(f *cmdutil.Factory, runF func(*EditOptions) error)
| 38 | |
| 39 | // NewCmdEdit returns a cobra command for editing a GitHub Discussion. |
| 40 | func NewCmdEdit(f *cmdutil.Factory, runF func(*EditOptions) error) *cobra.Command { |
| 41 | opts := &EditOptions{ |
| 42 | IO: f.IOStreams, |
| 43 | Prompter: f.Prompter, |
| 44 | Client: shared.DiscussionClientFunc(f), |
| 45 | } |
| 46 | |
| 47 | cmd := &cobra.Command{ |
| 48 | Use: "edit {<number> | <discussion-url>} [flags]", |
| 49 | Short: "Edit a discussion (preview)", |
| 50 | Long: heredoc.Doc(` |
| 51 | Edit a GitHub Discussion. |
| 52 | |
| 53 | Without flags, the command runs interactively when connected to a terminal. |
| 54 | Use flags to update specific fields non-interactively. |
| 55 | `), |
| 56 | Example: heredoc.Doc(` |
| 57 | # Edit interactively |
| 58 | $ gh discussion edit 123 |
| 59 | |
| 60 | # Update title, body, and category |
| 61 | $ gh discussion edit 123 --title "Updated title" --body "Updated body" --category "Ideas" |
| 62 | |
| 63 | # Update body from a file |
| 64 | $ gh discussion edit 123 --body-file body.md |
| 65 | |
| 66 | # Add and remove labels |
| 67 | $ gh discussion edit 123 --add-label "bug,help wanted" --remove-label "stale" |
| 68 | `), |
| 69 | Args: cobra.ExactArgs(1), |
| 70 | RunE: func(cmd *cobra.Command, args []string) error { |
| 71 | number, repo, err := shared.ParseDiscussionArg(args[0]) |
| 72 | if err != nil { |
| 73 | return cmdutil.FlagErrorWrap(err) |
| 74 | } |
| 75 | |
| 76 | if repo != nil { |
| 77 | opts.BaseRepo = func() (ghrepo.Interface, error) { |
| 78 | return repo, nil |
| 79 | } |
| 80 | } else { |
| 81 | opts.BaseRepo = f.BaseRepo |
| 82 | } |
| 83 | |
| 84 | opts.DiscussionNumber = number |
| 85 | |
| 86 | if err := cmdutil.MutuallyExclusive("specify only one of --body or --body-file", |
| 87 | cmd.Flags().Changed("body"), cmd.Flags().Changed("body-file")); err != nil { |
| 88 | return err |
| 89 | } |
| 90 | |
| 91 | opts.TitleProvided = cmd.Flags().Changed("title") |
| 92 | opts.BodyProvided = cmd.Flags().Changed("body") || cmd.Flags().Changed("body-file") |
| 93 | opts.CategoryProvided = cmd.Flags().Changed("category") |
| 94 | opts.LabelsProvided = len(opts.AddLabels) > 0 || len(opts.RemoveLabels) > 0 |
| 95 | |
| 96 | noFlagsSet := !opts.TitleProvided && !opts.BodyProvided && !opts.CategoryProvided && !opts.LabelsProvided |
| 97 | if noFlagsSet && !opts.IO.CanPrompt() { |