github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/cf/commands/application/set_health_check.go (about) 1 package application 2 3 import ( 4 "errors" 5 "fmt" 6 7 "github.com/liamawhite/cli-with-i18n/cf/api/applications" 8 "github.com/liamawhite/cli-with-i18n/cf/flags" 9 . "github.com/liamawhite/cli-with-i18n/cf/i18n" 10 "github.com/liamawhite/cli-with-i18n/cf/models" 11 12 "github.com/liamawhite/cli-with-i18n/cf/commandregistry" 13 "github.com/liamawhite/cli-with-i18n/cf/configuration/coreconfig" 14 "github.com/liamawhite/cli-with-i18n/cf/requirements" 15 "github.com/liamawhite/cli-with-i18n/cf/terminal" 16 ) 17 18 type SetHealthCheck struct { 19 ui terminal.UI 20 config coreconfig.Reader 21 appReq requirements.ApplicationRequirement 22 appRepo applications.Repository 23 } 24 25 func init() { 26 commandregistry.Register(&SetHealthCheck{}) 27 } 28 29 func (cmd *SetHealthCheck) MetaData() commandregistry.CommandMetadata { 30 return commandregistry.CommandMetadata{ 31 Name: "set-health-check", 32 Description: T("Set health_check_type flag to either 'port' or 'none'"), 33 Usage: []string{ 34 T("CF_NAME set-health-check APP_NAME 'port'|'none'"), 35 }, 36 } 37 } 38 39 func (cmd *SetHealthCheck) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) { 40 if len(fc.Args()) != 2 { 41 cmd.ui.Failed(T("Incorrect Usage. Requires APP_NAME and HEALTH_CHECK_TYPE as arguments\n\n") + commandregistry.Commands.CommandUsage("set-health-check")) 42 return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 2) 43 } 44 45 if fc.Args()[1] != "port" && fc.Args()[1] != "none" { 46 cmd.ui.Failed(T(`Incorrect Usage. HEALTH_CHECK_TYPE must be "port" or "none"\n\n`) + commandregistry.Commands.CommandUsage("set-health-check")) 47 return nil, fmt.Errorf("Incorrect usage: invalid healthcheck type") 48 } 49 50 cmd.appReq = requirementsFactory.NewApplicationRequirement(fc.Args()[0]) 51 52 reqs := []requirements.Requirement{ 53 requirementsFactory.NewLoginRequirement(), 54 requirementsFactory.NewTargetedSpaceRequirement(), 55 cmd.appReq, 56 } 57 58 return reqs, nil 59 } 60 61 func (cmd *SetHealthCheck) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command { 62 cmd.ui = deps.UI 63 cmd.config = deps.Config 64 cmd.appRepo = deps.RepoLocator.GetApplicationRepository() 65 return cmd 66 } 67 68 func (cmd *SetHealthCheck) Execute(fc flags.FlagContext) error { 69 healthCheckType := fc.Args()[1] 70 71 app := cmd.appReq.GetApplication() 72 73 if app.HealthCheckType == healthCheckType { 74 cmd.ui.Say(fmt.Sprintf("%s "+T("health_check_type is already set")+" to '%s'", app.Name, app.HealthCheckType)) 75 return nil 76 } 77 78 cmd.ui.Say(fmt.Sprintf(T(""), app.Name, healthCheckType)) 79 cmd.ui.Say(T("Updating {{.AppName}} health_check_type to '{{.HealthCheckType}}'", 80 map[string]interface{}{ 81 "AppName": app.Name, 82 "HealthCheckType": healthCheckType, 83 }, 84 )) 85 cmd.ui.Say("") 86 87 updatedApp, err := cmd.appRepo.Update(app.GUID, models.AppParams{HealthCheckType: &healthCheckType}) 88 if err != nil { 89 return errors.New(T("Error updating health_check_type for ") + app.Name + ": " + err.Error()) 90 } 91 92 if updatedApp.HealthCheckType == healthCheckType { 93 cmd.ui.Ok() 94 } else { 95 return errors.New(T("health_check_type is not set to ") + healthCheckType + T(" for ") + app.Name) 96 } 97 return nil 98 }