-
Notifications
You must be signed in to change notification settings - Fork 928
/
create_route_command.go
107 lines (88 loc) · 3.13 KB
/
create_route_command.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package v7
import (
"code.cloudfoundry.org/cli/actor/actionerror"
"code.cloudfoundry.org/cli/actor/sharedaction"
"code.cloudfoundry.org/cli/actor/v7action"
"code.cloudfoundry.org/cli/command"
"code.cloudfoundry.org/cli/command/flag"
"code.cloudfoundry.org/cli/command/v7/shared"
"code.cloudfoundry.org/clock"
)
//go:generate counterfeiter . CreateRouteActor
type CreateRouteActor interface {
CreateRoute(spaceGUID, domainName, hostname, path string) (v7action.Route, v7action.Warnings, error)
}
type CreateRouteCommand struct {
RequiredArgs flag.Domain `positional-args:"yes"`
usage interface{} `usage:"CF_NAME create-route DOMAIN [--hostname HOSTNAME] [--path PATH]\n\nEXAMPLES:\n CF_NAME create-route example.com # example.com\n CF_NAME create-route example.com --hostname myapp # myapp.example.com\n CF_NAME create-route example.com --hostname myapp --path foo # myapp.example.com/foo"`
Hostname string `long:"hostname" short:"n" description:"Hostname for the HTTP route (required for shared domains)"`
Path flag.V7RoutePath `long:"path" description:"Path for the HTTP route"`
relatedCommands interface{} `related_commands:"check-route, domains, map-route, routes, unmap-route"`
UI command.UI
Config command.Config
Actor CreateRouteActor
SharedActor command.SharedActor
}
func (cmd *CreateRouteCommand) Setup(config command.Config, ui command.UI) error {
cmd.UI = ui
cmd.Config = config
sharedActor := sharedaction.NewActor(config)
cmd.SharedActor = sharedActor
ccClient, uaaClient, err := shared.NewClients(config, ui, true, "")
if err != nil {
return err
}
cmd.Actor = v7action.NewActor(ccClient, config, sharedActor, uaaClient, clock.NewClock())
return nil
}
func (cmd CreateRouteCommand) Execute(args []string) error {
err := cmd.SharedActor.CheckTarget(true, true)
if err != nil {
return err
}
user, err := cmd.Config.CurrentUser()
if err != nil {
return err
}
domain := cmd.RequiredArgs.Domain
hostname := cmd.Hostname
pathName := cmd.Path.Path
spaceName := cmd.Config.TargetedSpace().Name
orgName := cmd.Config.TargetedOrganization().Name
spaceGUID := cmd.Config.TargetedSpace().GUID
fqdn := desiredFQDN(domain, hostname, pathName)
cmd.UI.DisplayTextWithFlavor("Creating route {{.FQDN}} for org {{.Organization}} / space {{.Space}} as {{.User}}...",
map[string]interface{}{
"FQDN": fqdn,
"User": user.Name,
"Space": spaceName,
"Organization": orgName,
})
_, warnings, err := cmd.Actor.CreateRoute(spaceGUID, domain, hostname, pathName)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
if _, ok := err.(actionerror.RouteAlreadyExistsError); ok {
cmd.UI.DisplayWarningV7(err.Error())
cmd.UI.DisplayOK()
return nil
}
return err
}
cmd.UI.DisplayText("Route {{.FQDN}} has been created.",
map[string]interface{}{
"FQDN": fqdn,
})
cmd.UI.DisplayOK()
return nil
}
func desiredFQDN(domain, hostname, path string) string {
fqdn := ""
if hostname != "" {
fqdn += hostname + "."
}
fqdn += domain
if path != "" {
fqdn += path
}
return fqdn
}