github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/command/v2/delete_orphaned_routes_command.go (about)

     1  package v2
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/actionerror"
     5  	"code.cloudfoundry.org/cli/actor/sharedaction"
     6  	"code.cloudfoundry.org/cli/actor/v2action"
     7  	"code.cloudfoundry.org/cli/command"
     8  	"code.cloudfoundry.org/cli/command/v2/shared"
     9  )
    10  
    11  //go:generate counterfeiter . DeleteOrphanedRoutesActor
    12  
    13  type DeleteOrphanedRoutesActor interface {
    14  	GetOrphanedRoutesBySpace(spaceGUID string) ([]v2action.Route, v2action.Warnings, error)
    15  	DeleteRoute(routeGUID string) (v2action.Warnings, error)
    16  }
    17  
    18  type DeleteOrphanedRoutesCommand struct {
    19  	Force           bool        `short:"f" description:"Force deletion without confirmation"`
    20  	usage           interface{} `usage:"CF_NAME delete-orphaned-routes [-f]"`
    21  	relatedCommands interface{} `related_commands:"delete-route, routes"`
    22  
    23  	UI          command.UI
    24  	Actor       DeleteOrphanedRoutesActor
    25  	SharedActor command.SharedActor
    26  	Config      command.Config
    27  }
    28  
    29  func (cmd *DeleteOrphanedRoutesCommand) Setup(config command.Config, ui command.UI) error {
    30  	cmd.UI = ui
    31  	cmd.Config = config
    32  	cmd.SharedActor = sharedaction.NewActor(config)
    33  
    34  	ccClient, uaaClient, err := shared.NewClients(config, ui, true)
    35  	if err != nil {
    36  		return err
    37  	}
    38  	cmd.Actor = v2action.NewActor(ccClient, uaaClient, config)
    39  
    40  	return nil
    41  }
    42  
    43  func (cmd *DeleteOrphanedRoutesCommand) Execute(args []string) error {
    44  	err := cmd.SharedActor.CheckTarget(true, true)
    45  	if err != nil {
    46  		return err
    47  	}
    48  
    49  	user, err := cmd.Config.CurrentUser()
    50  	if err != nil {
    51  		return err
    52  	}
    53  
    54  	if !cmd.Force {
    55  		deleteOrphanedRoutes, promptErr := cmd.UI.DisplayBoolPrompt(false, "Really delete orphaned routes?")
    56  		if promptErr != nil {
    57  			return promptErr
    58  		}
    59  
    60  		if !deleteOrphanedRoutes {
    61  			return nil
    62  		}
    63  	}
    64  
    65  	cmd.UI.DisplayTextWithFlavor("Getting routes as {{.CurrentUser}} ...", map[string]interface{}{
    66  		"CurrentUser": user.Name,
    67  	})
    68  	cmd.UI.DisplayNewline()
    69  
    70  	routes, warnings, err := cmd.Actor.GetOrphanedRoutesBySpace(cmd.Config.TargetedSpace().GUID)
    71  	cmd.UI.DisplayWarnings(warnings)
    72  	if err != nil {
    73  		switch err.(type) {
    74  		case actionerror.OrphanedRoutesNotFoundError:
    75  		// Do nothing to parity the existing behavior
    76  		default:
    77  			return err
    78  		}
    79  	}
    80  
    81  	for _, route := range routes {
    82  		cmd.UI.DisplayText("Deleting route {{.Route}} ...", map[string]interface{}{
    83  			"Route": route.String(),
    84  		})
    85  
    86  		warnings, err = cmd.Actor.DeleteRoute(route.GUID)
    87  		cmd.UI.DisplayWarnings(warnings)
    88  		if err != nil {
    89  			return err
    90  		}
    91  	}
    92  
    93  	cmd.UI.DisplayOK()
    94  
    95  	return nil
    96  }