-
Notifications
You must be signed in to change notification settings - Fork 6
/
create.go
74 lines (58 loc) · 2.1 KB
/
create.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
68
69
70
71
72
73
74
package storagebackup
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/output"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/resolver"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/ui"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
"github.com/spf13/pflag"
)
type createBackupCommand struct {
*commands.BaseCommand
resolver.CachingStorage
completion.Storage
params createBackupParams
}
type createBackupParams struct {
request.CreateBackupRequest
}
// CreateBackupCommand creates the "storage backup create" command
func CreateBackupCommand() commands.Command {
return &createBackupCommand{
BaseCommand: commands.New(
"create",
"Create backup of a storage",
`upctl storage backup create 01cbea5e-eb5b-4072-b2ac-9b635120e5d8 --title "first backup"`,
`upctl storage backup create "My Storage" --title second_backup`,
),
}
}
var defaultCreateBackupParams = &createBackupParams{
CreateBackupRequest: request.CreateBackupRequest{},
}
// InitCommand implements Command.InitCommand
func (s *createBackupCommand) InitCommand() {
s.params = createBackupParams{CreateBackupRequest: request.CreateBackupRequest{}}
flagSet := &pflag.FlagSet{}
flagSet.StringVar(&s.params.Title, "title", defaultCreateBackupParams.Title, "A short, informational description.")
s.AddFlags(flagSet)
_ = s.Cobra().MarkFlagRequired("title")
}
// Execute implements commands.MultipleArgumentCommand
func (s *createBackupCommand) Execute(exec commands.Executor, uuid string) (output.Output, error) {
svc := exec.Storage()
msg := fmt.Sprintf("Backing up storage %v to %v", uuid, s.params.Title)
exec.PushProgressStarted(msg)
req := s.params.CreateBackupRequest
req.UUID = uuid
res, err := svc.CreateBackup(exec.Context(), &req)
if err != nil {
return commands.HandleError(exec, msg, err)
}
exec.PushProgressSuccess(msg)
return output.MarshaledWithHumanDetails{Value: res, Details: []output.DetailRow{
{Title: "UUID", Value: res.UUID, Colour: ui.DefaultUUUIDColours},
}}, nil
}