github.com/zaquestion/lab@v0.25.1/cmd/label_create.go (about)

     1  package cmd
     2  
     3  import (
     4  	"github.com/MakeNowJust/heredoc/v2"
     5  	"github.com/rsteube/carapace"
     6  	"github.com/spf13/cobra"
     7  	gitlab "github.com/xanzy/go-gitlab"
     8  	"github.com/zaquestion/lab/internal/action"
     9  	lab "github.com/zaquestion/lab/internal/gitlab"
    10  )
    11  
    12  var labelCreateCmd = &cobra.Command{
    13  	Use:     "create [remote] <name>",
    14  	Aliases: []string{"add"},
    15  	Short:   "Create a new label",
    16  	Example: heredoc.Doc(`
    17  		lab label create my-label
    18  		lab label create --color cornflowerblue --description "Blue as a cornflower" blue
    19  		lab label create --color #6495ed --description "Also blue as a cornflower" blue2`),
    20  	PersistentPreRun: labPersistentPreRun,
    21  	Args:             cobra.MinimumNArgs(1),
    22  	Run: func(cmd *cobra.Command, args []string) {
    23  		rn, name, err := parseArgsRemoteAndProject(args)
    24  		if err != nil {
    25  			log.Fatal(err)
    26  		}
    27  
    28  		color, err := cmd.Flags().GetString("color")
    29  		if err != nil {
    30  			log.Fatal(err)
    31  		}
    32  
    33  		desc, err := cmd.Flags().GetString("description")
    34  		if err != nil {
    35  			log.Fatal(err)
    36  		}
    37  
    38  		err = lab.LabelCreate(rn, &gitlab.CreateLabelOptions{
    39  			Name:        &name,
    40  			Description: &desc,
    41  			Color:       &color,
    42  		})
    43  
    44  		if err != nil {
    45  			log.Fatal(err)
    46  		}
    47  	},
    48  }
    49  
    50  func init() {
    51  	labelCreateCmd.Flags().String("color", "#428BCA", "color of the new label in HTML hex notation or CSS color name")
    52  	labelCreateCmd.Flags().String("description", "", "description of the new label")
    53  	labelCmd.AddCommand(labelCreateCmd)
    54  	carapace.Gen(labelCmd).PositionalCompletion(
    55  		action.Remotes(),
    56  	)
    57  }