-
Notifications
You must be signed in to change notification settings - Fork 6
/
create.go
59 lines (48 loc) · 1.56 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
package router
import (
"fmt"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/ui"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
"github.com/spf13/pflag"
)
type createCommand struct {
*commands.BaseCommand
name string
}
// CreateCommand creates the "router create" command
func CreateCommand() commands.Command {
return &createCommand{
BaseCommand: commands.New(
"create",
"Create a router",
"upctl router create --name my_router",
`upctl router create --name "My Router"`,
),
}
}
// InitCommand implements Command.InitCommand
func (s *createCommand) InitCommand() {
fs := &pflag.FlagSet{}
fs.StringVar(&s.name, "name", s.name, "Router name.")
s.AddFlags(fs)
_ = s.Cobra().MarkFlagRequired("name")
}
// MaximumExecutions implements Command.MaximumExecutions
func (s *createCommand) MaximumExecutions() int {
return maxRouterActions
}
// ExecuteWithoutArguments implements commands.NoArgumentCommand
func (s *createCommand) ExecuteWithoutArguments(exec commands.Executor) (output.Output, error) {
msg := fmt.Sprintf("Creating router %s", s.name)
exec.PushProgressStarted(msg)
res, err := exec.Network().CreateRouter(exec.Context(), &request.CreateRouterRequest{Name: s.name})
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
}