Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions cmd/ctrlc/root/api/get/deployments/deployments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package deployments

import (
"fmt"

"github.com/ctrlplanedev/cli/internal/api"
"github.com/ctrlplanedev/cli/internal/cliutil"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func NewDeploymentsCmd() *cobra.Command {
var limit int
var offset int

cmd := &cobra.Command{
Use: "deployments",
Short: "Get deployments",
Long: `Commands for getting deployments.`,
RunE: func(cmd *cobra.Command, args []string) error {
apiURL := viper.GetString("url")
apiKey := viper.GetString("api-key")
workspace := viper.GetString("workspace")

client, err := api.NewAPIKeyClientWithResponses(apiURL, apiKey)
if err != nil {
return fmt.Errorf("failed to create API client: %w", err)
}

workspaceID := client.GetWorkspaceID(cmd.Context(), workspace)

if workspaceID.String() == "00000000-0000-0000-0000-000000000000" {
return fmt.Errorf("invalid workspace: %s", workspace)
}

params := &api.ListDeploymentsParams{}
if limit > 0 {
params.Limit = &limit
}
if offset > 0 {
params.Offset = &offset
}

resp, err := client.ListDeployments(cmd.Context(), workspaceID.String(), params)
if err != nil {
return fmt.Errorf("failed to get deployments: %w", err)
}

return cliutil.HandleResponseOutput(cmd, resp)
},
}

cmd.Flags().IntVarP(&limit, "limit", "l", 50, "Limit the number of results")
cmd.Flags().IntVarP(&offset, "offset", "o", 0, "Offset the results")

cmd.MarkFlagRequired("workspace")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

MarkFlagRequired has no effect here.

Same issue as in other commands—the workspace flag isn't defined on this command. Add explicit validation for the workspace string before use.

🤖 Prompt for AI Agents
In `@cmd/ctrlc/root/api/get/deployments/deployments.go` at line 56, The call to
cmd.MarkFlagRequired("workspace") is ineffective because the "workspace" flag
isn't defined; instead add explicit validation in the command's RunE handler (or
the function that constructs the command in deployments.go) to check the
workspace string before use: ensure the variable (workspace) is non-empty and
return a clear error if missing, or alternatively define the workspace flag on
the command before calling cmd.MarkFlagRequired("workspace"); update the code
path that uses workspace (the command's RunE) to rely on this validation.


return cmd
}
59 changes: 59 additions & 0 deletions cmd/ctrlc/root/api/get/environments/environments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package environments

import (
"fmt"

"github.com/ctrlplanedev/cli/internal/api"
"github.com/ctrlplanedev/cli/internal/cliutil"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func NewEnvironmentsCmd() *cobra.Command {
var limit int
var offset int

cmd := &cobra.Command{
Use: "environments",
Short: "Get environments",
Long: `Commands for getting environments.`,
RunE: func(cmd *cobra.Command, args []string) error {
apiURL := viper.GetString("url")
apiKey := viper.GetString("api-key")
workspace := viper.GetString("workspace")

client, err := api.NewAPIKeyClientWithResponses(apiURL, apiKey)
if err != nil {
return fmt.Errorf("failed to create API client: %w", err)
}

workspaceID := client.GetWorkspaceID(cmd.Context(), workspace)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Missing error handling for GetWorkspaceID.

Same issue as in the systems command — no validation of the workspace resolution result before using it.

🤖 Prompt for AI Agents
In `@cmd/ctrlc/root/api/get/environments/environments.go` at line 31,
GetWorkspaceID is called without checking its result; change the call to capture
and validate the error (e.g. workspaceID, err :=
client.GetWorkspaceID(cmd.Context(), workspace)) and handle it before using
workspaceID: if err != nil or workspaceID is empty, return or print a clear
error and stop further processing. Update the code path in the environments
command to propagate or log the error consistently (mirroring how other commands
handle workspace resolution) so workspaceID is never used when resolution
failed.


if workspaceID.String() == "00000000-0000-0000-0000-000000000000" {
return fmt.Errorf("invalid workspace: %s", workspace)
}

params := &api.ListEnvironmentsParams{}
if limit > 0 {
params.Limit = &limit
}
if offset > 0 {
params.Offset = &offset
}

resp, err := client.ListEnvironments(cmd.Context(), workspaceID.String(), params)
if err != nil {
return fmt.Errorf("failed to get environments: %w", err)
}

return cliutil.HandleResponseOutput(cmd, resp)
},
}

cmd.Flags().IntVarP(&limit, "limit", "l", 50, "Limit the number of results")
cmd.Flags().IntVarP(&offset, "offset", "o", 0, "Offset the results")

cmd.MarkFlagRequired("workspace")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

MarkFlagRequired has no effect here.

Same issue as in systems.go—the workspace flag isn't defined on this command. Add explicit validation for the workspace string before use.

🤖 Prompt for AI Agents
In `@cmd/ctrlc/root/api/get/environments/environments.go` at line 56, The call to
cmd.MarkFlagRequired("workspace") is ineffective because the "workspace" flag
isn't defined on this command; instead, add explicit validation for the
workspace string before use in the environments command handler (where
cmd.MarkFlagRequired("workspace") is currently called). Locate the command setup
and the function that reads/uses the workspace (references:
cmd.MarkFlagRequired, the environments command initialization, and the code that
reads the workspace variable) and implement one of: define the flag on this
command before marking it required, or remove MarkFlagRequired and check the
workspace string at runtime (e.g., if workspace == "" return/exit with a clear
error). Ensure the error message references the workspace variable and returns
non-zero/propagates the error so callers fail fast.


return cmd
}
6 changes: 6 additions & 0 deletions cmd/ctrlc/root/api/get/get.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package get

import (
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/get/deployments"
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/get/environments"
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/get/release"
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/get/resources"
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/get/systems"
"github.com/spf13/cobra"
)

Expand All @@ -18,6 +21,9 @@ func NewGetCmd() *cobra.Command {

cmd.AddCommand(resources.NewResourcesCmd())
cmd.AddCommand(release.NewReleaseCmd())
cmd.AddCommand(environments.NewEnvironmentsCmd())
cmd.AddCommand(systems.NewSystemsCmd())
cmd.AddCommand(deployments.NewDeploymentsCmd())

return cmd
}
59 changes: 59 additions & 0 deletions cmd/ctrlc/root/api/get/systems/systems.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package systems

import (
"fmt"

"github.com/ctrlplanedev/cli/internal/api"
"github.com/ctrlplanedev/cli/internal/cliutil"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func NewSystemsCmd() *cobra.Command {
var limit int
var offset int

cmd := &cobra.Command{
Use: "systems",
Short: "Get systems",
Long: `Commands for getting systems.`,
RunE: func(cmd *cobra.Command, args []string) error {
apiURL := viper.GetString("url")
apiKey := viper.GetString("api-key")
workspace := viper.GetString("workspace")

client, err := api.NewAPIKeyClientWithResponses(apiURL, apiKey)
if err != nil {
return fmt.Errorf("failed to create API client: %w", err)
}

workspaceID := client.GetWorkspaceID(cmd.Context(), workspace)

if workspaceID.String() == "00000000-0000-0000-0000-000000000000" {
return fmt.Errorf("invalid workspace: %s", workspace)
}

params := &api.ListSystemsParams{}
if limit > 0 {
params.Limit = &limit
}
if offset > 0 {
params.Offset = &offset
}

resp, err := client.ListSystems(cmd.Context(), workspaceID.String(), params)
if err != nil {
return fmt.Errorf("failed to get systems: %w", err)
}

return cliutil.HandleResponseOutput(cmd, resp)
},
}

cmd.Flags().IntVarP(&limit, "limit", "l", 50, "Limit the number of results")
cmd.Flags().IntVarP(&offset, "offset", "o", 0, "Offset the results")

cmd.MarkFlagRequired("workspace")

return cmd
}