github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/command/v7/share_route_command.go (about)

     1  package v7
     2  
     3  import (
     4  	"github.com/LukasHeimann/cloudfoundrycli/v8/actor/actionerror"
     5  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/flag"
     6  )
     7  
     8  type ShareRouteCommand struct {
     9  	BaseCommand
    10  
    11  	RequireArgs      flag.Domain      `positional-args:"yes"`
    12  	Hostname         string           `long:"hostname" short:"n" description:"Hostname for the HTTP route (required for shared domains)"`
    13  	Path             flag.V7RoutePath `long:"path" description:"Path for the HTTP route"`
    14  	DestinationOrg   string           `short:"o" description:"The org of the destination app (Default: targeted org)"`
    15  	DestinationSpace string           `short:"s" description:"The space of the destination app (Default: targeted space)"`
    16  
    17  	relatedCommands interface{} `related_commands:"create-route, map-route, unmap-route, routes"`
    18  }
    19  
    20  func (cmd ShareRouteCommand) Usage() string {
    21  	return `
    22  Share an existing route in between two spaces:
    23  	CF_NAME share-route DOMAIN [--hostname HOSTNAME] [--path PATH] -s OTHER_SPACE [-o OTHER_ORG]`
    24  }
    25  
    26  func (cmd ShareRouteCommand) Examples() string {
    27  	return `
    28  CF_NAME share-route example.com --hostname myHost --path foo -s TargetSpace -o TargetOrg        # myhost.example.com/foo
    29  CF_NAME share-route example.com --hostname myHost -s TargetSpace                                # myhost.example.com
    30  CF_NAME share-route example.com --hostname myHost -s TargetSpace -o TargetOrg                   # myhost.example.com`
    31  }
    32  
    33  func (cmd ShareRouteCommand) Execute(args []string) error {
    34  	err := cmd.SharedActor.CheckTarget(true, true)
    35  	if err != nil {
    36  		return err
    37  	}
    38  
    39  	user, err := cmd.Actor.GetCurrentUser()
    40  	if err != nil {
    41  		return err
    42  	}
    43  
    44  	domain, warnings, err := cmd.Actor.GetDomainByName(cmd.RequireArgs.Domain)
    45  	cmd.UI.DisplayWarnings(warnings)
    46  	if err != nil {
    47  		return err
    48  	}
    49  
    50  	path := cmd.Path.Path
    51  	route, warnings, err := cmd.Actor.GetRouteByAttributes(domain, cmd.Hostname, path, 0)
    52  	cmd.UI.DisplayWarnings(warnings)
    53  	if err != nil {
    54  		if _, ok := err.(actionerror.RouteNotFoundError); ok {
    55  			cmd.UI.DisplayText("Can not share route:")
    56  			return err
    57  		}
    58  	}
    59  
    60  	destinationOrgName := cmd.DestinationOrg
    61  
    62  	if destinationOrgName == "" {
    63  		destinationOrgName = cmd.Config.TargetedOrganizationName()
    64  	}
    65  
    66  	destinationOrg, warnings, err := cmd.Actor.GetOrganizationByName(destinationOrgName)
    67  
    68  	if err != nil {
    69  		if _, ok := err.(actionerror.OrganizationNotFoundError); ok {
    70  			cmd.UI.DisplayText("Can not share route:")
    71  			return err
    72  		}
    73  	}
    74  
    75  	targetedSpace, warnings, err := cmd.Actor.GetSpaceByNameAndOrganization(cmd.DestinationSpace, destinationOrg.GUID)
    76  	if err != nil {
    77  		if _, ok := err.(actionerror.SpaceNotFoundError); ok {
    78  			cmd.UI.DisplayText("Can not share route:")
    79  			return err
    80  		}
    81  	}
    82  
    83  	url := desiredURL(domain.Name, cmd.Hostname, path, 0)
    84  	cmd.UI.DisplayTextWithFlavor("Sharing route {{.URL}} to space {{.DestinationSpace}} as {{.User}}",
    85  		map[string]interface{}{
    86  			"URL":              url,
    87  			"DestinationSpace": cmd.DestinationSpace,
    88  			"User":             user.Name,
    89  		})
    90  	warnings, err = cmd.Actor.ShareRoute(
    91  		route.GUID,
    92  		targetedSpace.GUID,
    93  	)
    94  	cmd.UI.DisplayWarnings(warnings)
    95  	if err != nil {
    96  		return err
    97  	}
    98  	cmd.UI.DisplayOK()
    99  
   100  	return nil
   101  }