github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/command/v7/delete_route_command.go (about)

     1  package v7
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/actionerror"
     5  	"code.cloudfoundry.org/cli/actor/sharedaction"
     6  	"code.cloudfoundry.org/cli/actor/v7action"
     7  	"code.cloudfoundry.org/cli/command"
     8  	"code.cloudfoundry.org/cli/command/flag"
     9  	"code.cloudfoundry.org/cli/command/v7/shared"
    10  	"code.cloudfoundry.org/clock"
    11  )
    12  
    13  //go:generate counterfeiter . DeleteRouteActor
    14  
    15  type DeleteRouteActor interface {
    16  	DeleteRoute(domainName, hostname, path string) (v7action.Warnings, error)
    17  }
    18  
    19  type DeleteRouteCommand struct {
    20  	RequiredArgs    flag.Domain      `positional-args:"yes"`
    21  	usage           interface{}      `usage:"CF_NAME delete-route DOMAIN [--hostname HOSTNAME] [--path PATH] [-f]\n\nEXAMPLES:\n   CF_NAME delete-route example.com                             # example.com\n   CF_NAME delete-route example.com --hostname myhost            # myhost.example.com\n   CF_NAME delete-route example.com --hostname myhost --path foo # myhost.example.com/foo"`
    22  	Force           bool             `short:"f" description:"Force deletion without confirmation"`
    23  	Hostname        string           `long:"hostname" short:"n" description:"Hostname used to identify the HTTP route (required for shared domains)"`
    24  	Path            flag.V7RoutePath `long:"path" description:"Path used to identify the HTTP route"`
    25  	relatedCommands interface{}      `related_commands:"delete-orphaned-routes, routes, unmap-route"`
    26  
    27  	UI          command.UI
    28  	Config      command.Config
    29  	Actor       DeleteRouteActor
    30  	SharedActor command.SharedActor
    31  }
    32  
    33  func (cmd *DeleteRouteCommand) Setup(config command.Config, ui command.UI) error {
    34  	cmd.UI = ui
    35  	cmd.Config = config
    36  	sharedActor := sharedaction.NewActor(config)
    37  	cmd.SharedActor = sharedActor
    38  
    39  	ccClient, uaaClient, err := shared.GetNewClientsAndConnectToCF(config, ui, "")
    40  	if err != nil {
    41  		return err
    42  	}
    43  	cmd.Actor = v7action.NewActor(ccClient, config, sharedActor, uaaClient, clock.NewClock())
    44  	return nil
    45  }
    46  
    47  func (cmd DeleteRouteCommand) Execute(args []string) error {
    48  	err := cmd.SharedActor.CheckTarget(true, false)
    49  	if err != nil {
    50  		return err
    51  	}
    52  
    53  	_, err = cmd.Config.CurrentUser()
    54  	if err != nil {
    55  		return err
    56  	}
    57  
    58  	domain := cmd.RequiredArgs.Domain
    59  	hostname := cmd.Hostname
    60  	pathName := cmd.Path.Path
    61  	fqdn := desiredFQDN(domain, hostname, pathName)
    62  
    63  	cmd.UI.DisplayText("This action impacts all apps using this route.")
    64  	cmd.UI.DisplayText("Deleting the route will remove associated apps which will make apps with this route unreachable.")
    65  
    66  	if !cmd.Force {
    67  		response, promptErr := cmd.UI.DisplayBoolPrompt(false, "Really delete the route {{.FQDN}}?", map[string]interface{}{
    68  			"FQDN": fqdn,
    69  		})
    70  
    71  		if promptErr != nil {
    72  			return promptErr
    73  		}
    74  
    75  		if !response {
    76  			cmd.UI.DisplayText("'{{.FQDN}}' has not been deleted.", map[string]interface{}{
    77  				"FQDN": fqdn,
    78  			})
    79  			return nil
    80  		}
    81  	}
    82  
    83  	cmd.UI.DisplayTextWithFlavor("Deleting route {{.FQDN}}...",
    84  		map[string]interface{}{
    85  			"FQDN": fqdn,
    86  		})
    87  
    88  	warnings, err := cmd.Actor.DeleteRoute(domain, hostname, pathName)
    89  
    90  	cmd.UI.DisplayWarnings(warnings)
    91  	if err != nil {
    92  		if _, ok := err.(actionerror.RouteNotFoundError); ok {
    93  			cmd.UI.DisplayText(`Unable to delete. ` + err.Error())
    94  			cmd.UI.DisplayOK()
    95  			return nil
    96  		}
    97  		return err
    98  	}
    99  
   100  	cmd.UI.DisplayOK()
   101  	return nil
   102  }