Skip to content

Commit f3b40f7

Browse files
committed
feat: add get resources list
1 parent c22bab5 commit f3b40f7

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

cmd/ctrlc/root/api/get/get.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package get
22

33
import (
44
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/get/resource"
5+
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/get/resources"
56
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/get/system"
67
"github.com/spf13/cobra"
78
)
@@ -18,6 +19,7 @@ func NewGetCmd() *cobra.Command {
1819

1920
cmd.AddCommand(resource.NewGetResourceCmd())
2021
cmd.AddCommand(system.NewGetSystemCmd())
22+
cmd.AddCommand(resources.NewGetResourcesCmd())
2123

2224
return cmd
2325
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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

Comments
 (0)