github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/command/v2/delete_orphaned_routes_command.go (about)

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