github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/route/delete_orphaned_routes.go (about) 1 package route 2 3 import ( 4 "github.com/cloudfoundry/cli/cf/api" 5 "github.com/cloudfoundry/cli/cf/command_registry" 6 "github.com/cloudfoundry/cli/cf/configuration/core_config" 7 . "github.com/cloudfoundry/cli/cf/i18n" 8 "github.com/cloudfoundry/cli/cf/models" 9 "github.com/cloudfoundry/cli/cf/requirements" 10 "github.com/cloudfoundry/cli/cf/terminal" 11 "github.com/cloudfoundry/cli/flags" 12 "github.com/cloudfoundry/cli/flags/flag" 13 ) 14 15 type DeleteOrphanedRoutes struct { 16 ui terminal.UI 17 routeRepo api.RouteRepository 18 config core_config.Reader 19 } 20 21 func init() { 22 command_registry.Register(&DeleteOrphanedRoutes{}) 23 } 24 25 func (cmd *DeleteOrphanedRoutes) MetaData() command_registry.CommandMetadata { 26 fs := make(map[string]flags.FlagSet) 27 fs["f"] = &cliFlags.BoolFlag{Name: "f", Usage: T("Force deletion without confirmation")} 28 29 return command_registry.CommandMetadata{ 30 Name: "delete-orphaned-routes", 31 Description: T("Delete all orphaned routes (e.g.: those that are not mapped to an app)"), 32 Usage: T("CF_NAME delete-orphaned-routes [-f]"), 33 Flags: fs, 34 } 35 } 36 37 func (cmd *DeleteOrphanedRoutes) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) (reqs []requirements.Requirement, err error) { 38 if len(fc.Args()) != 0 { 39 cmd.ui.Failed(T("Incorrect Usage. No argument required\n\n") + command_registry.Commands.CommandUsage("delete-orphaned-routes")) 40 } 41 42 reqs = append(reqs, requirementsFactory.NewLoginRequirement()) 43 return 44 } 45 46 func (cmd *DeleteOrphanedRoutes) SetDependency(deps command_registry.Dependency, pluginCall bool) command_registry.Command { 47 cmd.ui = deps.Ui 48 cmd.config = deps.Config 49 cmd.routeRepo = deps.RepoLocator.GetRouteRepository() 50 return cmd 51 } 52 53 func (cmd *DeleteOrphanedRoutes) Execute(c flags.FlagContext) { 54 force := c.Bool("f") 55 if !force { 56 response := cmd.ui.Confirm(T("Really delete orphaned routes?{{.Prompt}}", 57 map[string]interface{}{"Prompt": terminal.PromptColor(">")})) 58 59 if !response { 60 return 61 } 62 } 63 64 cmd.ui.Say(T("Getting routes as {{.Username}} ...\n", 65 map[string]interface{}{"Username": terminal.EntityNameColor(cmd.config.Username())})) 66 67 apiErr := cmd.routeRepo.ListRoutes(func(route models.Route) bool { 68 69 if len(route.Apps) == 0 { 70 cmd.ui.Say(T("Deleting route {{.Route}}...", 71 map[string]interface{}{"Route": terminal.EntityNameColor(route.Host + "." + route.Domain.Name)})) 72 apiErr := cmd.routeRepo.Delete(route.Guid) 73 if apiErr != nil { 74 cmd.ui.Failed(apiErr.Error()) 75 return false 76 } 77 } 78 return true 79 }) 80 81 if apiErr != nil { 82 cmd.ui.Failed(T("Failed fetching routes.\n{{.Err}}", map[string]interface{}{"Err": apiErr.Error()})) 83 return 84 } 85 cmd.ui.Ok() 86 }