114 lines
2.8 KiB
Go
114 lines
2.8 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"git.franklin.lab/steve.cliff/pcli/output"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var labelCmd = &cobra.Command{
|
|
Use: "label",
|
|
Short: "Manage labels",
|
|
Long: "Commands for managing Planka labels",
|
|
}
|
|
|
|
var labelCreateCmd = &cobra.Command{
|
|
Use: "create",
|
|
Short: "Create a new label",
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
boardId, _ := cmd.Flags().GetString("board")
|
|
name, _ := cmd.Flags().GetString("name")
|
|
|
|
if boardId == "" {
|
|
return fmt.Errorf("--board is required")
|
|
}
|
|
if name == "" {
|
|
return fmt.Errorf("--name is required")
|
|
}
|
|
|
|
color, _ := cmd.Flags().GetString("color")
|
|
if color == "" {
|
|
return fmt.Errorf("--color is required")
|
|
}
|
|
|
|
fields := map[string]any{
|
|
"name": name,
|
|
"color": color,
|
|
"position": float64(65536),
|
|
}
|
|
|
|
if pos, _ := cmd.Flags().GetFloat64("position"); cmd.Flags().Changed("position") {
|
|
fields["position"] = pos
|
|
}
|
|
|
|
label, err := getClient().CreateLabel(getContext(), boardId, fields)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return output.Print(label, getFormat(), os.Stdout)
|
|
},
|
|
}
|
|
|
|
var labelUpdateCmd = &cobra.Command{
|
|
Use: "update <id>",
|
|
Short: "Update a label",
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
fields := make(map[string]any)
|
|
|
|
if name, _ := cmd.Flags().GetString("name"); name != "" {
|
|
fields["name"] = name
|
|
}
|
|
if color, _ := cmd.Flags().GetString("color"); color != "" {
|
|
fields["color"] = color
|
|
}
|
|
if pos, _ := cmd.Flags().GetFloat64("position"); cmd.Flags().Changed("position") {
|
|
fields["position"] = pos
|
|
}
|
|
|
|
if len(fields) == 0 {
|
|
return fmt.Errorf("at least one field must be specified for update")
|
|
}
|
|
|
|
label, err := getClient().UpdateLabel(getContext(), args[0], fields)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return output.Print(label, getFormat(), os.Stdout)
|
|
},
|
|
}
|
|
|
|
var labelDeleteCmd = &cobra.Command{
|
|
Use: "delete <id>",
|
|
Short: "Delete a label",
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
err := getClient().DeleteLabel(getContext(), args[0])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return output.Print(map[string]string{"status": "deleted"}, getFormat(), os.Stdout)
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(labelCmd)
|
|
labelCmd.AddCommand(labelCreateCmd)
|
|
labelCmd.AddCommand(labelUpdateCmd)
|
|
labelCmd.AddCommand(labelDeleteCmd)
|
|
|
|
labelCreateCmd.Flags().String("board", "", "Board ID (required)")
|
|
labelCreateCmd.Flags().String("name", "", "Label name (required)")
|
|
labelCreateCmd.Flags().String("color", "", "Label color")
|
|
labelCreateCmd.Flags().Float64("position", 0, "Position")
|
|
|
|
labelUpdateCmd.Flags().String("name", "", "Label name")
|
|
labelUpdateCmd.Flags().String("color", "", "Label color")
|
|
labelUpdateCmd.Flags().Float64("position", 0, "Position")
|
|
}
|