github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/commands/application/env.go (about) 1 package application 2 3 import ( 4 "encoding/json" 5 "sort" 6 7 . "github.com/cloudfoundry/cli/cf/i18n" 8 9 "github.com/cloudfoundry/cli/cf/api/applications" 10 "github.com/cloudfoundry/cli/cf/command_metadata" 11 "github.com/cloudfoundry/cli/cf/configuration/core_config" 12 "github.com/cloudfoundry/cli/cf/errors" 13 "github.com/cloudfoundry/cli/cf/requirements" 14 "github.com/cloudfoundry/cli/cf/terminal" 15 "github.com/codegangsta/cli" 16 ) 17 18 type Env struct { 19 ui terminal.UI 20 config core_config.Reader 21 appRepo applications.ApplicationRepository 22 } 23 24 func NewEnv(ui terminal.UI, config core_config.Reader, appRepo applications.ApplicationRepository) (cmd *Env) { 25 cmd = new(Env) 26 cmd.ui = ui 27 cmd.config = config 28 cmd.appRepo = appRepo 29 return 30 } 31 32 func (cmd *Env) Metadata() command_metadata.CommandMetadata { 33 return command_metadata.CommandMetadata{ 34 Name: "env", 35 ShortName: "e", 36 Description: T("Show all env variables for an app"), 37 Usage: T("CF_NAME env APP_NAME"), 38 } 39 } 40 41 func (cmd *Env) GetRequirements(requirementsFactory requirements.Factory, c *cli.Context) ([]requirements.Requirement, error) { 42 if len(c.Args()) != 1 { 43 cmd.ui.FailWithUsage(c) 44 } 45 46 return []requirements.Requirement{requirementsFactory.NewLoginRequirement()}, nil 47 } 48 49 func (cmd *Env) Run(c *cli.Context) { 50 app, err := cmd.appRepo.Read(c.Args()[0]) 51 if notFound, ok := err.(*errors.ModelNotFoundError); ok { 52 cmd.ui.Failed(notFound.Error()) 53 } 54 55 cmd.ui.Say(T("Getting env variables for app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", 56 map[string]interface{}{ 57 "AppName": terminal.EntityNameColor(app.Name), 58 "OrgName": terminal.EntityNameColor(cmd.config.OrganizationFields().Name), 59 "SpaceName": terminal.EntityNameColor(cmd.config.SpaceFields().Name), 60 "Username": terminal.EntityNameColor(cmd.config.Username())})) 61 62 env, err := cmd.appRepo.ReadEnv(app.Guid) 63 if err != nil { 64 cmd.ui.Failed(err.Error()) 65 } 66 67 cmd.ui.Ok() 68 cmd.ui.Say("") 69 70 cmd.displaySystemiAndAppProvidedEnvironment(env.System, env.Application) 71 cmd.ui.Say("") 72 cmd.displayUserProvidedEnvironment(env.Environment) 73 cmd.ui.Say("") 74 cmd.displayRunningEnvironment(env.Running) 75 cmd.ui.Say("") 76 cmd.displayStagingEnvironment(env.Staging) 77 cmd.ui.Say("") 78 } 79 80 func (cmd *Env) displaySystemiAndAppProvidedEnvironment(env map[string]interface{}, app map[string]interface{}) { 81 var vcapServices string 82 var vcapApplication string 83 84 servicesAsMap, ok := env["VCAP_SERVICES"].(map[string]interface{}) 85 if ok && len(servicesAsMap) > 0 { 86 jsonBytes, err := json.MarshalIndent(env, "", " ") 87 if err != nil { 88 cmd.ui.Failed(err.Error()) 89 } 90 vcapServices = string(jsonBytes) 91 } 92 93 applicationAsMap, ok := app["VCAP_APPLICATION"].(map[string]interface{}) 94 if ok && len(applicationAsMap) > 0 { 95 jsonBytes, err := json.MarshalIndent(app, "", " ") 96 if err != nil { 97 cmd.ui.Failed(err.Error()) 98 } 99 vcapApplication = string(jsonBytes) 100 } 101 102 if len(vcapServices) == 0 && len(vcapApplication) == 0 { 103 cmd.ui.Say(T("No system-provided env variables have been set")) 104 return 105 } 106 107 cmd.ui.Say(terminal.EntityNameColor(T("System-Provided:"))) 108 109 cmd.ui.Say(vcapServices) 110 cmd.ui.Say("") 111 cmd.ui.Say(vcapApplication) 112 } 113 114 func (cmd *Env) displayUserProvidedEnvironment(envVars map[string]interface{}) { 115 if len(envVars) == 0 { 116 cmd.ui.Say(T("No user-defined env variables have been set")) 117 return 118 } 119 120 keys := make([]string, 0, len(envVars)) 121 for key, _ := range envVars { 122 keys = append(keys, key) 123 } 124 sort.Strings(keys) 125 126 cmd.ui.Say(terminal.EntityNameColor(T("User-Provided:"))) 127 for _, key := range keys { 128 cmd.ui.Say("%s: %v", key, envVars[key]) 129 } 130 } 131 132 func (cmd *Env) displayRunningEnvironment(envVars map[string]interface{}) { 133 if len(envVars) == 0 { 134 cmd.ui.Say(T("No running env variables have been set")) 135 return 136 } 137 138 keys := make([]string, 0, len(envVars)) 139 for key, _ := range envVars { 140 keys = append(keys, key) 141 } 142 sort.Strings(keys) 143 144 cmd.ui.Say(terminal.EntityNameColor(T("Running Environment Variable Groups:"))) 145 for _, key := range keys { 146 cmd.ui.Say("%s: %v", key, envVars[key]) 147 } 148 } 149 150 func (cmd *Env) displayStagingEnvironment(envVars map[string]interface{}) { 151 if len(envVars) == 0 { 152 cmd.ui.Say(T("No staging env variables have been set")) 153 return 154 } 155 156 keys := make([]string, 0, len(envVars)) 157 for key, _ := range envVars { 158 keys = append(keys, key) 159 } 160 sort.Strings(keys) 161 162 cmd.ui.Say(terminal.EntityNameColor(T("Staging Environment Variable Groups:"))) 163 for _, key := range keys { 164 cmd.ui.Say("%s: %v", key, envVars[key]) 165 } 166 }