func (cmd Start) waitForOneRunningInstance(app models.Application) { startupStartTime := time.Now() for { if time.Since(startupStartTime) > cmd.StartupTimeout { cmd.ui.Failed(fmt.Sprintf(T("Start app timeout\n\nTIP: use '{{.Command}}' for more information", map[string]interface{}{ "Command": terminal.CommandColor(fmt.Sprintf("%s logs %s --recent", cf.Name(), app.Name))}))) return } count, err := cmd.fetchInstanceCount(app.Guid) if err != nil { cmd.ui.Wait(cmd.PingerThrottle) continue } cmd.ui.Say(instancesDetails(count)) if count.running > 0 { return } if count.flapping > 0 { cmd.ui.Failed(fmt.Sprintf(T("Start unsuccessful\n\nTIP: use '{{.Command}}' for more information", map[string]interface{}{"Command": terminal.CommandColor(fmt.Sprintf("%s logs %s --recent", cf.Name(), app.Name))}))) return } cmd.ui.Wait(cmd.PingerThrottle) } }
func (req ApiEndpointRequirement) Execute() (success bool) { if req.config.ApiEndpoint() == "" { loginTip := terminal.CommandColor(fmt.Sprintf("%s login", cf.Name())) apiTip := terminal.CommandColor(fmt.Sprintf("%s api", cf.Name())) req.ui.Say("No API endpoint targeted. Use '%s' or '%s' to target an endpoint.", loginTip, apiTip) return false } return true }
func (req ApiEndpointRequirement) Execute() (success bool) { if req.config.ApiEndpoint() == "" { loginTip := terminal.CommandColor(fmt.Sprintf(T("{{.CFName}} login", map[string]interface{}{"CFName": cf.Name()}))) apiTip := terminal.CommandColor(fmt.Sprintf(T("{{.CFName}} api", map[string]interface{}{"CFName": cf.Name()}))) req.ui.Say(T("No API endpoint targeted. Use '{{.LoginTip}}' or '{{.APITip}}' to target an endpoint.", map[string]interface{}{ "LoginTip": loginTip, "APITip": apiTip, })) return false } return true }
func (req ApiEndpointRequirement) Execute() error { if req.config.ApiEndpoint() == "" { loginTip := terminal.CommandColor(fmt.Sprintf(T("{{.CFName}} login", map[string]interface{}{"CFName": cf.Name}))) apiTip := terminal.CommandColor(fmt.Sprintf(T("{{.CFName}} api", map[string]interface{}{"CFName": cf.Name}))) return errors.New(T("No API endpoint set. Use '{{.LoginTip}}' or '{{.APITip}}' to target an endpoint.", map[string]interface{}{ "LoginTip": loginTip, "APITip": apiTip, })) } return nil }
func (cmd *Start) waitForInstancesToStage(app models.Application) bool { stagingStartTime := time.Now() var err error if cmd.StagingTimeout == 0 { app, err = cmd.appRepo.GetApp(app.Guid) } else { for app.PackageState != "STAGED" && app.PackageState != "FAILED" && time.Since(stagingStartTime) < cmd.StagingTimeout { app, err = cmd.appRepo.GetApp(app.Guid) if err != nil { break } time.Sleep(cmd.PingerThrottle) } } if err != nil { cmd.ui.Failed(err.Error()) } if app.PackageState == "FAILED" { cmd.ui.Say("") if app.StagingFailedReason == "NoAppDetectedError" { cmd.ui.Failed(T(`{{.Err}} TIP: Buildpacks are detected when the "{{.PushCommand}}" is executed from within the directory that contains the app source code. Use '{{.BuildpackCommand}}' to see a list of supported buildpacks. Use '{{.Command}}' for more in depth log information.`, map[string]interface{}{ "Err": app.StagingFailedReason, "PushCommand": terminal.CommandColor(fmt.Sprintf("%s push", cf.Name())), "BuildpackCommand": terminal.CommandColor(fmt.Sprintf("%s buildpacks", cf.Name())), "Command": terminal.CommandColor(fmt.Sprintf("%s logs %s --recent", cf.Name(), app.Name))})) } else { cmd.ui.Failed(T("{{.Err}}\n\nTIP: use '{{.Command}}' for more information", map[string]interface{}{ "Err": app.StagingFailedReason, "Command": terminal.CommandColor(fmt.Sprintf("%s logs %s --recent", cf.Name(), app.Name))})) } } if time.Since(stagingStartTime) >= cmd.StagingTimeout { return false } return true }
func (cmd *UnsetEnv) Run(c *cli.Context) { varName := c.Args()[1] app := cmd.appReq.GetApplication() cmd.ui.Say("Removing env variable %s from app %s in org %s / space %s as %s...", terminal.EntityNameColor(varName), terminal.EntityNameColor(app.Name), terminal.EntityNameColor(cmd.config.OrganizationFields().Name), terminal.EntityNameColor(cmd.config.SpaceFields().Name), terminal.EntityNameColor(cmd.config.Username()), ) envParams := app.EnvironmentVars if _, ok := envParams[varName]; !ok { cmd.ui.Ok() cmd.ui.Warn("Env variable %s was not set.", varName) return } delete(envParams, varName) _, apiErr := cmd.appRepo.Update(app.Guid, models.AppParams{EnvironmentVars: &envParams}) if apiErr != nil { cmd.ui.Failed(apiErr.Error()) return } cmd.ui.Ok() cmd.ui.Say("TIP: Use '%s' to ensure your env variable changes take effect", terminal.CommandColor(cf.Name()+" push")) }
func (cmd Api) setApiEndpoint(endpoint string, skipSSL bool, cmdName string) { if strings.HasSuffix(endpoint, "/") { endpoint = strings.TrimSuffix(endpoint, "/") } cmd.config.SetSSLDisabled(skipSSL) endpoint, err := cmd.endpointRepo.UpdateEndpoint(endpoint) if err != nil { cmd.config.SetApiEndpoint("") cmd.config.SetSSLDisabled(false) switch typedErr := err.(type) { case *errors.InvalidSSLCert: cfApiCommand := terminal.CommandColor(fmt.Sprintf("%s %s --skip-ssl-validation", cf.Name, cmdName)) tipMessage := fmt.Sprintf(T("TIP: Use '{{.ApiCommand}}' to continue with an insecure API endpoint", map[string]interface{}{"ApiCommand": cfApiCommand})) cmd.ui.Failed(T("Invalid SSL Cert for {{.URL}}\n{{.TipMessage}}", map[string]interface{}{"URL": typedErr.URL, "TipMessage": tipMessage})) default: cmd.ui.Failed(typedErr.Error()) } } if !strings.HasPrefix(endpoint, "https://") { cmd.ui.Say(terminal.WarningColor(T("Warning: Insecure http API endpoint detected: secure https API endpoints are recommended\n"))) } }
func (cmd *SetEnv) Run(c *cli.Context) { varName := c.Args()[1] varValue := c.Args()[2] app := cmd.appReq.GetApplication() cmd.ui.Say("Setting env variable '%s' to '%s' for app %s in org %s / space %s as %s...", terminal.EntityNameColor(varName), terminal.EntityNameColor(varValue), terminal.EntityNameColor(app.Name), terminal.EntityNameColor(cmd.config.OrganizationFields().Name), terminal.EntityNameColor(cmd.config.SpaceFields().Name), terminal.EntityNameColor(cmd.config.Username()), ) if len(app.EnvironmentVars) == 0 { app.EnvironmentVars = map[string]string{} } envParams := app.EnvironmentVars envParams[varName] = varValue _, apiErr := cmd.appRepo.Update(app.Guid, models.AppParams{EnvironmentVars: &envParams}) if apiErr != nil { cmd.ui.Failed(apiErr.Error()) return } cmd.ui.Ok() cmd.ui.Say("TIP: Use '%s' to ensure your env variable changes take effect", terminal.CommandColor(cf.Name()+" push")) }
func (cmd *BindService) Run(c *cli.Context) { app := cmd.appReq.GetApplication() serviceInstance := cmd.serviceInstanceReq.GetServiceInstance() cmd.ui.Say("Binding service %s to app %s in org %s / space %s as %s...", terminal.EntityNameColor(serviceInstance.Name), terminal.EntityNameColor(app.Name), terminal.EntityNameColor(cmd.config.OrganizationFields().Name), terminal.EntityNameColor(cmd.config.SpaceFields().Name), terminal.EntityNameColor(cmd.config.Username()), ) err := cmd.BindApplication(app, serviceInstance) if err != nil { if err, ok := err.(errors.HttpError); ok && err.ErrorCode() == errors.APP_ALREADY_BOUND { cmd.ui.Ok() cmd.ui.Warn("App %s is already bound to %s.", app.Name, serviceInstance.Name) return } else { cmd.ui.Failed(err.Error()) } } cmd.ui.Ok() cmd.ui.Say("TIP: Use '%s' to ensure your env variable changes take effect", terminal.CommandColor(cf.Name()+" push")) }
func (cmd *SetEnv) Run(c *cli.Context) { varName := c.Args()[1] varValue := c.Args()[2] app := cmd.appReq.GetApplication() cmd.ui.Say(T("Setting env variable '{{.VarName}}' to '{{.VarValue}}' for app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...", map[string]interface{}{ "VarName": terminal.EntityNameColor(varName), "VarValue": terminal.EntityNameColor(varValue), "AppName": terminal.EntityNameColor(app.Name), "OrgName": terminal.EntityNameColor(cmd.config.OrganizationFields().Name), "SpaceName": terminal.EntityNameColor(cmd.config.SpaceFields().Name), "CurrentUser": terminal.EntityNameColor(cmd.config.Username())})) if len(app.EnvironmentVars) == 0 { app.EnvironmentVars = map[string]string{} } envParams := app.EnvironmentVars envParams[varName] = varValue _, apiErr := cmd.appRepo.Update(app.Guid, models.AppParams{EnvironmentVars: &envParams}) if apiErr != nil { cmd.ui.Failed(apiErr.Error()) return } cmd.ui.Ok() cmd.ui.Say(T("TIP: Use '{{.Command}}' to ensure your env variable changes take effect", map[string]interface{}{"Command": terminal.CommandColor(cf.Name() + " restage")})) }
func (cmd Api) Execute(c flags.FlagContext) { if c.Bool("unset") { cmd.ui.Say(T("Unsetting api endpoint...")) cmd.config.SetAPIEndpoint("") cmd.ui.Ok() cmd.ui.Say(T("\nNo api endpoint set.")) } else if len(c.Args()) == 0 { if cmd.config.APIEndpoint() == "" { cmd.ui.Say(fmt.Sprintf(T("No api endpoint set. Use '{{.Name}}' to set an endpoint", map[string]interface{}{"Name": terminal.CommandColor(cf.Name + " api")}))) } else { cmd.ui.Say(T("API endpoint: {{.APIEndpoint}} (API version: {{.APIVersion}})", map[string]interface{}{"APIEndpoint": terminal.EntityNameColor(cmd.config.APIEndpoint()), "APIVersion": terminal.EntityNameColor(cmd.config.APIVersion())})) } } else { endpoint := c.Args()[0] cmd.ui.Say(T("Setting api endpoint to {{.Endpoint}}...", map[string]interface{}{"Endpoint": terminal.EntityNameColor(endpoint)})) cmd.setAPIEndpoint(endpoint, c.Bool("skip-ssl-validation"), cmd.MetaData().Name) cmd.ui.Ok() cmd.ui.Say("") cmd.ui.ShowConfiguration(cmd.config) } }
func (cmd *Start) waitForOneRunningInstance(app models.Application) { startupStartTime := time.Now() for { if time.Since(startupStartTime) > cmd.StartupTimeout { tipMsg := T("Start app timeout\n\nTIP: Application must be listening on the right port. Instead of hard coding the port, use the $PORT environment variable.") + "\n\n" tipMsg += T("Use '{{.Command}}' for more information", map[string]interface{}{"Command": terminal.CommandColor(fmt.Sprintf("%s logs %s --recent", cf.Name(), app.Name))}) cmd.ui.Failed(tipMsg) return } count, err := cmd.fetchInstanceCount(app.Guid) if err != nil { cmd.ui.Warn("Could not fetch instance count: %s", err.Error()) time.Sleep(cmd.PingerThrottle) continue } cmd.ui.Say(instancesDetails(count)) if count.running > 0 { return } if count.flapping > 0 || count.crashed > 0 { cmd.ui.Failed(fmt.Sprintf(T("Start unsuccessful\n\nTIP: use '{{.Command}}' for more information", map[string]interface{}{"Command": terminal.CommandColor(fmt.Sprintf("%s logs %s --recent", cf.Name(), app.Name))}))) return } time.Sleep(cmd.PingerThrottle) } }
func (cmd *Start) waitForInstancesToStage(app models.Application) bool { stagingStartTime := time.Now() var err error if cmd.StagingTimeout == 0 { app, err = cmd.appRepo.GetApp(app.Guid) } else { for app.PackageState != "STAGED" && app.PackageState != "FAILED" && time.Since(stagingStartTime) < cmd.StagingTimeout { app, err = cmd.appRepo.GetApp(app.Guid) if err != nil { break } cmd.ui.Wait(cmd.PingerThrottle) } } if err != nil { cmd.ui.Failed(err.Error()) } if app.PackageState == "FAILED" { cmd.ui.Say("") cmd.ui.Failed(T("{{.Err}}\n\nTIP: use '{{.Command}}' for more information", map[string]interface{}{ "Err": app.StagingFailedReason, "Command": terminal.CommandColor(fmt.Sprintf("%s logs %s --recent", cf.Name(), app.Name))})) } if time.Since(stagingStartTime) >= cmd.StagingTimeout { return false } return true }
func (cmd API) setAPIEndpoint(endpoint string, skipSSL bool, cmdName string) error { if strings.HasSuffix(endpoint, "/") { endpoint = strings.TrimSuffix(endpoint, "/") } cmd.config.SetSSLDisabled(skipSSL) refresher := coreconfig.APIConfigRefresher{ Endpoint: endpoint, EndpointRepo: cmd.endpointRepo, Config: cmd.config, } warning, err := refresher.Refresh() if err != nil { cmd.config.SetAPIEndpoint("") cmd.config.SetSSLDisabled(false) switch typedErr := err.(type) { case *errors.InvalidSSLCert: cfAPICommand := terminal.CommandColor(fmt.Sprintf("%s %s --skip-ssl-validation", cf.Name, cmdName)) tipMessage := fmt.Sprintf(T("TIP: Use '{{.APICommand}}' to continue with an insecure API endpoint", map[string]interface{}{"APICommand": cfAPICommand})) return errors.New(T("Invalid SSL Cert for {{.URL}}\n{{.TipMessage}}", map[string]interface{}{"URL": typedErr.URL, "TipMessage": tipMessage})) default: return typedErr } } if warning != nil { cmd.ui.Say(terminal.WarningColor(warning.Warn())) } return nil }
func (cmd CreateOrg) Run(c *cli.Context) { name := c.Args()[0] cmd.ui.Say(T("Creating org {{.OrgName}} as {{.Username}}...", map[string]interface{}{ "OrgName": terminal.EntityNameColor(name), "Username": terminal.EntityNameColor(cmd.config.Username())})) org := models.Organization{OrganizationFields: models.OrganizationFields{Name: name}} quotaName := c.String("q") if quotaName != "" { quota, err := cmd.quotaRepo.FindByName(quotaName) if err != nil { cmd.ui.Failed(err.Error()) } org.QuotaDefinition.Guid = quota.Guid } err := cmd.orgRepo.Create(org) if err != nil { if apiErr, ok := err.(errors.HttpError); ok && apiErr.ErrorCode() == errors.ORG_EXISTS { cmd.ui.Ok() cmd.ui.Warn(T("Org {{.OrgName}} already exists", map[string]interface{}{"OrgName": name})) return } else { cmd.ui.Failed(err.Error()) } } cmd.ui.Ok() cmd.ui.Say(T("\nTIP: Use '{{.Command}}' to target new org", map[string]interface{}{"Command": terminal.CommandColor(cf.Name() + " target -o " + name)})) }
func (cmd *Start) waitForOneRunningInstance(app models.Application) error { timer := time.NewTimer(cmd.StartupTimeout) for { select { case <-timer.C: tipMsg := T("Start app timeout\n\nTIP: Application must be listening on the right port. Instead of hard coding the port, use the $PORT environment variable.") + "\n\n" tipMsg += T("Use '{{.Command}}' for more information", map[string]interface{}{"Command": terminal.CommandColor(fmt.Sprintf("%s logs %s --recent", cf.Name, app.Name))}) return errors.New(tipMsg) default: count, err := cmd.fetchInstanceCount(app.GUID) if err != nil { cmd.ui.Warn("Could not fetch instance count: %s", err.Error()) time.Sleep(cmd.PingerThrottle) continue } cmd.ui.Say(instancesDetails(count)) if count.running > 0 { return nil } if count.flapping > 0 || count.crashed > 0 { return fmt.Errorf(T("Start unsuccessful\n\nTIP: use '{{.Command}}' for more information", map[string]interface{}{"Command": terminal.CommandColor(fmt.Sprintf("%s logs %s --recent", cf.Name, app.Name))})) } time.Sleep(cmd.PingerThrottle) } } }
func (cmd *UpdateUserProvidedService) Execute(c flags.FlagContext) { serviceInstance := cmd.serviceInstanceReq.GetServiceInstance() if !serviceInstance.IsUserProvided() { cmd.ui.Failed(T("Service Instance is not user provided")) return } drainUrl := c.String("l") credentials := strings.Trim(c.String("p"), `'"`) routeServiceUrl := c.String("r") credentialsMap := make(map[string]interface{}) if c.IsSet("p") { jsonBytes, err := util.GetContentsFromFlagValue(credentials) if err != nil && strings.HasPrefix(credentials, "@") { cmd.ui.Failed(err.Error()) } if bytes.IndexAny(jsonBytes, "[{") != -1 { err = json.Unmarshal(jsonBytes, &credentialsMap) if err != nil { cmd.ui.Failed(T("JSON is invalid: {{.ErrorDescription}}", map[string]interface{}{"ErrorDescription": err.Error()})) } } else { for _, param := range strings.Split(credentials, ",") { param = strings.Trim(param, " ") credentialsMap[param] = cmd.ui.Ask("%s", param) } } } cmd.ui.Say(T("Updating user provided service {{.ServiceName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...", map[string]interface{}{ "ServiceName": terminal.EntityNameColor(serviceInstance.Name), "OrgName": terminal.EntityNameColor(cmd.config.OrganizationFields().Name), "SpaceName": terminal.EntityNameColor(cmd.config.SpaceFields().Name), "CurrentUser": terminal.EntityNameColor(cmd.config.Username()), })) serviceInstance.Params = credentialsMap serviceInstance.SysLogDrainUrl = drainUrl serviceInstance.RouteServiceUrl = routeServiceUrl apiErr := cmd.userProvidedServiceInstanceRepo.Update(serviceInstance.ServiceInstanceFields) if apiErr != nil { cmd.ui.Failed(apiErr.Error()) return } cmd.ui.Ok() cmd.ui.Say(T("TIP: Use '{{.CFRestageCommand}}' for any bound apps to ensure your env variable changes take effect", map[string]interface{}{ "CFRestageCommand": terminal.CommandColor(cf.Name() + " restage"), })) if routeServiceUrl == "" && credentials == "" && drainUrl == "" { cmd.ui.Warn(T("No flags specified. No changes were made.")) } }
func (cmd *UnsetEnv) Execute(c flags.FlagContext) { varName := c.Args()[1] app := cmd.appReq.GetApplication() cmd.ui.Say(T("Removing env variable {{.VarName}} from app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...", map[string]interface{}{ "VarName": terminal.EntityNameColor(varName), "AppName": terminal.EntityNameColor(app.Name), "OrgName": terminal.EntityNameColor(cmd.config.OrganizationFields().Name), "SpaceName": terminal.EntityNameColor(cmd.config.SpaceFields().Name), "CurrentUser": terminal.EntityNameColor(cmd.config.Username())})) envParams := app.EnvironmentVars if _, ok := envParams[varName]; !ok { cmd.ui.Ok() cmd.ui.Warn(T("Env variable {{.VarName}} was not set.", map[string]interface{}{"VarName": varName})) return } delete(envParams, varName) _, apiErr := cmd.appRepo.Update(app.Guid, models.AppParams{EnvironmentVars: &envParams}) if apiErr != nil { cmd.ui.Failed(apiErr.Error()) return } cmd.ui.Ok() cmd.ui.Say(T("TIP: Use '{{.Command}}' to ensure your env variable changes take effect", map[string]interface{}{"Command": terminal.CommandColor(cf.Name() + " restage")})) }
func (req TargetedSpaceRequirement) Execute() (success bool) { if !req.config.HasOrganization() { message := fmt.Sprintf("No org and space targeted, use '%s' to target an org and space", terminal.CommandColor(cf.Name()+" target -o ORG -s SPACE")) req.ui.Failed(message) return false } if !req.config.HasSpace() { message := fmt.Sprintf("No space targeted, use '%s' to target a space", terminal.CommandColor("cf target -s")) req.ui.Failed(message) return false } return true }
func (cmd *BindService) Run(c *cli.Context) { app := cmd.appReq.GetApplication() serviceInstance := cmd.serviceInstanceReq.GetServiceInstance() cmd.ui.Say(T("Binding service {{.ServiceInstanceName}} to app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...", map[string]interface{}{ "ServiceInstanceName": terminal.EntityNameColor(serviceInstance.Name), "AppName": terminal.EntityNameColor(app.Name), "OrgName": terminal.EntityNameColor(cmd.config.OrganizationFields().Name), "SpaceName": terminal.EntityNameColor(cmd.config.SpaceFields().Name), "CurrentUser": terminal.EntityNameColor(cmd.config.Username()), })) err := cmd.BindApplication(app, serviceInstance) if err != nil { if httperr, ok := err.(errors.HttpError); ok && httperr.ErrorCode() == errors.APP_ALREADY_BOUND { cmd.ui.Ok() cmd.ui.Warn(T("App {{.AppName}} is already bound to {{.ServiceName}}.", map[string]interface{}{ "AppName": app.Name, "ServiceName": serviceInstance.Name, })) return } else { cmd.ui.Failed(err.Error()) } } cmd.ui.Ok() cmd.ui.Say(T("TIP: Use '{{.CFCommand}}' to ensure your env variable changes take effect", map[string]interface{}{"CFCommand": terminal.CommandColor(cf.Name() + " restage")})) }
func (cmd CreateSpace) Run(c *cli.Context) { spaceName := c.Args()[0] orgName := c.String("o") orgGuid := "" if orgName == "" { orgName = cmd.config.OrganizationFields().Name orgGuid = cmd.config.OrganizationFields().Guid } cmd.ui.Say("Creating space %s in org %s as %s...", terminal.EntityNameColor(spaceName), terminal.EntityNameColor(orgName), terminal.EntityNameColor(cmd.config.Username()), ) if orgGuid == "" { org, apiErr := cmd.orgRepo.FindByName(orgName) switch apiErr.(type) { case nil: case *errors.ModelNotFoundError: cmd.ui.Failed("Org %s does not exist or is not accessible", orgName) return default: cmd.ui.Failed("Error finding org %s\n%s", orgName, apiErr.Error()) return } orgGuid = org.Guid } space, err := cmd.spaceRepo.Create(spaceName, orgGuid) if err != nil { if httpErr, ok := err.(errors.HttpError); ok && httpErr.ErrorCode() == errors.SPACE_EXISTS { cmd.ui.Ok() cmd.ui.Warn("Space %s already exists", spaceName) return } cmd.ui.Failed(err.Error()) return } cmd.ui.Ok() err = cmd.spaceRoleSetter.SetSpaceRole(space, models.SPACE_MANAGER, cmd.config.UserGuid(), cmd.config.Username()) if err != nil { cmd.ui.Failed(err.Error()) return } err = cmd.spaceRoleSetter.SetSpaceRole(space, models.SPACE_DEVELOPER, cmd.config.UserGuid(), cmd.config.Username()) if err != nil { cmd.ui.Failed(err.Error()) return } cmd.ui.Say("\nTIP: Use '%s' to target new space", terminal.CommandColor(cf.Name()+" target -o "+orgName+" -s "+space.Name)) }
func (req targetedOrgApiRequirement) Execute() (success bool) { if !req.config.HasOrganization() { message := fmt.Sprintf("No org targeted, use '%s' to target an org.", terminal.CommandColor(cf.Name()+" target -o ORG")) req.ui.Failed(message) return false } return true }
func (cmd Start) waitForOneRunningInstance(app models.Application) { var runningCount, startingCount, flappingCount, downCount int startupStartTime := time.Now() for runningCount == 0 { if time.Since(startupStartTime) > cmd.StartupTimeout { cmd.ui.Failed(fmt.Sprintf(T("Start app timeout\n\nTIP: use '{{.Command}}' for more information", map[string]interface{}{ "Command": terminal.CommandColor(fmt.Sprintf("%s logs %s --recent", cf.Name(), app.Name))}))) return } instances, apiErr := cmd.appInstancesRepo.GetInstances(app.Guid) if apiErr != nil { cmd.ui.Wait(cmd.PingerThrottle) continue } totalCount := len(instances) runningCount, startingCount, flappingCount, downCount = 0, 0, 0, 0 for _, inst := range instances { switch inst.State { case models.InstanceRunning: runningCount++ case models.InstanceStarting: startingCount++ case models.InstanceFlapping: flappingCount++ case models.InstanceDown: downCount++ } } cmd.ui.Say(instancesDetails(startingCount, downCount, runningCount, flappingCount, totalCount)) if flappingCount > 0 { cmd.ui.Failed(fmt.Sprintf(T("Start unsuccessful\n\nTIP: use '{{.Command}}' for more information", map[string]interface{}{"Command": terminal.CommandColor(fmt.Sprintf("%s logs %s --recent", cf.Name(), app.Name))}))) return } } }
func (cmd *UpdateUserProvidedService) Execute(c flags.FlagContext) error { serviceInstance := cmd.serviceInstanceReq.GetServiceInstance() if !serviceInstance.IsUserProvided() { return errors.New(T("Service Instance is not user provided")) } drainURL := c.String("l") credentials := strings.Trim(c.String("p"), `'"`) routeServiceURL := c.String("r") credentialsMap := make(map[string]interface{}) if c.IsSet("p") { jsonBytes, err := util.GetContentsFromFlagValue(credentials) if err != nil { return err } err = json.Unmarshal(jsonBytes, &credentialsMap) if err != nil { for _, param := range strings.Split(credentials, ",") { param = strings.Trim(param, " ") credentialsMap[param] = cmd.ui.Ask(param) } } } cmd.ui.Say(T("Updating user provided service {{.ServiceName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...", map[string]interface{}{ "ServiceName": terminal.EntityNameColor(serviceInstance.Name), "OrgName": terminal.EntityNameColor(cmd.config.OrganizationFields().Name), "SpaceName": terminal.EntityNameColor(cmd.config.SpaceFields().Name), "CurrentUser": terminal.EntityNameColor(cmd.config.Username()), })) serviceInstance.Params = credentialsMap serviceInstance.SysLogDrainURL = drainURL serviceInstance.RouteServiceURL = routeServiceURL err := cmd.userProvidedServiceInstanceRepo.Update(serviceInstance.ServiceInstanceFields) if err != nil { return err } cmd.ui.Ok() cmd.ui.Say(T("TIP: Use '{{.CFRestageCommand}}' for any bound apps to ensure your env variable changes take effect", map[string]interface{}{ "CFRestageCommand": terminal.CommandColor(cf.Name + " restage"), })) if routeServiceURL == "" && credentials == "" && drainURL == "" { cmd.ui.Warn(T("No flags specified. No changes were made.")) } return nil }
func printSuccessMessageForServiceInstance(serviceInstanceName string, serviceRepo api.ServiceRepository, ui terminal.UI) error { instance, apiErr := serviceRepo.FindInstanceByName(serviceInstanceName) if apiErr != nil { return apiErr } if instance.ServiceInstanceFields.LastOperation.State == "in progress" { ui.Ok() ui.Say("") ui.Say(T("{{.State}} in progress. Use '{{.ServicesCommand}}' or '{{.ServiceCommand}}' to check operation status.", map[string]interface{}{ "State": strings.Title(instance.ServiceInstanceFields.LastOperation.Type), "ServicesCommand": terminal.CommandColor("cf services"), "ServiceCommand": terminal.CommandColor(fmt.Sprintf("cf service %s", serviceInstanceName)), })) } else { ui.Ok() } return nil }
func (cmd *UpdateUserProvidedService) Execute(c flags.FlagContext) { serviceInstance := cmd.serviceInstanceReq.GetServiceInstance() if !serviceInstance.IsUserProvided() { cmd.ui.Failed(T("Service Instance is not user provided")) return } drainUrl := c.String("l") params := c.String("p") routeServiceUrl := c.String("r") paramsMap := make(map[string]interface{}) if params != "" { err := json.Unmarshal([]byte(params), ¶msMap) if err != nil { cmd.ui.Failed(T("JSON is invalid: {{.ErrorDescription}}", map[string]interface{}{"ErrorDescription": err.Error()})) return } } cmd.ui.Say(T("Updating user provided service {{.ServiceName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...", map[string]interface{}{ "ServiceName": terminal.EntityNameColor(serviceInstance.Name), "OrgName": terminal.EntityNameColor(cmd.config.OrganizationFields().Name), "SpaceName": terminal.EntityNameColor(cmd.config.SpaceFields().Name), "CurrentUser": terminal.EntityNameColor(cmd.config.Username()), })) serviceInstance.Params = paramsMap serviceInstance.SysLogDrainUrl = drainUrl serviceInstance.RouteServiceUrl = routeServiceUrl apiErr := cmd.userProvidedServiceInstanceRepo.Update(serviceInstance.ServiceInstanceFields) if apiErr != nil { cmd.ui.Failed(apiErr.Error()) return } cmd.ui.Ok() cmd.ui.Say(T("TIP: Use '{{.CFRestageCommand}}' for any bound apps to ensure your env variable changes take effect", map[string]interface{}{ "CFRestageCommand": terminal.CommandColor(cf.Name() + " restage"), })) if routeServiceUrl == "" && params == "" && drainUrl == "" { cmd.ui.Warn(T("No flags specified. No changes were made.")) } }
func (cmd Start) waitForInstancesToStage(app models.Application) { stagingStartTime := time.Now() _, err := cmd.appInstancesRepo.GetInstances(app.Guid) for err != nil && time.Since(stagingStartTime) < cmd.StagingTimeout { if httpError, ok := err.(errors.HttpError); ok && httpError.ErrorCode() != errors.APP_NOT_STAGED { cmd.ui.Say("") cmd.ui.Failed(fmt.Sprintf("%s\n\nTIP: use '%s' for more information", httpError.Error(), terminal.CommandColor(fmt.Sprintf("%s logs %s --recent", cf.Name(), app.Name)))) return } cmd.ui.Wait(cmd.PingerThrottle) _, err = cmd.appInstancesRepo.GetInstances(app.Guid) } return }
func (cmd Authenticate) Run(c *cli.Context) { cmd.config.ClearSession() cmd.authenticator.GetLoginPromptsAndSaveUAAServerURL() cmd.ui.Say(T("API endpoint: {{.ApiEndpoint}}", map[string]interface{}{"ApiEndpoint": terminal.EntityNameColor(cmd.config.ApiEndpoint())})) cmd.ui.Say(T("Authenticating...")) apiErr := cmd.authenticator.Authenticate(map[string]string{"username": c.Args()[0], "password": c.Args()[1]}) if apiErr != nil { cmd.ui.Failed(apiErr.Error()) return } cmd.ui.Ok() cmd.ui.Say(T("Use '{{.Name}}' to view or set your target org and space", map[string]interface{}{"Name": terminal.CommandColor(cf.Name() + " target")})) return }
func (cmd Start) waitForInstancesToStage(app models.Application) { stagingStartTime := time.Now() _, err := cmd.appInstancesRepo.GetInstances(app.Guid) for isStagingError(err) && time.Since(stagingStartTime) < cmd.StagingTimeout { cmd.ui.Wait(cmd.PingerThrottle) _, err = cmd.appInstancesRepo.GetInstances(app.Guid) } if err != nil && !isStagingError(err) { cmd.ui.Say("") cmd.ui.Failed(T("{{.Err}}\n\nTIP: use '{{.Command}}' for more information", map[string]interface{}{ "Err": err.Error(), "Command": terminal.CommandColor(fmt.Sprintf("%s logs %s --recent", cf.Name(), app.Name))})) } return }
func (cmd *Authenticate) Execute(c flags.FlagContext) error { cmd.config.ClearSession() cmd.authenticator.GetLoginPromptsAndSaveUAAServerURL() cmd.ui.Say(T("API endpoint: {{.APIEndpoint}}", map[string]interface{}{"APIEndpoint": terminal.EntityNameColor(cmd.config.APIEndpoint())})) cmd.ui.Say(T("Authenticating...")) err := cmd.authenticator.Authenticate(map[string]string{"username": c.Args()[0], "password": c.Args()[1]}) if err != nil { return err } cmd.ui.Ok() cmd.ui.Say(T("Use '{{.Name}}' to view or set your target org and space", map[string]interface{}{"Name": terminal.CommandColor(cf.Name + " target")})) cmd.ui.NotifyUpdateIfNeeded(cmd.config) return nil }