159 lines
3.8 KiB
Go
159 lines
3.8 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"git.franklin.lab/steve.cliff/pcli/client"
|
|
"git.franklin.lab/steve.cliff/pcli/output"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var boardCmd = &cobra.Command{
|
|
Use: "board",
|
|
Short: "Manage boards",
|
|
Long: "Commands for managing Planka boards",
|
|
}
|
|
|
|
func resolveProjectNameToID(projectName string) (string, error) {
|
|
projects, err := getClient().ListProjects(getContext())
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
for _, project := range projects {
|
|
if strings.EqualFold(project.Name, projectName) {
|
|
return project.ID, nil
|
|
}
|
|
}
|
|
|
|
return "", fmt.Errorf("project not found: %s", projectName)
|
|
}
|
|
|
|
var boardListCmd = &cobra.Command{
|
|
Use: "list",
|
|
Short: "List all accessible boards",
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
projectName, _ := cmd.Flags().GetString("project")
|
|
|
|
boards, err := getClient().ListBoards(getContext())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if projectName != "" {
|
|
projectID, err := resolveProjectNameToID(projectName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var filteredBoards []interface{}
|
|
for _, board := range boards {
|
|
if board.ProjectID == projectID {
|
|
filteredBoards = append(filteredBoards, board)
|
|
}
|
|
}
|
|
return output.Print(filteredBoards, getFormat(), os.Stdout)
|
|
}
|
|
|
|
return output.Print(boards, getFormat(), os.Stdout)
|
|
},
|
|
}
|
|
|
|
var boardGetCmd = &cobra.Command{
|
|
Use: "get <id>",
|
|
Short: "Get a board by ID",
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
board, err := getClient().GetBoard(getContext(), args[0])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return output.Print(board, getFormat(), os.Stdout)
|
|
},
|
|
}
|
|
|
|
var boardActionsCmd = &cobra.Command{
|
|
Use: "actions <id>",
|
|
Short: "List actions for a board",
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
limit, _ := cmd.Flags().GetInt("limit")
|
|
|
|
actions, err := getClient().ListBoardActions(getContext(), args[0], limit)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return output.Print(actions, getFormat(), os.Stdout)
|
|
},
|
|
}
|
|
|
|
var boardCreateCmd = &cobra.Command{
|
|
Use: "create",
|
|
Short: "Create a new board",
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
project, _ := cmd.Flags().GetString("project")
|
|
name, _ := cmd.Flags().GetString("name")
|
|
position, _ := cmd.Flags().GetFloat64("position")
|
|
|
|
// Validate required flags
|
|
if project == "" {
|
|
return cmd.Usage()
|
|
}
|
|
if name == "" {
|
|
return cmd.Usage()
|
|
}
|
|
|
|
fields := client.BoardCreateFields{
|
|
Name: name,
|
|
Position: position,
|
|
}
|
|
|
|
board, err := getClient().CreateBoard(getContext(), project, fields)
|
|
if err != nil {
|
|
return friendlyAPIError(err, "create board", "requires project manager role")
|
|
}
|
|
|
|
return output.Print(board, getFormat(), os.Stdout)
|
|
},
|
|
}
|
|
|
|
var boardDeleteCmd = &cobra.Command{
|
|
Use: "delete <id>",
|
|
Short: "Delete a board",
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
err := getClient().DeleteBoard(getContext(), args[0])
|
|
if err != nil {
|
|
return friendlyAPIError(err, "delete board", "requires project manager role")
|
|
}
|
|
|
|
fmt.Println("Board deleted successfully")
|
|
return nil
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(boardCmd)
|
|
boardCmd.AddCommand(boardListCmd)
|
|
boardCmd.AddCommand(boardGetCmd)
|
|
boardCmd.AddCommand(boardActionsCmd)
|
|
boardCmd.AddCommand(boardCreateCmd)
|
|
boardCmd.AddCommand(boardDeleteCmd)
|
|
|
|
boardListCmd.Flags().String("project", "", "Filter boards by project name")
|
|
|
|
boardActionsCmd.Flags().Int("limit", 0, "Limit number of actions (0 = no limit)")
|
|
|
|
// Flags for board create
|
|
boardCreateCmd.Flags().String("project", "", "Project ID (required)")
|
|
boardCreateCmd.Flags().String("name", "", "Board name (required)")
|
|
boardCreateCmd.Flags().Float64("position", 65536, "Board position (optional, default 65536)")
|
|
|
|
boardCreateCmd.MarkFlagRequired("project")
|
|
boardCreateCmd.MarkFlagRequired("name")
|
|
}
|