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