func (ui terminalUI) ShowConfiguration(config configuration.Reader) { if config.HasAPIEndpoint() { ui.Say("API endpoint: %s (API version: %s)", EntityNameColor(config.ApiEndpoint()), EntityNameColor(config.ApiVersion())) } if !config.IsLoggedIn() { ui.Say(NotLoggedInText()) return } else { ui.Say("User: %s", EntityNameColor(config.UserEmail())) } if !config.HasOrganization() && !config.HasSpace() { command := fmt.Sprintf("%s target -o ORG -s SPACE", cf.Name()) ui.Say("No org or space targeted, use '%s'", CommandColor(command)) return } if config.HasOrganization() { ui.Say("Org: %s", EntityNameColor(config.OrganizationFields().Name)) } else { command := fmt.Sprintf("%s target -o Org", cf.Name()) ui.Say("Org: No org targeted, use '%s'", CommandColor(command)) } if config.HasSpace() { ui.Say("Space: %s", EntityNameColor(config.SpaceFields().Name)) } else { command := fmt.Sprintf("%s target -s SPACE", cf.Name()) ui.Say("Space: No space targeted, use '%s'", CommandColor(command)) } }
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 loadConfig(termUI terminal.UI, configRepo configuration.ConfigurationRepository) (config *configuration.Configuration) { config, err := configRepo.Get() if err != nil { termUI.Failed(fmt.Sprintf( "Error loading config. Please reset target (%s) and log in (%s).", terminal.CommandColor(fmt.Sprintf("%s target", cf.Name())), terminal.CommandColor(fmt.Sprintf("%s login", cf.Name())), )) configRepo.Delete() os.Exit(1) return } return }
func (cmd *DeleteSpace) Run(c *cli.Context) { spaceName := c.Args()[0] if !c.Bool("f") { if !cmd.ui.ConfirmDelete("space", spaceName) { return } } cmd.ui.Say("Deleting space %s in org %s as %s...", terminal.EntityNameColor(spaceName), terminal.EntityNameColor(cmd.config.OrganizationFields().Name), terminal.EntityNameColor(cmd.config.Username()), ) space := cmd.spaceReq.GetSpace() apiErr := cmd.spaceRepo.Delete(space.Guid) if apiErr != nil { cmd.ui.Failed(apiErr.Error()) return } cmd.ui.Ok() if cmd.config.SpaceFields().Name == spaceName { cmd.config.SetSpaceFields(models.SpaceFields{}) cmd.ui.Say("TIP: No space targeted, use '%s target -s' to target a space", cf.Name()) } return }
func (cmd Target) setSpace(spaceName string) (err error) { if !cmd.config.IsLoggedIn() { cmd.ui.Failed("You must be logged in to set a space. Use '%s login'.", cf.Name()) return } if !cmd.config.HasOrganization() { cmd.ui.Failed("An org must be targeted before targeting a space") return } space, apiResponse := cmd.spaceRepo.FindByName(spaceName) if apiResponse.IsNotSuccessful() { cmd.ui.Failed("Unable to access space %s.\n%s", spaceName, apiResponse.Message) return } err = cmd.configRepo.SetSpace(space.SpaceFields) if err != nil { cmd.ui.Failed("Error setting space in config file.\n%s", err) return } return }
func (cmd Api) setApiEndpoint(endpoint string, skipSSL bool) { 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 api --skip-ssl-validation", cf.Name())) tipMessage := fmt.Sprintf("TIP: Use '%s' to continue with an insecure API endpoint", cfApiCommand) cmd.ui.Failed("Invalid SSL Cert for %s\n%s", typedErr.URL, tipMessage) default: cmd.ui.Failed(typedErr.Error()) } } if !strings.HasPrefix(endpoint, "https://") { cmd.ui.Say(terminal.WarningColor("Warning: Insecure http API endpoint detected: secure https API endpoints are recommended\n")) } }
func displayCrashDialog() { formattedString := ` d8888 888 888 888 888 .d8888b. 888 888 888 888 .d8888b. 888 d8P .d8888b. d88888 888 o 888 888 o 888 d88P Y88b 888 888 888 888 d88P Y88b 888 d8P d88P Y88b d88P888 888 d8b 888 888 d8b 888 Y88b. 888 888 888 888 888 888 888 d8P Y88b. d88P 888 888 d888b 888 888 d888b 888 "Y888b. 8888888888 888 888 888 888d88K "Y888b. d88P 888 888d88888b888 888d88888b888 "Y88b. 888 888 888 888 888 8888888b "Y88b. d88P 888 88888P Y88888 88888P Y88888 "888 888 888 888 888 888 888 888 Y88b "888 d8888888888 8888P Y8888 8888P Y8888 Y88b d88P 888 888 Y88b. .d88P Y88b d88P 888 Y88b Y88b d88P d8b d88P 888 888P Y888 888P Y888 "Y8888P" 888 888 "Y88888P" "Y8888P" 888 Y88b "Y8888P" Y8P Something completely unexpected happened. This is a bug in %s. Please file this bug : https://github.com/cloudfoundry/cli/issues Tell us that you ran this command: %s and got this stack trace: %s ` stackTrace := "\t" + strings.Replace(string(debug.Stack()), "\n", "\n\t", -1) println(fmt.Sprintf(formattedString, cf.Name(), strings.Join(os.Args, " "), stackTrace)) os.Exit(1) }
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.(cferrors.HttpError); ok && err.ErrorCode() == AppAlreadyBoundErrorCode { 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 push' to ensure your env variable changes take effect", cf.Name()) }
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()), ) apiResponse := cmd.BindApplication(app, serviceInstance) if apiResponse.IsNotSuccessful() && apiResponse.ErrorCode != AppAlreadyBoundErrorCode { cmd.ui.Failed(apiResponse.Message) } cmd.ui.Ok() if apiResponse.ErrorCode == AppAlreadyBoundErrorCode { cmd.ui.Warn("App %s is already bound to %s.", app.Name, serviceInstance.Name) return } cmd.ui.Say("TIP: Use '%s push' to ensure your env variable changes take effect", cf.Name()) }
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.Organization.Name), terminal.EntityNameColor(cmd.config.Space.Name), terminal.EntityNameColor(cmd.config.Username()), ) envVars := app.EnvironmentVars if !envVarFound(varName, envVars) { cmd.ui.Ok() cmd.ui.Warn("Env variable %s was not set.", varName) return } delete(envVars, varName) apiResponse := cmd.appRepo.SetEnv(app, envVars) if apiResponse.IsNotSuccessful() { cmd.ui.Failed(apiResponse.Message) return } cmd.ui.Ok() cmd.ui.Say("TIP: Use '%s push' to ensure your env variable changes take effect", cf.Name()) }
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, apiErr := cmd.spaceRepo.Create(spaceName, orgGuid) if apiErr != nil { if apiErr.ErrorCode() == cf.SPACE_EXISTS { cmd.ui.Ok() cmd.ui.Warn("Space %s already exists", spaceName) return } cmd.ui.Failed(apiErr.Error()) return } cmd.ui.Ok() var err error 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 (cmd *DeleteSpace) Run(c *cli.Context) { spaceName := c.Args()[0] force := c.Bool("f") cmd.ui.Say("Deleting space %s in org %s as %s...", terminal.EntityNameColor(spaceName), terminal.EntityNameColor(cmd.config.Organization.Name), terminal.EntityNameColor(cmd.config.Username()), ) space, apiResponse := cmd.spaceRepo.FindByName(spaceName) if apiResponse.IsError() { cmd.ui.Failed(apiResponse.Message) return } if apiResponse.IsNotFound() { cmd.ui.Ok() cmd.ui.Warn("Space %s does not exist.", spaceName) return } if !force { response := cmd.ui.Confirm( "Really delete space %s and everything associated with it?%s", terminal.EntityNameColor(spaceName), terminal.PromptColor(">"), ) if !response { return } } apiResponse = cmd.spaceRepo.Delete(space) if apiResponse.IsNotSuccessful() { cmd.ui.Failed(apiResponse.Message) return } cmd.ui.Ok() config, err := cmd.configRepo.Get() if err != nil { cmd.ui.ConfigFailure(err) return } if config.Space.Name == spaceName { config.Space = cf.Space{} cmd.configRepo.Save() cmd.ui.Say("TIP: No space targeted, use '%s target -s' to target a space", cf.Name()) } return }
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 Target) Run(c *cli.Context) { orgName := c.String("o") spaceName := c.String("s") shouldShowTarget := (orgName == "" && spaceName == "") if shouldShowTarget { cmd.ui.ShowConfiguration(cmd.config) if !cmd.config.HasOrganization() { cmd.ui.Say("No org targeted, use '%s target -o' to target an org", cf.Name()) } if !cmd.config.HasSpace() { cmd.ui.Say("No space targeted, use '%s target -s' to target a space", cf.Name()) } return } if orgName != "" { err := cmd.setOrganization(orgName) if spaceName == "" && cmd.config.IsLoggedIn() { cmd.showConfig() cmd.ui.Say("No space targeted, use '%s target -s' to target a space", cf.Name()) return } if err != nil { return } } if spaceName != "" { err := cmd.setSpace(spaceName) if err != nil { return } } cmd.showConfig() return }
func getCommand(metadata command_metadata.CommandMetadata, runner command_runner.Runner) cli.Command { return cli.Command{ Name: metadata.Name, ShortName: metadata.ShortName, Description: metadata.Description, Usage: strings.Replace(metadata.Usage, "CF_NAME", cf.Name(), -1), Action: func(context *cli.Context) { runner.RunCmdByName(metadata.Name, context) }, Flags: metadata.Flags, } }
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 Start) waitForInstancesToStage(app models.Application) { stagingStartTime := time.Now() _, apiResponse := cmd.appInstancesRepo.GetInstances(app.Guid) for apiResponse.IsNotSuccessful() && time.Since(stagingStartTime) < cmd.StagingTimeout { if apiResponse.ErrorCode != cf.APP_NOT_STAGED { cmd.ui.Say("") cmd.ui.Failed(fmt.Sprintf("%s\n\nTIP: use '%s' for more information", apiResponse.Message, terminal.CommandColor(fmt.Sprintf("%s logs %s --recent", cf.Name(), app.Name)))) return } cmd.ui.Wait(cmd.PingerThrottle) _, apiResponse = cmd.appInstancesRepo.GetInstances(app.Guid) } return }
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 err, ok := err.(errors.HttpError); ok && err.ErrorCode() != errors.APP_NOT_STAGED { cmd.ui.Say("") cmd.ui.Failed(fmt.Sprintf("%s\n\nTIP: use '%s' for more information", err.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 (ui terminalUI) ShowConfiguration(config *configuration.Configuration) { ui.Say("API endpoint: %s (API version: %s)", EntityNameColor(config.Target), EntityNameColor(config.ApiVersion)) if !config.IsLoggedIn() { ui.Say("Logged out, use '%s' to login", CommandColor(cf.Name()+" login USERNAME")) } else { ui.Say("User: %s", EntityNameColor(config.UserEmail())) } if config.HasOrganization() { ui.Say("Org: %s", EntityNameColor(config.Organization.Name)) } if config.HasSpace() { ui.Say("Space: %s", EntityNameColor(config.Space.Name)) } }
func (cmd CreateUserFields) Run(c *cli.Context) { username := c.Args()[0] password := c.Args()[1] cmd.ui.Say("Creating user %s as %s...", terminal.EntityNameColor(username), terminal.EntityNameColor(cmd.config.Username()), ) apiErr := cmd.userRepo.Create(username, password) if apiErr != nil { cmd.ui.Failed("Error creating user %s.\n%s", terminal.EntityNameColor(username), apiErr.Error()) return } cmd.ui.Ok() cmd.ui.Say("\nTIP: Assign roles with '%s set-org-role' and '%s set-space-role'", cf.Name(), cf.Name()) }
func (cmd Target) setOrganization(orgName string) (err error) { if !cmd.config.IsLoggedIn() { cmd.ui.Failed("You must be logged in to target an org. Use '%s login'.", cf.Name()) return } org, apiResponse := cmd.orgRepo.FindByName(orgName) if apiResponse.IsNotSuccessful() { cmd.ui.Failed("Could not target org.\n%s", apiResponse.Message) return } err = cmd.configRepo.SetOrganization(org) if err != nil { cmd.ui.Failed("Error setting org in config file.\n%s", apiResponse.Message) return } return }
func displayCrashDialog() { formattedString := ` %s Something completely unexpected happened. This is a bug in %s. Please file this bug : https://github.com/cloudfoundry/cli/issues Tell us that you ran this command: %s and got this stack trace: %s ` stackTrace := "\t" + strings.Replace(string(debug.Stack()), "\n", "\n\t", -1) println(fmt.Sprintf(formattedString, awwShucks(), cf.Name(), strings.Join(os.Args, " "), stackTrace)) os.Exit(1) }
func (cmd *CreateDomain) Run(c *cli.Context) { domainName := c.Args()[1] owningOrg := cmd.orgReq.GetOrganization() cmd.ui.Say("Creating domain %s for org %s as %s...", terminal.EntityNameColor(domainName), terminal.EntityNameColor(owningOrg.Name), terminal.EntityNameColor(cmd.config.Username()), ) _, apiResponse := cmd.domainRepo.Create(domainName, owningOrg.Guid) if apiResponse.IsNotSuccessful() { cmd.ui.Failed(apiResponse.Message) return } cmd.ui.Ok() cmd.ui.Say("TIP: Use '%s' to assign it to a space", terminal.CommandColor(cf.Name()+" map-domain")) }
func NewApp(cmdRunner command_runner.Runner, metadatas ...command_metadata.CommandMetadata) (app *cli.App) { helpCommand := cli.Command{ Name: "help", ShortName: "h", Description: "Show help", Usage: fmt.Sprintf("%s help [COMMAND]", cf.Name()), Action: func(c *cli.Context) { args := c.Args() if len(args) > 0 { cli.ShowCommandHelp(c, args[0]) } else { showAppHelp(appHelpTemplate, c.App) } }, } cli.HelpPrinter = showAppHelp cli.AppHelpTemplate = appHelpTemplate trace.Logger.Printf("\n%s\n%s\n\n", terminal.HeaderColor("VERSION:"), cf.Version) app = cli.NewApp() app.Usage = cf.Usage app.Version = cf.Version app.Action = helpCommand.Action compiledAtTime, err := time.Parse("Jan 2, 2006 3:04PM", cf.BuiltOnDate) if err == nil { app.Compiled = compiledAtTime } else { err = nil app.Compiled = time.Now() } app.Commands = []cli.Command{helpCommand} for _, metadata := range metadatas { app.Commands = append(app.Commands, getCommand(metadata, cmdRunner)) } return }
func (cmd CreateUser) Run(c *cli.Context) { username := c.Args()[0] password := c.Args()[1] cmd.ui.Say("Creating user %s as %s...", terminal.EntityNameColor(username), terminal.EntityNameColor(cmd.config.Username()), ) err := cmd.userRepo.Create(username, password) switch err.(type) { case nil: case *errors.ModelAlreadyExistsError: cmd.ui.Warn("%s", err.Error()) default: cmd.ui.Failed("Error creating user %s.\n%s", terminal.EntityNameColor(username), err.Error()) } cmd.ui.Ok() cmd.ui.Say("\nTIP: Assign roles with '%s set-org-role' and '%s set-space-role'", cf.Name(), cf.Name()) }
func (cmd CreateOrg) Run(c *cli.Context) { name := c.Args()[0] cmd.ui.Say("Creating org %s as %s...", terminal.EntityNameColor(name), terminal.EntityNameColor(cmd.config.Username()), ) err := cmd.orgRepo.Create(name) if err != nil { if apiErr, ok := err.(errors.HttpError); ok && apiErr.ErrorCode() == errors.ORG_EXISTS { cmd.ui.Ok() cmd.ui.Warn("Org %s already exists", name) return } else { cmd.ui.Failed(err.Error()) } } cmd.ui.Ok() cmd.ui.Say("\nTIP: Use '%s' to target new org", terminal.CommandColor(cf.Name()+" target -o "+name)) }
func (cmd CreateOrg) Run(c *cli.Context) { name := c.Args()[0] cmd.ui.Say("Creating org %s as %s...", terminal.EntityNameColor(name), terminal.EntityNameColor(cmd.config.Username()), ) apiResponse := cmd.orgRepo.Create(name) if apiResponse.IsNotSuccessful() { if apiResponse.ErrorCode == cf.ORG_EXISTS { cmd.ui.Ok() cmd.ui.Warn("Org %s already exists", name) return } cmd.ui.Failed(apiResponse.Message) return } cmd.ui.Ok() cmd.ui.Say("\nTIP: Use '%s' to target new org", terminal.CommandColor(cf.Name()+" target -o "+name)) }
func (cmd CreateSpace) Run(c *cli.Context) { spaceName := c.Args()[0] cmd.ui.Say("Creating space %s in org %s as %s...", terminal.EntityNameColor(spaceName), terminal.EntityNameColor(cmd.config.Organization.Name), terminal.EntityNameColor(cmd.config.Username()), ) apiResponse := cmd.spaceRepo.Create(spaceName) if apiResponse.IsNotSuccessful() { if apiResponse.ErrorCode == cf.SPACE_EXISTS { cmd.ui.Ok() cmd.ui.Warn("Space %s already exists", spaceName) return } cmd.ui.Failed(apiResponse.Message) return } cmd.ui.Ok() cmd.ui.Say("\nTIP: Use '%s' to target new space", terminal.CommandColor(cf.Name()+" target -s "+spaceName)) }
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 for app %s in org %s / space %s as %s...", terminal.EntityNameColor(varName), terminal.EntityNameColor(app.Name), terminal.EntityNameColor(cmd.config.Organization.Name), terminal.EntityNameColor(cmd.config.Space.Name), terminal.EntityNameColor(cmd.config.Username()), ) var envVars map[string]string if app.EnvironmentVars != nil { envVars = app.EnvironmentVars } else { envVars = map[string]string{} } if envVarFound(varName, envVars) { cmd.ui.Ok() cmd.ui.Warn("Env var %s was already set.", varName) return } envVars[varName] = varValue apiResponse := cmd.appRepo.SetEnv(app, envVars) if apiResponse.IsNotSuccessful() { cmd.ui.Failed(apiResponse.Message) return } cmd.ui.Ok() cmd.ui.Say("TIP: Use '%s push' to ensure your env variable changes take effect", cf.Name()) }
func (cmd Target) setSpace(spaceName string) (err error) { if !cmd.config.IsLoggedIn() { cmd.ui.Failed("You must be logged in to set a space. Use '%s login'.", cf.Name()) return } if !cmd.config.HasOrganization() { cmd.ui.Failed("An org must be targeted before targeting a space") return } space, apiResponse := cmd.spaceRepo.FindByName(spaceName) if apiResponse.IsNotSuccessful() { cmd.ui.Failed("Unable to access space %s.\n%s", spaceName, apiResponse.Message) return } cmd.config.Space = space cmd.saveConfig() return }