-
Notifications
You must be signed in to change notification settings - Fork 932
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement cf route
command
#2205
Merged
jdgonzaleza
merged 1 commit into
cloudfoundry:master
from
pet-forks:add_cf_route_command
Sep 15, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason why we are reorganizing this slice?
cc: @bepotts @a-b