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"
|
||||
)
|
||||
@@ -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