Skip to content
Merged
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
2 changes: 2 additions & 0 deletions cmd/ctrlc/root/api/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package delete

import (
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/delete/environment"
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/delete/releasechannel"
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/delete/resource"
"github.com/spf13/cobra"
)
Expand All @@ -18,6 +19,7 @@ func NewDeleteCmd() *cobra.Command {

cmd.AddCommand(resource.NewDeleteResourceCmd())
cmd.AddCommand(environment.NewDeleteEnvironmentCmd())
cmd.AddCommand(releasechannel.NewDeleteReleaseChannelCmd())

return cmd
}
48 changes: 48 additions & 0 deletions cmd/ctrlc/root/api/delete/releasechannel/release-channel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package releasechannel

import (
"fmt"

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

func NewDeleteReleaseChannelCmd() *cobra.Command {
var deploymentId string
var name string

cmd := &cobra.Command{
Use: "release-channel [flags]",
Short: "Delete a release channel",
Long: `Delete a release channel by specifying a deployment ID and a name.`,
Example: heredoc.Doc(`
$ ctrlc delete release-channel --deployment 123e4567-e89b-12d3-a456-426614174000 --name mychannel
`),
RunE: func(cmd *cobra.Command, args []string) error {
if deploymentId == "" || name == "" {
return fmt.Errorf("deployment and name are required")
}

apiURL := viper.GetString("url")
apiKey := viper.GetString("api-key")
client, err := api.NewAPIKeyClientWithResponses(apiURL, apiKey)
if err != nil {
return fmt.Errorf("failed to create API client: %w", err)
}
resp, err := client.DeleteReleaseChannel(cmd.Context(), deploymentId, name)
if err != nil {
return fmt.Errorf("failed to delete release channel: %w", err)
}

return cliutil.HandleOutput(cmd, resp)
},
Comment on lines +24 to +41
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add safety measures and improve error handling

The current implementation could benefit from additional safety measures and better error handling:

  1. Add UUID validation for deployment ID
  2. Add confirmation prompt for this destructive action
  3. Add context timeout for API call
  4. Improve error messages
 RunE: func(cmd *cobra.Command, args []string) error {
   if deploymentId == "" || name == "" {
-    return fmt.Errorf("deployment and name are required")
+    return fmt.Errorf("both --deployment and --name flags are required")
+  }
+
+  if !isValidUUID(deploymentId) {
+    return fmt.Errorf("invalid deployment ID format. Expected UUID format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")
   }
+
+  if !cmd.Flags().Changed("force") {
+    confirmed, err := cliutil.ConfirmDeletion(fmt.Sprintf("Are you sure you want to delete release channel '%s'?", name))
+    if err != nil {
+      return err
+    }
+    if !confirmed {
+      return nil
+    }
+  }

   apiURL := viper.GetString("url")
   apiKey := viper.GetString("api-key")
   client, err := api.NewAPIKeyClientWithResponses(apiURL, apiKey)
   if err != nil {
     return fmt.Errorf("failed to create API client: %w", err)
   }
-  resp, err := client.DeleteReleaseChannel(cmd.Context(), deploymentId, name)
+
+  ctx, cancel := context.WithTimeout(cmd.Context(), 30*time.Second)
+  defer cancel()
+
+  resp, err := client.DeleteReleaseChannel(ctx, deploymentId, name)
   if err != nil {
-    return fmt.Errorf("failed to delete release channel: %w", err)
+    return fmt.Errorf("failed to delete release channel '%s': %w", name, err)
   }

   return cliutil.HandleOutput(cmd, resp)
 },

Don't forget to add the new imports and helper function:

import (
  "context"
  "time"
)

func isValidUUID(uuid string) bool {
  r := regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$")
  return r.MatchString(uuid)
}

}

cmd.Flags().StringVar(&deploymentId, "deployment", "", "Deployment ID")
cmd.Flags().StringVar(&name, "name", "", "Release channel name")

Comment on lines +44 to +46
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Mark required flags and add force flag

Add required flag marking and a force flag for skipping confirmation:

 cmd.Flags().StringVar(&deploymentId, "deployment", "", "Deployment ID")
 cmd.Flags().StringVar(&name, "name", "", "Release channel name")
+cmd.Flags().Bool("force", false, "Skip confirmation prompt")
+
+cmd.MarkFlagRequired("deployment")
+cmd.MarkFlagRequired("name")
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
cmd.Flags().StringVar(&deploymentId, "deployment", "", "Deployment ID")
cmd.Flags().StringVar(&name, "name", "", "Release channel name")
cmd.Flags().StringVar(&deploymentId, "deployment", "", "Deployment ID")
cmd.Flags().StringVar(&name, "name", "", "Release channel name")
cmd.Flags().Bool("force", false, "Skip confirmation prompt")
cmd.MarkFlagRequired("deployment")
cmd.MarkFlagRequired("name")

return cmd
}
156 changes: 156 additions & 0 deletions internal/api/client.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.