-
Notifications
You must be signed in to change notification settings - Fork 6
/
templatize.go
83 lines (68 loc) · 2.57 KB
/
templatize.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
75
76
77
78
79
80
81
82
83
package storage
import (
"fmt"
"github.com/UpCloudLtd/upcloud-cli/v2/internal/commands"
"github.com/UpCloudLtd/upcloud-cli/v2/internal/completion"
"github.com/UpCloudLtd/upcloud-cli/v2/internal/config"
"github.com/UpCloudLtd/upcloud-cli/v2/internal/output"
"github.com/UpCloudLtd/upcloud-cli/v2/internal/resolver"
"github.com/UpCloudLtd/upcloud-cli/v2/internal/ui"
"github.com/UpCloudLtd/upcloud-go-api/v6/upcloud"
"github.com/UpCloudLtd/upcloud-go-api/v6/upcloud/request"
"github.com/spf13/pflag"
)
type templatizeCommand struct {
*commands.BaseCommand
wait config.OptionalBoolean
resolver.CachingStorage
completion.Storage
params templatizeParams
}
type templatizeParams struct {
request.TemplatizeStorageRequest
}
// TemplatizeCommand creates the "storage templatise" command
// TODO: figure out consistent naming, one way or the other.
func TemplatizeCommand() commands.Command {
return &templatizeCommand{
BaseCommand: commands.New(
"templatise",
"Templatise a storage",
`upctl storage templatise 01271548-2e92-44bb-9774-d282508cc762 --title "My Template"`,
`upctl storage templatise 01271548-2e92-44bb-9774-d282508cc762 --title "My Template" --wait`,
`upctl storage templatise "My Storage" --title super_template`,
),
}
}
var defaultTemplatizeParams = &templatizeParams{
TemplatizeStorageRequest: request.TemplatizeStorageRequest{},
}
// InitCommand implements Command.InitCommand
func (s *templatizeCommand) InitCommand() {
s.params = templatizeParams{TemplatizeStorageRequest: request.TemplatizeStorageRequest{}}
flagSet := &pflag.FlagSet{}
flagSet.StringVar(&s.params.Title, "title", defaultTemplatizeParams.Title, "A short, informational description.")
config.AddToggleFlag(flagSet, &s.wait, "wait", false, "Wait for storage to be in online state before returning.")
s.AddFlags(flagSet)
_ = s.Cobra().MarkFlagRequired("title")
}
// Execute implements commands.MultipleArgumentCommand
func (s *templatizeCommand) Execute(exec commands.Executor, uuid string) (output.Output, error) {
svc := exec.Storage()
req := s.params.TemplatizeStorageRequest
req.UUID = uuid
msg := fmt.Sprintf("Templatise storage %v", uuid)
exec.PushProgressStarted(msg)
res, err := svc.TemplatizeStorage(exec.Context(), &req)
if err != nil {
return commands.HandleError(exec, msg, err)
}
if s.wait.Value() {
waitForStorageState(res.UUID, upcloud.StorageStateOnline, exec, msg)
} else {
exec.PushProgressSuccess(msg)
}
return output.MarshaledWithHumanDetails{Value: res, Details: []output.DetailRow{
{Title: "UUID", Value: res.UUID, Colour: ui.DefaultUUUIDColours},
}}, nil
}