forked from cloudfoundry/cli
/
share_route_command.go
101 lines (84 loc) · 3.11 KB
/
share_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
package v7
import (
"github.com/LukasHeimann/cloudfoundrycli/v8/actor/actionerror"
"github.com/LukasHeimann/cloudfoundrycli/v8/command/flag"
)
type ShareRouteCommand struct {
BaseCommand
RequireArgs flag.Domain `positional-args:"yes"`
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"`
DestinationOrg string `short:"o" description:"The org of the destination app (Default: targeted org)"`
DestinationSpace string `short:"s" description:"The space of the destination app (Default: targeted space)"`
relatedCommands interface{} `related_commands:"create-route, map-route, unmap-route, routes"`
}
func (cmd ShareRouteCommand) Usage() string {
return `
Share an existing route in between two spaces:
CF_NAME share-route DOMAIN [--hostname HOSTNAME] [--path PATH] -s OTHER_SPACE [-o OTHER_ORG]`
}
func (cmd ShareRouteCommand) Examples() string {
return `
CF_NAME share-route example.com --hostname myHost --path foo -s TargetSpace -o TargetOrg # myhost.example.com/foo
CF_NAME share-route example.com --hostname myHost -s TargetSpace # myhost.example.com
CF_NAME share-route example.com --hostname myHost -s TargetSpace -o TargetOrg # myhost.example.com`
}
func (cmd ShareRouteCommand) Execute(args []string) error {
err := cmd.SharedActor.CheckTarget(true, true)
if err != nil {
return err
}
user, err := cmd.Actor.GetCurrentUser()
if err != nil {
return err
}
domain, warnings, err := cmd.Actor.GetDomainByName(cmd.RequireArgs.Domain)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return err
}
path := cmd.Path.Path
route, warnings, err := cmd.Actor.GetRouteByAttributes(domain, cmd.Hostname, path, 0)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
if _, ok := err.(actionerror.RouteNotFoundError); ok {
cmd.UI.DisplayText("Can not share route:")
return err
}
}
destinationOrgName := cmd.DestinationOrg
if destinationOrgName == "" {
destinationOrgName = cmd.Config.TargetedOrganizationName()
}
destinationOrg, warnings, err := cmd.Actor.GetOrganizationByName(destinationOrgName)
if err != nil {
if _, ok := err.(actionerror.OrganizationNotFoundError); ok {
cmd.UI.DisplayText("Can not share route:")
return err
}
}
targetedSpace, warnings, err := cmd.Actor.GetSpaceByNameAndOrganization(cmd.DestinationSpace, destinationOrg.GUID)
if err != nil {
if _, ok := err.(actionerror.SpaceNotFoundError); ok {
cmd.UI.DisplayText("Can not share route:")
return err
}
}
url := desiredURL(domain.Name, cmd.Hostname, path, 0)
cmd.UI.DisplayTextWithFlavor("Sharing route {{.URL}} to space {{.DestinationSpace}} as {{.User}}",
map[string]interface{}{
"URL": url,
"DestinationSpace": cmd.DestinationSpace,
"User": user.Name,
})
warnings, err = cmd.Actor.ShareRoute(
route.GUID,
targetedSpace.GUID,
)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return err
}
cmd.UI.DisplayOK()
return nil
}