|
| 1 | +package databaseconnection |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/UpCloudLtd/upcloud-cli/internal/commands" |
| 7 | + "github.com/UpCloudLtd/upcloud-cli/internal/completion" |
| 8 | + "github.com/UpCloudLtd/upcloud-cli/internal/config" |
| 9 | + "github.com/UpCloudLtd/upcloud-cli/internal/output" |
| 10 | + "github.com/UpCloudLtd/upcloud-cli/internal/resolver" |
| 11 | + |
| 12 | + "github.com/UpCloudLtd/upcloud-go-api/v4/upcloud/request" |
| 13 | + "github.com/spf13/pflag" |
| 14 | +) |
| 15 | + |
| 16 | +type cancelCommand struct { |
| 17 | + *commands.BaseCommand |
| 18 | + resolver.CachingDatabase |
| 19 | + completion.Database |
| 20 | + pid int |
| 21 | + terminate config.OptionalBoolean |
| 22 | +} |
| 23 | + |
| 24 | +// CancelCommand creates the "connection cancel" command |
| 25 | +func CancelCommand() commands.Command { |
| 26 | + return &cancelCommand{ |
| 27 | + BaseCommand: commands.New( |
| 28 | + "cancel", |
| 29 | + "Terminate client connection or cancel runnig query for a database", |
| 30 | + `upctl database connection cancel 0fa980c4-0e4f-460b-9869-11b7bd62b833 --pid 2345421`, |
| 31 | + `upctl database connection cancel 0fa980c4-0e4f-460b-9869-11b7bd62b833 --pid 2345421 --terminate`, |
| 32 | + ), |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// InitCommand implements Command.InitCommand |
| 37 | +func (s *cancelCommand) InitCommand() { |
| 38 | + flagSet := &pflag.FlagSet{} |
| 39 | + flagSet.IntVar(&s.pid, "pid", 0, "Process ID of the connection to cancel.") |
| 40 | + config.AddToggleFlag(flagSet, &s.terminate, "terminate", false, "Request immediate termination instead of soft cancel.") |
| 41 | + |
| 42 | + s.AddFlags(flagSet) |
| 43 | + s.Cobra().MarkFlagRequired("pid") //nolint:errcheck |
| 44 | +} |
| 45 | + |
| 46 | +// Execute implements commands.MultipleArgumentCommand |
| 47 | +func (s *cancelCommand) Execute(exec commands.Executor, uuid string) (output.Output, error) { |
| 48 | + svc := exec.All() |
| 49 | + |
| 50 | + msg := fmt.Sprintf("Cancelling connection %v to database %v", s.pid, uuid) |
| 51 | + logline := exec.NewLogEntry(msg) |
| 52 | + |
| 53 | + logline.StartedNow() |
| 54 | + |
| 55 | + if err := svc.CancelManagedDatabaseConnection(&request.CancelManagedDatabaseConnection{ |
| 56 | + UUID: uuid, |
| 57 | + Pid: s.pid, |
| 58 | + Terminate: s.terminate.Value(), |
| 59 | + }); err != nil { |
| 60 | + return commands.HandleError(logline, fmt.Sprintf("%s: failed", msg), err) |
| 61 | + } |
| 62 | + |
| 63 | + logline.SetMessage(fmt.Sprintf("%s: success", msg)) |
| 64 | + logline.MarkDone() |
| 65 | + |
| 66 | + return output.None{}, nil |
| 67 | +} |
0 commit comments