-
Notifications
You must be signed in to change notification settings - Fork 1
fix: Delete release channel #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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) | ||||||||||||||||||
| }, | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| cmd.Flags().StringVar(&deploymentId, "deployment", "", "Deployment ID") | ||||||||||||||||||
| cmd.Flags().StringVar(&name, "name", "", "Release channel name") | ||||||||||||||||||
|
|
||||||||||||||||||
|
Comment on lines
+44
to
+46
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Suggested change
|
||||||||||||||||||
| return cmd | ||||||||||||||||||
| } | ||||||||||||||||||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
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:
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: