Added create and delete operations for projects and boards with validation and error handling
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"git.franklin.lab/steve.cliff/pcli/client"
|
||||
"git.franklin.lab/steve.cliff/pcli/output"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
@@ -56,11 +58,66 @@ var boardActionsCmd = &cobra.Command{
|
||||
},
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.franklin.lab/steve.cliff/pcli/model"
|
||||
)
|
||||
|
||||
// friendlyAPIError translates APIError status codes into human-readable messages with operation context.
|
||||
func friendlyAPIError(err error, operation string, permissionHint string) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check if it's an APIError
|
||||
apiErr, ok := err.(*model.APIError)
|
||||
if !ok {
|
||||
// Not an API error, return as-is
|
||||
return err
|
||||
}
|
||||
|
||||
switch apiErr.StatusCode {
|
||||
case 401:
|
||||
return fmt.Errorf("%s: authentication failed — check your API key", operation)
|
||||
case 403:
|
||||
if permissionHint != "" {
|
||||
return fmt.Errorf("%s: permission denied (%s)", operation, permissionHint)
|
||||
}
|
||||
return fmt.Errorf("%s: permission denied", operation)
|
||||
case 404:
|
||||
return fmt.Errorf("%s: not found — the resource may not exist or you may not have access to it", operation)
|
||||
default:
|
||||
// Unknown status code, return original error
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"git.franklin.lab/steve.cliff/pcli/client"
|
||||
"git.franklin.lab/steve.cliff/pcli/output"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
@@ -40,8 +42,67 @@ var projectGetCmd = &cobra.Command{
|
||||
},
|
||||
}
|
||||
|
||||
var projectCreateCmd = &cobra.Command{
|
||||
Use: "create",
|
||||
Short: "Create a new project",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
name, _ := cmd.Flags().GetString("name")
|
||||
projectType, _ := cmd.Flags().GetString("type")
|
||||
description, _ := cmd.Flags().GetString("description")
|
||||
|
||||
// Validate required flags
|
||||
if name == "" {
|
||||
return cmd.Usage()
|
||||
}
|
||||
if projectType == "" {
|
||||
return cmd.Usage()
|
||||
}
|
||||
|
||||
fields := client.ProjectCreateFields{
|
||||
Type: projectType,
|
||||
Name: name,
|
||||
}
|
||||
|
||||
if description != "" {
|
||||
fields.Description = &description
|
||||
}
|
||||
|
||||
project, err := getClient().CreateProject(getContext(), fields)
|
||||
if err != nil {
|
||||
return friendlyAPIError(err, "create project", "")
|
||||
}
|
||||
|
||||
return output.Print(project, getFormat(), os.Stdout)
|
||||
},
|
||||
}
|
||||
|
||||
var projectDeleteCmd = &cobra.Command{
|
||||
Use: "delete <id>",
|
||||
Short: "Delete a project",
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
err := getClient().DeleteProject(getContext(), args[0])
|
||||
if err != nil {
|
||||
return friendlyAPIError(err, "delete project", "requires project manager role")
|
||||
}
|
||||
|
||||
fmt.Println("Project deleted successfully")
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(projectCmd)
|
||||
projectCmd.AddCommand(projectListCmd)
|
||||
projectCmd.AddCommand(projectGetCmd)
|
||||
projectCmd.AddCommand(projectCreateCmd)
|
||||
projectCmd.AddCommand(projectDeleteCmd)
|
||||
|
||||
// Flags for project create
|
||||
projectCreateCmd.Flags().String("name", "", "Project name (required)")
|
||||
projectCreateCmd.Flags().String("type", "", "Project type (required, values: public/private)")
|
||||
projectCreateCmd.Flags().String("description", "", "Project description (optional)")
|
||||
|
||||
projectCreateCmd.MarkFlagRequired("name")
|
||||
projectCreateCmd.MarkFlagRequired("type")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user