|
| 1 | +package database |
| 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/output" |
| 9 | + "github.com/UpCloudLtd/upcloud-cli/internal/resolver" |
| 10 | + |
| 11 | + "github.com/UpCloudLtd/upcloud-go-api/v4/upcloud/request" |
| 12 | +) |
| 13 | + |
| 14 | +// StopCommand creates the "database stop" command |
| 15 | +func StopCommand() commands.Command { |
| 16 | + return &stopCommand{ |
| 17 | + BaseCommand: commands.New( |
| 18 | + "stop", |
| 19 | + "Stop a managed database", |
| 20 | + "upctl database stop b0952286-1193-4a81-a1af-62efc014ae4b", |
| 21 | + "upctl database stop b0952286-1193-4a81-a1af-62efc014ae4b 666bcd3c-5c63-428d-a4fd-07c27469a5a6", |
| 22 | + "upctl database stop pg-1x1xcpu-2gb-25gb-pl-waw1", |
| 23 | + ), |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +type stopCommand struct { |
| 28 | + *commands.BaseCommand |
| 29 | + completion.Database |
| 30 | + resolver.CachingDatabase |
| 31 | +} |
| 32 | + |
| 33 | +// InitCommand implements Command.InitCommand |
| 34 | +func (s *stopCommand) InitCommand() { |
| 35 | + s.Cobra().Aliases = []string{"shutdown"} |
| 36 | +} |
| 37 | + |
| 38 | +// Execute implements commands.MultipleArgumentCommand |
| 39 | +func (s *stopCommand) Execute(exec commands.Executor, uuid string) (output.Output, error) { |
| 40 | + svc := exec.All() |
| 41 | + msg := fmt.Sprintf("Stopping database %v", uuid) |
| 42 | + logline := exec.NewLogEntry(msg) |
| 43 | + |
| 44 | + logline.StartedNow() |
| 45 | + logline.SetMessage(fmt.Sprintf("%s: sending request", msg)) |
| 46 | + |
| 47 | + res, err := svc.ShutdownManagedDatabase(&request.ShutdownManagedDatabaseRequest{ |
| 48 | + UUID: uuid, |
| 49 | + }) |
| 50 | + if err != nil { |
| 51 | + return commands.HandleError(logline, fmt.Sprintf("%s: failed", msg), err) |
| 52 | + } |
| 53 | + |
| 54 | + logline.SetMessage(fmt.Sprintf("%s: done", msg)) |
| 55 | + logline.MarkDone() |
| 56 | + |
| 57 | + return output.OnlyMarshaled{Value: res}, nil |
| 58 | +} |
0 commit comments