-
Notifications
You must be signed in to change notification settings - Fork 3
feat: add get/delete subcommand for interacting with custom resources. #69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c0816a5
feat: add get/delete subcommand for interacting with custom resources.
dacbd 7172d91
better handling of output flag, custom handling of 200 response from …
dacbd 3d2a1c5
update to use new search endpoints
dacbd 3cf8d82
update for search endpoint
dacbd 0332a81
add makefile entry
dacbd e9d1ab2
update make file
dacbd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,21 @@ | ||
| package delete | ||
|
|
||
| import ( | ||
| "github.com/ctrlplanedev/cli/cmd/ctrlc/root/delete/resource" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func NewDeleteCmd() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "delete <command>", | ||
| Short: "Delete resources and other objects", | ||
| Long: `Commands for deleting resources and other objects.`, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| return cmd.Help() | ||
| }, | ||
| } | ||
|
|
||
| cmd.AddCommand(resource.NewResourceCmd()) | ||
|
|
||
| return cmd | ||
| } |
This file contains hidden or 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,78 @@ | ||
| package resource | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/charmbracelet/log" | ||
| "github.com/ctrlplanedev/cli/internal/cliutil" | ||
| "github.com/ctrlplanedev/cli/internal/resources" | ||
| "github.com/spf13/cobra" | ||
| "github.com/spf13/viper" | ||
| ) | ||
|
|
||
| func NewResourceCmd() *cobra.Command { | ||
| var autoAccept bool | ||
| var output string | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "resource <identifier>", | ||
| Short: "Delete a resource by identifier", | ||
| Args: cobra.ExactArgs(1), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| cmdStart := time.Now() | ||
| defer func() { | ||
| log.Debug("delete resource completed", "duration", time.Since(cmdStart)) | ||
| }() | ||
|
|
||
| identifier := args[0] | ||
| log.Debug("delete resource", "identifier", identifier, "output", output, "autoAccept", autoAccept) | ||
|
|
||
| svc, err := resources.NewAPIResourceService(cmd.Context(), viper.GetString("url"), viper.GetString("api-key"), viper.GetString("workspace")) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| log.Debug("fetching resource before delete", "identifier", identifier) | ||
| resource, err := svc.GetByIdentifier(cmd.Context(), identifier) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| log.Debug("resource found", "identifier", resource.Identifier, "name", resource.Name, "kind", resource.Kind) | ||
|
|
||
| if output != "silent" { | ||
| if err := cliutil.HandleAnyOutput(cmd, resource, output); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| if !autoAccept { | ||
| message := fmt.Sprintf("Are you sure you want to delete resource %q?", identifier) | ||
| confirmed, err := cliutil.ConfirmAction(message) | ||
| if err != nil { | ||
| return fmt.Errorf("confirmation prompt failed: %w", err) | ||
| } | ||
| if !confirmed { | ||
| log.Debug("delete aborted by user") | ||
| fmt.Fprintln(cmd.ErrOrStderr(), "Aborted.") | ||
| return nil | ||
| } | ||
| log.Debug("delete confirmed by user") | ||
| } | ||
|
|
||
| log.Debug("deleting resource", "identifier", identifier) | ||
| result, err := svc.DeleteByIdentifier(cmd.Context(), identifier) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| log.Debug("delete accepted", "id", result.Id, "message", result.Message) | ||
|
|
||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().BoolVar(&autoAccept, "auto-accept", false, "Skip confirmation prompt") | ||
| cmd.Flags().StringVarP(&output, "output", "o", "silent", "Output format (json, yaml, or silent)") | ||
|
|
||
| return cmd | ||
| } |
This file contains hidden or 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,23 @@ | ||
| package get | ||
|
|
||
| import ( | ||
| "github.com/ctrlplanedev/cli/cmd/ctrlc/root/get/resource" | ||
| "github.com/ctrlplanedev/cli/cmd/ctrlc/root/get/resources" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func NewGetCmd() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "get <command>", | ||
| Short: "Get resources and other objects", | ||
| Long: `Commands for retrieving resources and other objects.`, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| return cmd.Help() | ||
| }, | ||
| } | ||
|
|
||
| cmd.AddCommand(resource.NewResourceCmd()) | ||
| cmd.AddCommand(resources.NewResourcesCmd()) | ||
|
|
||
| return cmd | ||
| } |
This file contains hidden or 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,43 @@ | ||
| package resource | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| "github.com/charmbracelet/log" | ||
| "github.com/ctrlplanedev/cli/internal/cliutil" | ||
| "github.com/ctrlplanedev/cli/internal/resources" | ||
| "github.com/spf13/cobra" | ||
| "github.com/spf13/viper" | ||
| ) | ||
|
|
||
| func NewResourceCmd() *cobra.Command { | ||
| var output string | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "resource <identifier>", | ||
| Short: "Get a single resource by identifier", | ||
| Args: cobra.ExactArgs(1), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| cmdStart := time.Now() | ||
| defer func() { | ||
| log.Debug("get resource completed", "duration", time.Since(cmdStart)) | ||
| }() | ||
|
|
||
| svc, err := resources.NewAPIResourceService(cmd.Context(), viper.GetString("url"), viper.GetString("api-key"), viper.GetString("workspace")) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| resource, err := svc.GetByIdentifier(cmd.Context(), args[0]) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return cliutil.HandleAnyOutput(cmd, resource, output) | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().StringVarP(&output, "output", "o", "json", "Output format (json or yaml)") | ||
|
|
||
| return cmd | ||
| } |
This file contains hidden or 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,88 @@ | ||
| package resources | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/charmbracelet/log" | ||
| "github.com/ctrlplanedev/cli/internal/api" | ||
| "github.com/ctrlplanedev/cli/internal/cliutil" | ||
| "github.com/ctrlplanedev/cli/internal/resources" | ||
| "github.com/spf13/cobra" | ||
| "github.com/spf13/viper" | ||
| ) | ||
|
|
||
| func NewResourcesCmd() *cobra.Command { | ||
| var kinds []string | ||
| var metadata []string | ||
| var versions []string | ||
| var providerIDs []string | ||
| var output string | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "resources", | ||
| Short: "Get resources", | ||
| Long: `Get resources with optional filtering.`, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| cmdStart := time.Now() | ||
| defer func() { | ||
| log.Debug("get resources completed", "duration", time.Since(cmdStart)) | ||
| }() | ||
|
|
||
| log.Debug("resources command", "kinds", kinds, "metadata", metadata, "versions", versions, "providerIDs", providerIDs) | ||
|
|
||
| if len(kinds) == 0 && len(metadata) == 0 && len(versions) == 0 && len(providerIDs) == 0 { | ||
| return fmt.Errorf("at least one filter is required (--kind, --metadata, --version, or --provider-id)") | ||
| } | ||
|
|
||
| svc, err := resources.NewAPIResourceService(cmd.Context(), viper.GetString("url"), viper.GetString("api-key"), viper.GetString("workspace")) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| filters := buildFilters(kinds, metadata, versions, providerIDs) | ||
|
|
||
| items, err := svc.Search(cmd.Context(), filters) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return cliutil.HandleAnyOutput(cmd, items, output) | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().StringSliceVarP(&kinds, "kind", "k", nil, "Filter by resource kind (repeatable)") | ||
| cmd.Flags().StringSliceVarP(&metadata, "metadata", "m", nil, "Filter by metadata key=value (repeatable)") | ||
| cmd.Flags().StringSliceVarP(&versions, "version", "v", nil, "Filter by resource version (repeatable)") | ||
| cmd.Flags().StringSliceVarP(&providerIDs, "provider-id", "p", nil, "Filter by provider ID (repeatable)") | ||
| cmd.Flags().StringVarP(&output, "output", "o", "json", "Output format (json or yaml)") | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func buildFilters(kinds, metadata, versions, providerIDs []string) api.ListResourcesFilters { | ||
| filters := api.ListResourcesFilters{} | ||
|
|
||
| if len(kinds) > 0 { | ||
| filters.Kinds = &kinds | ||
| } | ||
| if len(versions) > 0 { | ||
| filters.Versions = &versions | ||
| } | ||
| if len(providerIDs) > 0 { | ||
| filters.ProviderIds = &providerIDs | ||
| } | ||
| if len(metadata) > 0 { | ||
| m := make(map[string]string) | ||
| for _, kv := range metadata { | ||
| parts := strings.SplitN(kv, "=", 2) | ||
| if len(parts) == 2 { | ||
| m[parts[0]] = parts[1] | ||
| } | ||
| } | ||
| filters.Metadata = &m | ||
| } | ||
|
|
||
| return filters | ||
| } |
This file contains hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.