| 36 | } |
| 37 | |
| 38 | func NewCmdCreateField(f *cmdutil.Factory, runF func(config createFieldConfig) error) *cobra.Command { |
| 39 | opts := createFieldOpts{} |
| 40 | createFieldCmd := &cobra.Command{ |
| 41 | Short: "Create a field in a project", |
| 42 | Use: "field-create [<number>]", |
| 43 | Example: heredoc.Doc(` |
| 44 | # Create a field in the current user's project "1" |
| 45 | $ gh project field-create 1 --owner "@me" --name "new field" --data-type "text" |
| 46 | |
| 47 | # Create a field with three options to select from for owner monalisa |
| 48 | $ gh project field-create 1 --owner monalisa --name "new field" --data-type "SINGLE_SELECT" --single-select-options "one,two,three" |
| 49 | `), |
| 50 | Args: cobra.MaximumNArgs(1), |
| 51 | RunE: func(cmd *cobra.Command, args []string) error { |
| 52 | client, err := client.New(f) |
| 53 | if err != nil { |
| 54 | return err |
| 55 | } |
| 56 | |
| 57 | if len(args) == 1 { |
| 58 | num, err := strconv.ParseInt(args[0], 10, 32) |
| 59 | if err != nil { |
| 60 | return cmdutil.FlagErrorf("invalid number: %v", args[0]) |
| 61 | } |
| 62 | opts.number = int32(num) |
| 63 | } |
| 64 | |
| 65 | config := createFieldConfig{ |
| 66 | client: client, |
| 67 | opts: opts, |
| 68 | io: f.IOStreams, |
| 69 | } |
| 70 | |
| 71 | if config.opts.dataType == "SINGLE_SELECT" && len(config.opts.singleSelectOptions) == 0 { |
| 72 | return fmt.Errorf("passing `--single-select-options` is required for SINGLE_SELECT data type") |
| 73 | } |
| 74 | |
| 75 | // allow testing of the command without actually running it |
| 76 | if runF != nil { |
| 77 | return runF(config) |
| 78 | } |
| 79 | return runCreateField(config) |
| 80 | }, |
| 81 | } |
| 82 | |
| 83 | createFieldCmd.Flags().StringVar(&opts.owner, "owner", "", "Login of the owner. Use \"@me\" for the current user.") |
| 84 | createFieldCmd.Flags().StringVar(&opts.name, "name", "", "Name of the new field") |
| 85 | cmdutil.StringEnumFlag(createFieldCmd, &opts.dataType, "data-type", "", "", []string{"TEXT", "SINGLE_SELECT", "DATE", "NUMBER"}, "DataType of the new field.") |
| 86 | createFieldCmd.Flags().StringSliceVar(&opts.singleSelectOptions, "single-select-options", []string{}, "Options for SINGLE_SELECT data type") |
| 87 | cmdutil.AddFormatFlags(createFieldCmd, &opts.exporter) |
| 88 | |
| 89 | _ = createFieldCmd.MarkFlagRequired("name") |
| 90 | _ = createFieldCmd.MarkFlagRequired("data-type") |
| 91 | |
| 92 | return createFieldCmd |
| 93 | } |
| 94 | |
| 95 | func runCreateField(config createFieldConfig) error { |