-
Notifications
You must be signed in to change notification settings - Fork 932
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2205 from pet-forks/add_cf_route_command
Implement `cf route` command
- Loading branch information
Showing
10 changed files
with
824 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package v7 | ||
|
||
import ( | ||
"code.cloudfoundry.org/cli/command/flag" | ||
"code.cloudfoundry.org/cli/resources" | ||
|
||
"strconv" | ||
) | ||
|
||
type RouteCommand struct { | ||
BaseCommand | ||
|
||
RequiredArgs flag.Domain `positional-args:"yes"` | ||
Hostname string `long:"hostname" short:"n" description:"Hostname used to identify the HTTP route"` | ||
Path flag.V7RoutePath `long:"path" description:"Path used to identify the HTTP route"` | ||
Port int `long:"port" description:"Port used to identify the TCP route"` | ||
relatedCommands interface{} `related_commands:"create-route, delete-route, routes"` | ||
} | ||
|
||
func (cmd RouteCommand) Usage() string { | ||
return ` | ||
Display an HTTP route: | ||
CF_NAME route DOMAIN [--hostname HOSTNAME] [--path PATH] | ||
Display a TCP route: | ||
CF_NAME route DOMAIN --port PORT` | ||
} | ||
|
||
func (cmd RouteCommand) Examples() string { | ||
return ` | ||
CF_NAME route example.com # example.com | ||
CF_NAME route example.com -n myhost --path foo # myhost.example.com/foo | ||
CF_NAME route example.com --path foo # example.com/foo | ||
CF_NAME route example.com --port 5000 # example.com:5000` | ||
} | ||
|
||
func (cmd RouteCommand) Execute(args []string) error { | ||
err := cmd.SharedActor.CheckTarget(true, false) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
user, err := cmd.Config.CurrentUser() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
domain, warnings, err := cmd.Actor.GetDomainByName(cmd.RequiredArgs.Domain) | ||
|
||
cmd.UI.DisplayWarnings(warnings) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
hostName := "" | ||
if cmd.Hostname != "" { | ||
hostName = cmd.Hostname + "." | ||
} | ||
|
||
displayPort := "" | ||
if cmd.Port != 0 { | ||
displayPort = ":" + strconv.Itoa(cmd.Port) | ||
|
||
} | ||
|
||
cmd.UI.DisplayTextWithFlavor(" Showing route {{.HostName}}{{.DomainName}}{{.Port}}{{.PathName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{ | ||
"HostName": hostName, | ||
"DomainName": cmd.RequiredArgs.Domain, | ||
"PathName": cmd.Path.Path, | ||
"Port": displayPort, | ||
"OrgName": cmd.Config.TargetedOrganization().Name, | ||
"SpaceName": cmd.Config.TargetedSpace().Name, | ||
"Username": user.Name, | ||
}) | ||
cmd.UI.DisplayNewline() | ||
|
||
route, warnings, err := cmd.Actor.GetRouteByAttributes(domain, cmd.Hostname, cmd.Path.Path, cmd.Port) | ||
cmd.UI.DisplayWarnings(warnings) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
port := "" | ||
if route.Port != 0 { | ||
port = strconv.Itoa(route.Port) | ||
} | ||
|
||
appMap, warnings, err := cmd.Actor.GetApplicationMapForRoute(route) | ||
cmd.UI.DisplayWarnings(warnings) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
table := [][]string{ | ||
{cmd.UI.TranslateText("domain:"), domain.Name}, | ||
{cmd.UI.TranslateText("host:"), route.Host}, | ||
{cmd.UI.TranslateText("port:"), port}, | ||
{cmd.UI.TranslateText("path:"), route.Path}, | ||
{cmd.UI.TranslateText("protocol:"), route.Protocol}, | ||
} | ||
|
||
cmd.UI.DisplayKeyValueTable("", table, 3) | ||
cmd.UI.DisplayNewline() | ||
|
||
cmd.UI.DisplayText("Destinations:") | ||
cmd.displayDestinations(route, appMap) | ||
|
||
return nil | ||
} | ||
|
||
func (cmd RouteCommand) displayDestinations(route resources.Route, appMap map[string]resources.Application) { | ||
destinations := route.Destinations | ||
if len(destinations) > 0 { | ||
var keyValueTable = [][]string{ | ||
{ | ||
cmd.UI.TranslateText("app"), | ||
cmd.UI.TranslateText("process"), | ||
cmd.UI.TranslateText("port"), | ||
cmd.UI.TranslateText("protocol"), | ||
}, | ||
} | ||
|
||
for _, destination := range destinations { | ||
port := "" | ||
if destination.Port != 0 { | ||
port = strconv.Itoa(destination.Port) | ||
} | ||
keyValueTable = append(keyValueTable, []string{ | ||
appMap[destination.App.GUID].Name, | ||
destination.App.Process.Type, | ||
port, | ||
destination.Protocol, | ||
}) | ||
} | ||
|
||
cmd.UI.DisplayKeyValueTable("\t", keyValueTable, 3) | ||
} | ||
} |
Oops, something went wrong.