| 35 | } |
| 36 | |
| 37 | func NewCmdCreateItem(f *cmdutil.Factory, runF func(config createItemConfig) error) *cobra.Command { |
| 38 | opts := createItemOpts{} |
| 39 | createItemCmd := &cobra.Command{ |
| 40 | Short: "Create a draft issue item in a project", |
| 41 | Use: "item-create [<number>]", |
| 42 | Example: heredoc.Doc(` |
| 43 | # Create a draft issue in the current user's project "1" |
| 44 | $ gh project item-create 1 --owner "@me" --title "new item" --body "new item body" |
| 45 | `), |
| 46 | Args: cobra.MaximumNArgs(1), |
| 47 | RunE: func(cmd *cobra.Command, args []string) error { |
| 48 | client, err := client.New(f) |
| 49 | if err != nil { |
| 50 | return err |
| 51 | } |
| 52 | |
| 53 | if len(args) == 1 { |
| 54 | num, err := strconv.ParseInt(args[0], 10, 32) |
| 55 | if err != nil { |
| 56 | return cmdutil.FlagErrorf("invalid number: %v", args[0]) |
| 57 | } |
| 58 | opts.number = int32(num) |
| 59 | } |
| 60 | |
| 61 | config := createItemConfig{ |
| 62 | client: client, |
| 63 | opts: opts, |
| 64 | io: f.IOStreams, |
| 65 | } |
| 66 | |
| 67 | // allow testing of the command without actually running it |
| 68 | if runF != nil { |
| 69 | return runF(config) |
| 70 | } |
| 71 | return runCreateItem(config) |
| 72 | }, |
| 73 | } |
| 74 | |
| 75 | createItemCmd.Flags().StringVar(&opts.owner, "owner", "", "Login of the owner. Use \"@me\" for the current user.") |
| 76 | createItemCmd.Flags().StringVar(&opts.title, "title", "", "Title for the draft issue") |
| 77 | createItemCmd.Flags().StringVar(&opts.body, "body", "", "Body for the draft issue") |
| 78 | cmdutil.AddFormatFlags(createItemCmd, &opts.exporter) |
| 79 | |
| 80 | _ = createItemCmd.MarkFlagRequired("title") |
| 81 | |
| 82 | return createItemCmd |
| 83 | } |
| 84 | |
| 85 | func runCreateItem(config createItemConfig) error { |
| 86 | canPrompt := config.io.CanPrompt() |