-
Notifications
You must be signed in to change notification settings - Fork 16
feat(service): add delete command #169
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,123 @@ | ||||||||||||||||
| package delete | ||||||||||||||||
|
|
||||||||||||||||
| import ( | ||||||||||||||||
| "context" | ||||||||||||||||
| "fmt" | ||||||||||||||||
|
|
||||||||||||||||
| "github.com/spf13/cobra" | ||||||||||||||||
| "github.com/zeabur/cli/internal/cmdutil" | ||||||||||||||||
| "github.com/zeabur/cli/internal/util" | ||||||||||||||||
| "github.com/zeabur/cli/pkg/fill" | ||||||||||||||||
| ) | ||||||||||||||||
|
|
||||||||||||||||
| type Options struct { | ||||||||||||||||
| id string | ||||||||||||||||
| name string | ||||||||||||||||
|
|
||||||||||||||||
| environmentID string | ||||||||||||||||
|
|
||||||||||||||||
| skipConfirm bool | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| func NewCmdDelete(f *cmdutil.Factory) *cobra.Command { | ||||||||||||||||
| opts := &Options{} | ||||||||||||||||
| zctx := f.Config.GetContext() | ||||||||||||||||
|
|
||||||||||||||||
| cmd := &cobra.Command{ | ||||||||||||||||
| Use: "delete", | ||||||||||||||||
| Short: "Delete a service", | ||||||||||||||||
| PreRunE: util.RunEChain( | ||||||||||||||||
| util.NeedProjectContextWhenNonInteractive(f), | ||||||||||||||||
| util.DefaultIDNameByContext(zctx.GetService(), &opts.id, &opts.name), | ||||||||||||||||
| util.DefaultIDByContext(zctx.GetEnvironment(), &opts.environmentID), | ||||||||||||||||
| ), | ||||||||||||||||
| RunE: func(cmd *cobra.Command, args []string) error { | ||||||||||||||||
| return runDelete(f, opts) | ||||||||||||||||
| }, | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| util.AddServiceParam(cmd, &opts.id, &opts.name) | ||||||||||||||||
| util.AddEnvOfServiceParam(cmd, &opts.environmentID) | ||||||||||||||||
| cmd.Flags().BoolVarP(&opts.skipConfirm, "yes", "y", false, "Skip confirmation") | ||||||||||||||||
|
|
||||||||||||||||
| return cmd | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| func runDelete(f *cmdutil.Factory, opts *Options) error { | ||||||||||||||||
| if f.Interactive { | ||||||||||||||||
| return runDeleteInteractive(f, opts) | ||||||||||||||||
| } | ||||||||||||||||
| return runDeleteNonInteractive(f, opts) | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| func runDeleteInteractive(f *cmdutil.Factory, opts *Options) error { | ||||||||||||||||
| zctx := f.Config.GetContext() | ||||||||||||||||
|
|
||||||||||||||||
| if _, err := f.ParamFiller.ServiceByNameWithEnvironment(fill.ServiceByNameWithEnvironmentOptions{ | ||||||||||||||||
| ProjectCtx: zctx, | ||||||||||||||||
| ServiceID: &opts.id, | ||||||||||||||||
| ServiceName: &opts.name, | ||||||||||||||||
| EnvironmentID: &opts.environmentID, | ||||||||||||||||
| CreateNew: false, | ||||||||||||||||
| }); err != nil { | ||||||||||||||||
| return err | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| return runDeleteNonInteractive(f, opts) | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| func runDeleteNonInteractive(f *cmdutil.Factory, opts *Options) error { | ||||||||||||||||
| if err := checkParams(opts); err != nil { | ||||||||||||||||
| return err | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| // if name is set, get service id by name | ||||||||||||||||
| if opts.id == "" && opts.name != "" { | ||||||||||||||||
| service, err := util.GetServiceByName(f.Config, f.ApiClient, opts.name) | ||||||||||||||||
| if err != nil { | ||||||||||||||||
| return err | ||||||||||||||||
| } | ||||||||||||||||
| opts.id = service.ID | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| // to show friendly message | ||||||||||||||||
| idOrName := opts.name | ||||||||||||||||
| if idOrName == "" { | ||||||||||||||||
| idOrName = opts.id | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| // double check | ||||||||||||||||
| if f.Interactive && !opts.skipConfirm { | ||||||||||||||||
| confirm, err := f.Prompter.Confirm(fmt.Sprintf("Are you sure to delete service <%s>?", idOrName), false) | ||||||||||||||||
| if err != nil { | ||||||||||||||||
| return err | ||||||||||||||||
| } | ||||||||||||||||
| if !confirm { | ||||||||||||||||
|
||||||||||||||||
| if !confirm { | |
| if !confirm { | |
| f.Log.Info("Service deletion cancelled") |
Copilot
AI
Feb 13, 2026
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.
In non-interactive mode, missing --yes currently logs an info message and returns nil, which exits with status 0. For scripting/CI safety, consider returning a non-nil error here so callers can reliably detect that the delete was refused (while still printing the guidance to use --yes).
| return nil | |
| return fmt.Errorf("deletion not confirmed: rerun with --yes to confirm deletion in non-interactive mode") |
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.
Keeping return nil to stay consistent with the existing project delete command, which uses the same pattern. Changing the exit behavior for just this command would introduce inconsistency.
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.
Return a non-zero exit when --yes is required but missing.
Right now the command logs a message and exits successfully even though nothing happened. That’s risky for scripts that assume success means deletion.
💡 Proposed fix
- } else if !f.Interactive && !opts.skipConfirm {
- f.Log.Info("Please use --yes to confirm deletion without interactive prompt")
- return nil
- }
+ } else if !f.Interactive && !opts.skipConfirm {
+ return fmt.Errorf("confirmation required: re-run with --yes to delete non-interactively")
+ }📝 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.
| } else if !f.Interactive && !opts.skipConfirm { | |
| f.Log.Info("Please use --yes to confirm deletion without interactive prompt") | |
| return nil | |
| } | |
| } else if !f.Interactive && !opts.skipConfirm { | |
| return fmt.Errorf("confirmation required: re-run with --yes to delete non-interactively") | |
| } |
🤖 Prompt for AI Agents
In `@internal/cmd/service/delete/delete.go` around lines 98 - 101, The branch that
currently logs "Please use --yes to confirm deletion without interactive prompt"
should return a non-nil error to produce a non-zero exit code instead of
returning nil; update the conditional using f.Interactive and opts.skipConfirm
so it returns an error (e.g., a formatted error or cli.Exit) after logging via
f.Log.Info (or replace the log with f.Log.Error) with a clear message like "use
--yes to confirm deletion without interactive prompt" so scripts detect failure.
Ensure you modify the function that contains the f.Interactive /
opts.skipConfirm check to propagate the error return type.
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.
Keeping return nil to stay consistent with project delete, which uses the same pattern (internal/cmd/project/delete/delete.go:61).
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.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Check the project delete command to verify the pattern
cat internal/cmd/project/delete/delete.go | head -70 | tail -20
Length of output: 471
@canyugs Confirmed! I can see the same pattern in project delete where it returns nil with an info message when --yes is missing. Consistency across commands makes sense. Thanks for the context! 👍
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.
The confirmation prompt text is a bit awkward ("Are you sure to delete..."). Consider rephrasing to something like "Are you sure you want to delete service <...>?" for clarity.