| 27 | } |
| 28 | |
| 29 | func NewCmdDelete(f *cmdutil.Factory, runF func(*DeleteOptions) error) *cobra.Command { |
| 30 | opts := DeleteOptions{ |
| 31 | IO: f.IOStreams, |
| 32 | Config: f.Config, |
| 33 | HttpClient: f.HttpClient, |
| 34 | Prompter: f.Prompter, |
| 35 | } |
| 36 | |
| 37 | cmd := &cobra.Command{ |
| 38 | Use: "delete {<id> | <url>}", |
| 39 | Short: "Delete a gist", |
| 40 | Long: heredoc.Docf(` |
| 41 | Delete a GitHub gist. |
| 42 | |
| 43 | To delete a gist interactively, use %[1]sgh gist delete%[1]s with no arguments. |
| 44 | |
| 45 | To delete a gist non-interactively, supply the gist id or url. |
| 46 | `, "`"), |
| 47 | Example: heredoc.Doc(` |
| 48 | # Delete a gist interactively |
| 49 | $ gh gist delete |
| 50 | |
| 51 | # Delete a gist non-interactively |
| 52 | $ gh gist delete 1234 |
| 53 | `), |
| 54 | Args: cobra.MaximumNArgs(1), |
| 55 | RunE: func(c *cobra.Command, args []string) error { |
| 56 | if !opts.IO.CanPrompt() && !opts.Confirmed { |
| 57 | return cmdutil.FlagErrorf("--yes required when not running interactively") |
| 58 | } |
| 59 | |
| 60 | if !opts.IO.CanPrompt() && len(args) == 0 { |
| 61 | return cmdutil.FlagErrorf("id or url argument required in non-interactive mode") |
| 62 | } |
| 63 | |
| 64 | if len(args) == 1 { |
| 65 | opts.Selector = args[0] |
| 66 | } |
| 67 | |
| 68 | if runF != nil { |
| 69 | return runF(&opts) |
| 70 | } |
| 71 | return deleteRun(&opts) |
| 72 | }, |
| 73 | } |
| 74 | cmd.Flags().BoolVar(&opts.Confirmed, "yes", false, "Confirm deletion without prompting") |
| 75 | return cmd |
| 76 | } |
| 77 | |
| 78 | func deleteRun(opts *DeleteOptions) error { |
| 79 | client, err := opts.HttpClient() |