|
| 1 | +package resources |
| 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 NewGetResourcesCmd() *cobra.Command { |
| 14 | + var workspace string |
| 15 | + |
| 16 | + cmd := &cobra.Command{ |
| 17 | + Use: "resource [flags]", |
| 18 | + Short: "Get a resource", |
| 19 | + Long: `Get a resource by specifying either an ID or both a workspace and an identifier.`, |
| 20 | + Example: heredoc.Doc(` |
| 21 | + # Get a resource by workspace and identifier |
| 22 | + $ ctrlc get resource --workspace 00000000-0000-0000-0000-000000000000 |
| 23 | + `), |
| 24 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 25 | + apiURL := viper.GetString("url") |
| 26 | + apiKey := viper.GetString("api-key") |
| 27 | + |
| 28 | + client, err := api.NewAPIKeyClientWithResponses(apiURL, apiKey) |
| 29 | + if err != nil { |
| 30 | + return fmt.Errorf("failed to create API client: %w", err) |
| 31 | + } |
| 32 | + |
| 33 | + resp, err := client.ListResources(cmd.Context(), workspace) |
| 34 | + if err != nil { |
| 35 | + return fmt.Errorf("failed to get resource by workspace and identifier: %w", err) |
| 36 | + } |
| 37 | + |
| 38 | + return cliutil.HandleResponseOutput(cmd, resp) |
| 39 | + }, |
| 40 | + } |
| 41 | + |
| 42 | + // Add flags |
| 43 | + cmd.Flags().StringVar(&workspace, "workspace", "", "Workspace of the target resource") |
| 44 | + |
| 45 | + return cmd |
| 46 | +} |
0 commit comments