Skip to content

Commit 23bab64

Browse files
committed
add delete
1 parent c281b3b commit 23bab64

File tree

5 files changed

+238
-123
lines changed

5 files changed

+238
-123
lines changed

Diff for: cmd/ctrlc/root/api/api.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55

66
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/create"
7+
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/delete"
78
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/get"
89
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/upsert"
910
"github.com/spf13/cobra"
@@ -12,7 +13,7 @@ import (
1213

1314
func NewAPICmd() *cobra.Command {
1415
cmd := &cobra.Command{
15-
Use: "api <subcommand>",
16+
Use: "api <action> <resource> [flags]",
1617
Short: "API commands",
1718
Long: `Commands for interacting with the CtrlPlane API.`,
1819
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
@@ -41,6 +42,7 @@ func NewAPICmd() *cobra.Command {
4142
cmd.AddCommand(get.NewGetCmd())
4243
cmd.AddCommand(create.NewCreateCmd())
4344
cmd.AddCommand(upsert.NewUpsertCmd())
45+
cmd.AddCommand(delete.NewDeleteCmd())
4446

4547
return cmd
4648
}

Diff for: cmd/ctrlc/root/api/delete/delete.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package delete
2+
3+
import (
4+
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/delete/resource"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
func NewDeleteCmd() *cobra.Command {
9+
cmd := &cobra.Command{
10+
Use: "delete <command>",
11+
Short: "Delete resources",
12+
Long: `Commands for deleting resources.`,
13+
RunE: func(cmd *cobra.Command, args []string) error {
14+
return cmd.Help()
15+
},
16+
}
17+
18+
19+
cmd.AddCommand(resource.NewDeleteResourceCmd())
20+
21+
return cmd
22+
}

Diff for: cmd/ctrlc/root/api/delete/resource/resource.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package resource
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/MakeNowJust/heredoc/v2"
7+
"github.com/ctrlplanedev/cli/internal/api"
8+
"github.com/ctrlplanedev/cli/internal/cliutil"
9+
"github.com/spf13/cobra"
10+
"github.com/spf13/viper"
11+
)
12+
13+
func NewDeleteResourceCmd() *cobra.Command {
14+
var resourceId string
15+
var workspace string
16+
var identifier string
17+
18+
cmd := &cobra.Command{
19+
Use: "resource [flags]",
20+
Short: "Delete a resource",
21+
Long: `Delete a resource by specifying either an ID or both a workspace and an identifier.`,
22+
Example: heredoc.Doc(`
23+
# Delete a resource by ID
24+
$ ctrlc delete resource --id 123e4567-e89b-12d3-a456-426614174000
25+
26+
# Delete a resource by workspace and identifier
27+
$ ctrlc delete resource --workspace 123e4567-e89b-12d3-a456-426614174000 --identifier myidentifier
28+
29+
# Delete a resource using Go template syntax
30+
$ ctrlc delete resource --id 123e4567-e89b-12d3-a456-426614174000 --template='{{.id}}'
31+
`),
32+
RunE: func(cmd *cobra.Command, args []string) error {
33+
if resourceId == "" && (workspace == "" || identifier == "") {
34+
return fmt.Errorf("either --id or both --workspace and --identifier must be provided")
35+
}
36+
37+
if resourceId != "" && (workspace != "" || identifier != "") {
38+
return fmt.Errorf("--id and --workspace/--identifier are mutually exclusive")
39+
}
40+
41+
apiURL := viper.GetString("url")
42+
apiKey := viper.GetString("api-key")
43+
client, err := api.NewAPIKeyClientWithResponses(apiURL, apiKey)
44+
if err != nil {
45+
return fmt.Errorf("failed to delete resource API client: %w", err)
46+
}
47+
48+
if resourceId != "" {
49+
resp, err := client.DeleteResource(cmd.Context(), resourceId)
50+
if err != nil {
51+
return fmt.Errorf("failed to delete resource by ID: %w", err)
52+
}
53+
return cliutil.HandleOutput(cmd, resp)
54+
}
55+
56+
resp, err := client.DeleteResourceByIdentifier(cmd.Context(), workspace, identifier)
57+
if err != nil {
58+
return fmt.Errorf("failed to delete resource by workspace and identifier: %w", err)
59+
}
60+
return cliutil.HandleOutput(cmd, resp)
61+
},
62+
}
63+
64+
// Add flags
65+
cmd.Flags().StringVar(&resourceId, "id", "", "ID of the target resource")
66+
cmd.Flags().StringVar(&workspace, "workspace", "", "Workspace of the target resource")
67+
cmd.Flags().StringVar(&identifier, "identifier", "", "Identifier of the target resource")
68+
69+
return cmd
70+
}

Diff for: cmd/ctrlc/root/api/get/resource/resource.go

+34-13
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,59 @@ import (
1212

1313
func NewGetResourceCmd() *cobra.Command {
1414
var resourceId string
15+
var workspace string
16+
var identifier string
1517

1618
cmd := &cobra.Command{
17-
Use: "target [flags]",
18-
Short: "Get a target",
19-
Long: `Get a target with the specified name and configuration.`,
19+
Use: "resource [flags]",
20+
Short: "Get a resource",
21+
Long: `Get a resource by specifying either an ID or both a workspace and an identifier.`,
2022
Example: heredoc.Doc(`
21-
# Get a target
22-
$ ctrlc get target --name my-target
23+
# Get a resource by ID
24+
$ ctrlc get resource --id 123e4567-e89b-12d3-a456-426614174000
2325
24-
# Get a target using Go template syntax
25-
$ ctrlc get target --name my-target --template='{{.id}}'
26-
`),
26+
# Get a resource by workspace and identifier
27+
$ ctrlc get resource --workspace myworkspace --identifier myidentifier
28+
29+
# Get a resource using Go template syntax
30+
$ ctrlc get resource --id 123e4567-e89b-12d3-a456-426614174000 --template='{{.id}}'
31+
`),
2732
RunE: func(cmd *cobra.Command, args []string) error {
33+
if resourceId == "" && (workspace == "" || identifier == "") {
34+
return fmt.Errorf("either --id or both --workspace and --identifier must be provided")
35+
}
36+
37+
if resourceId != "" && (workspace != "" || identifier != "") {
38+
return fmt.Errorf("--id and --workspace/--identifier are mutually exclusive")
39+
}
40+
2841
apiURL := viper.GetString("url")
2942
apiKey := viper.GetString("api-key")
3043
client, err := api.NewAPIKeyClientWithResponses(apiURL, apiKey)
3144
if err != nil {
3245
return fmt.Errorf("failed to create API client: %w", err)
3346
}
3447

35-
resp, err := client.GetResource(cmd.Context(), resourceId)
36-
if err != nil {
37-
return fmt.Errorf("failed to get target: %w", err)
48+
if resourceId != "" {
49+
resp, err := client.GetResource(cmd.Context(), resourceId)
50+
if err != nil {
51+
return fmt.Errorf("failed to get resource by ID: %w", err)
52+
}
53+
return cliutil.HandleOutput(cmd, resp)
3854
}
3955

56+
resp, err := client.GetResourceByIdentifier(cmd.Context(), workspace, identifier)
57+
if err != nil {
58+
return fmt.Errorf("failed to get resource by workspace and identifier: %w", err)
59+
}
4060
return cliutil.HandleOutput(cmd, resp)
4161
},
4262
}
4363

4464
// Add flags
45-
cmd.Flags().StringVar(&resourceId, "id", "", "ID of the target (required)")
46-
cmd.MarkFlagRequired("id")
65+
cmd.Flags().StringVar(&resourceId, "id", "", "ID of the target resource")
66+
cmd.Flags().StringVar(&workspace, "workspace", "", "Workspace of the target resource")
67+
cmd.Flags().StringVar(&identifier, "identifier", "", "Identifier of the target resource")
4768

4869
return cmd
4970
}

0 commit comments

Comments
 (0)