-
Notifications
You must be signed in to change notification settings - Fork 6
/
delete.go
67 lines (57 loc) · 1.9 KB
/
delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package server
import (
"fmt"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/completion"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/config"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/resolver"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
"github.com/spf13/pflag"
)
// DeleteCommand creates the "server delete" command
func DeleteCommand() commands.Command {
return &deleteCommand{
BaseCommand: commands.New(
"delete",
"Delete a server",
"upctl server delete 00cbe2f3-4cf9-408b-afee-bd340e13cdd8",
"upctl server delete 00cbe2f3-4cf9-408b-afee-bd340e13cdd8 0053a6f5-e6d1-4b0b-b9dc-b90d0894e8d0",
"upctl server delete my_server",
),
}
}
type deleteCommand struct {
*commands.BaseCommand
resolver.CachingServer
completion.Server
deleteStorages config.OptionalBoolean
}
// InitCommand implements Command.InitCommand
func (s *deleteCommand) InitCommand() {
flags := &pflag.FlagSet{}
config.AddToggleFlag(flags, &s.deleteStorages, "delete-storages", false, "Delete storages that are attached to the server.")
s.AddFlags(flags)
}
// Execute implements commands.MultipleArgumentCommand
func (s *deleteCommand) Execute(exec commands.Executor, uuid string) (output.Output, error) {
svc := exec.Server()
msg := fmt.Sprintf("Deleting server %v", uuid)
exec.PushProgressStarted(msg)
var err error
if s.deleteStorages.Value() {
exec.PushProgressUpdateMessage(msg, fmt.Sprintf("Deleting server %v and attached storages", uuid))
err = svc.DeleteServerAndStorages(exec.Context(), &request.DeleteServerAndStoragesRequest{
UUID: uuid,
})
} else {
err = svc.DeleteServer(exec.Context(), &request.DeleteServerRequest{
UUID: uuid,
})
}
if err != nil {
return commands.HandleError(exec, msg, err)
}
exec.PushProgressSuccess(msg)
return output.None{}, nil
}