Skip to content

Commit

Permalink
add delete
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbroks committed Nov 14, 2024
1 parent c281b3b commit 23bab64
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 123 deletions.
4 changes: 3 additions & 1 deletion cmd/ctrlc/root/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/create"
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/delete"
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/get"
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/upsert"
"github.com/spf13/cobra"
Expand All @@ -12,7 +13,7 @@ import (

func NewAPICmd() *cobra.Command {
cmd := &cobra.Command{
Use: "api <subcommand>",
Use: "api <action> <resource> [flags]",
Short: "API commands",
Long: `Commands for interacting with the CtrlPlane API.`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -41,6 +42,7 @@ func NewAPICmd() *cobra.Command {
cmd.AddCommand(get.NewGetCmd())
cmd.AddCommand(create.NewCreateCmd())
cmd.AddCommand(upsert.NewUpsertCmd())
cmd.AddCommand(delete.NewDeleteCmd())

return cmd
}
22 changes: 22 additions & 0 deletions cmd/ctrlc/root/api/delete/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package delete

import (
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/delete/resource"
"github.com/spf13/cobra"
)

func NewDeleteCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "delete <command>",
Short: "Delete resources",
Long: `Commands for deleting resources.`,
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
}


cmd.AddCommand(resource.NewDeleteResourceCmd())

return cmd
}
70 changes: 70 additions & 0 deletions cmd/ctrlc/root/api/delete/resource/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package resource

import (
"fmt"

"github.com/MakeNowJust/heredoc/v2"
"github.com/ctrlplanedev/cli/internal/api"
"github.com/ctrlplanedev/cli/internal/cliutil"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func NewDeleteResourceCmd() *cobra.Command {
var resourceId string
var workspace string
var identifier string

cmd := &cobra.Command{
Use: "resource [flags]",
Short: "Delete a resource",
Long: `Delete a resource by specifying either an ID or both a workspace and an identifier.`,
Example: heredoc.Doc(`
# Delete a resource by ID
$ ctrlc delete resource --id 123e4567-e89b-12d3-a456-426614174000
# Delete a resource by workspace and identifier
$ ctrlc delete resource --workspace 123e4567-e89b-12d3-a456-426614174000 --identifier myidentifier
# Delete a resource using Go template syntax
$ ctrlc delete resource --id 123e4567-e89b-12d3-a456-426614174000 --template='{{.id}}'
`),
RunE: func(cmd *cobra.Command, args []string) error {
if resourceId == "" && (workspace == "" || identifier == "") {
return fmt.Errorf("either --id or both --workspace and --identifier must be provided")
}

if resourceId != "" && (workspace != "" || identifier != "") {
return fmt.Errorf("--id and --workspace/--identifier are mutually exclusive")
}

apiURL := viper.GetString("url")
apiKey := viper.GetString("api-key")
client, err := api.NewAPIKeyClientWithResponses(apiURL, apiKey)
if err != nil {
return fmt.Errorf("failed to delete resource API client: %w", err)
}

if resourceId != "" {
resp, err := client.DeleteResource(cmd.Context(), resourceId)
if err != nil {
return fmt.Errorf("failed to delete resource by ID: %w", err)
}
return cliutil.HandleOutput(cmd, resp)
}

resp, err := client.DeleteResourceByIdentifier(cmd.Context(), workspace, identifier)
if err != nil {
return fmt.Errorf("failed to delete resource by workspace and identifier: %w", err)
}
return cliutil.HandleOutput(cmd, resp)
},
}

// Add flags
cmd.Flags().StringVar(&resourceId, "id", "", "ID of the target resource")
cmd.Flags().StringVar(&workspace, "workspace", "", "Workspace of the target resource")
cmd.Flags().StringVar(&identifier, "identifier", "", "Identifier of the target resource")

return cmd
}
47 changes: 34 additions & 13 deletions cmd/ctrlc/root/api/get/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,59 @@ import (

func NewGetResourceCmd() *cobra.Command {
var resourceId string
var workspace string
var identifier string

cmd := &cobra.Command{
Use: "target [flags]",
Short: "Get a target",
Long: `Get a target with the specified name and configuration.`,
Use: "resource [flags]",
Short: "Get a resource",
Long: `Get a resource by specifying either an ID or both a workspace and an identifier.`,
Example: heredoc.Doc(`
# Get a target
$ ctrlc get target --name my-target
# Get a resource by ID
$ ctrlc get resource --id 123e4567-e89b-12d3-a456-426614174000
# Get a target using Go template syntax
$ ctrlc get target --name my-target --template='{{.id}}'
`),
# Get a resource by workspace and identifier
$ ctrlc get resource --workspace myworkspace --identifier myidentifier
# Get a resource using Go template syntax
$ ctrlc get resource --id 123e4567-e89b-12d3-a456-426614174000 --template='{{.id}}'
`),
RunE: func(cmd *cobra.Command, args []string) error {
if resourceId == "" && (workspace == "" || identifier == "") {
return fmt.Errorf("either --id or both --workspace and --identifier must be provided")
}

if resourceId != "" && (workspace != "" || identifier != "") {
return fmt.Errorf("--id and --workspace/--identifier are mutually exclusive")
}

apiURL := viper.GetString("url")
apiKey := viper.GetString("api-key")
client, err := api.NewAPIKeyClientWithResponses(apiURL, apiKey)
if err != nil {
return fmt.Errorf("failed to create API client: %w", err)
}

resp, err := client.GetResource(cmd.Context(), resourceId)
if err != nil {
return fmt.Errorf("failed to get target: %w", err)
if resourceId != "" {
resp, err := client.GetResource(cmd.Context(), resourceId)
if err != nil {
return fmt.Errorf("failed to get resource by ID: %w", err)
}
return cliutil.HandleOutput(cmd, resp)
}

resp, err := client.GetResourceByIdentifier(cmd.Context(), workspace, identifier)
if err != nil {
return fmt.Errorf("failed to get resource by workspace and identifier: %w", err)
}
return cliutil.HandleOutput(cmd, resp)
},
}

// Add flags
cmd.Flags().StringVar(&resourceId, "id", "", "ID of the target (required)")
cmd.MarkFlagRequired("id")
cmd.Flags().StringVar(&resourceId, "id", "", "ID of the target resource")
cmd.Flags().StringVar(&workspace, "workspace", "", "Workspace of the target resource")
cmd.Flags().StringVar(&identifier, "identifier", "", "Identifier of the target resource")

return cmd
}
Loading

0 comments on commit 23bab64

Please sign in to comment.