-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
238 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.