1+ package resources
2+
3+ import (
4+ "fmt"
5+
6+ "github.com/ctrlplanedev/cli/internal/api"
7+ "github.com/ctrlplanedev/cli/internal/cliutil"
8+ "github.com/spf13/cobra"
9+ "github.com/spf13/viper"
10+ )
11+
12+ func NewResourcesCmd () * cobra.Command {
13+ var query string
14+ var limit int
15+ var offset int
16+ var workspace string
17+
18+ cmd := & cobra.Command {
19+ Use : "resources" ,
20+ Short : "Get resources" ,
21+ Long : `Commands for getting resources.` ,
22+ RunE : func (cmd * cobra.Command , args []string ) error {
23+ apiURL := viper .GetString ("url" )
24+ apiKey := viper .GetString ("api-key" )
25+ client , err := api .NewAPIKeyClientWithResponses (apiURL , apiKey )
26+ if err != nil {
27+ return fmt .Errorf ("failed to create API client: %w" , err )
28+ }
29+
30+ workspaceID := client .GetWorkspaceID (cmd .Context (), workspace )
31+
32+ params := & api.GetAllResourcesParams {}
33+ if limit > 0 {
34+ params .Limit = & limit
35+ }
36+ if offset > 0 {
37+ params .Offset = & offset
38+ }
39+ if query != "" {
40+ params .Cel = & query
41+ }
42+ resp , err := client .GetAllResources (cmd .Context (), workspaceID .String (), params )
43+ if err != nil {
44+ return fmt .Errorf ("failed to get resources: %w" , err )
45+ }
46+
47+ return cliutil .HandleResponseOutput (cmd , resp )
48+ },
49+ }
50+
51+ cmd .Flags ().StringVarP (& query , "query" , "q" , "" , "CEL filter" )
52+ cmd .Flags ().IntVarP (& limit , "limit" , "l" , 50 , "Limit the number of results" )
53+ cmd .Flags ().IntVarP (& offset , "offset" , "o" , 0 , "Offset the results" )
54+ cmd .Flags ().StringVarP (& workspace , "workspace" , "w" , "" , "Workspace to get resources from" )
55+
56+ cmd .MarkFlagRequired ("workspace" )
57+
58+ return cmd
59+ }
0 commit comments