Skip to content

Commit 8c80f10

Browse files
committed
cli get with cel filter
1 parent 3c3f803 commit 8c80f10

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

cmd/ctrlc/root/api/api.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66

7+
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/get"
78
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/upsert"
89
"github.com/spf13/cobra"
910
"github.com/spf13/viper"
@@ -41,7 +42,8 @@ func NewAPICmd() *cobra.Command {
4142
}
4243
cmd.PersistentFlags().String("template", "", "Template for output format. Accepts Go template format (e.g. --template='{{.status.phase}}')")
4344
cmd.PersistentFlags().String("format", "json", "Output format. Accepts 'json', 'yaml', or 'github-action'")
44-
45+
46+
cmd.AddCommand(get.NewGetCmd())
4547
cmd.AddCommand(upsert.NewUpsertCmd())
4648

4749
return cmd

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package get
2+
3+
import (
4+
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/upsert/deploymentversion"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
func NewGetCmd() *cobra.Command {
9+
cmd := &cobra.Command{
10+
Use: "get <command>",
11+
Short: "Get resources",
12+
Long: `Commands for getting resources.`,
13+
RunE: func(cmd *cobra.Command, args []string) error {
14+
return cmd.Help()
15+
},
16+
}
17+
18+
cmd.AddCommand(deploymentversion.NewUpsertDeploymentVersionCmd())
19+
20+
return cmd
21+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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

Comments
 (0)