github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/cf/commands/application/env.go (about)

     1  package application
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"sort"
     7  
     8  	"code.cloudfoundry.org/cli/cf/commandregistry"
     9  	"code.cloudfoundry.org/cli/cf/flags"
    10  	. "code.cloudfoundry.org/cli/cf/i18n"
    11  
    12  	"code.cloudfoundry.org/cli/cf/api/applications"
    13  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    14  	"code.cloudfoundry.org/cli/cf/errors"
    15  	"code.cloudfoundry.org/cli/cf/requirements"
    16  	"code.cloudfoundry.org/cli/cf/terminal"
    17  )
    18  
    19  type Env struct {
    20  	ui      terminal.UI
    21  	config  coreconfig.Reader
    22  	appRepo applications.Repository
    23  }
    24  
    25  func init() {
    26  	commandregistry.Register(&Env{})
    27  }
    28  
    29  func (cmd *Env) MetaData() commandregistry.CommandMetadata {
    30  	return commandregistry.CommandMetadata{
    31  		Name:        "env",
    32  		ShortName:   "e",
    33  		Description: T("Show all env variables for an app"),
    34  		Usage: []string{
    35  			T("CF_NAME env APP_NAME"),
    36  		},
    37  	}
    38  }
    39  
    40  func (cmd *Env) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    41  	if len(fc.Args()) != 1 {
    42  		cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("env"))
    43  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1)
    44  	}
    45  
    46  	reqs := []requirements.Requirement{
    47  		requirementsFactory.NewLoginRequirement(),
    48  		requirementsFactory.NewTargetedSpaceRequirement(),
    49  	}
    50  
    51  	return reqs, nil
    52  }
    53  
    54  func (cmd *Env) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    55  	cmd.ui = deps.UI
    56  	cmd.config = deps.Config
    57  	cmd.appRepo = deps.RepoLocator.GetApplicationRepository()
    58  	return cmd
    59  }
    60  
    61  func (cmd *Env) Execute(c flags.FlagContext) error {
    62  	app, err := cmd.appRepo.Read(c.Args()[0])
    63  	if notFound, ok := err.(*errors.ModelNotFoundError); ok {
    64  		return notFound
    65  	}
    66  
    67  	cmd.ui.Say(T("Getting env variables for app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...",
    68  		map[string]interface{}{
    69  			"AppName":   terminal.EntityNameColor(app.Name),
    70  			"OrgName":   terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
    71  			"SpaceName": terminal.EntityNameColor(cmd.config.SpaceFields().Name),
    72  			"Username":  terminal.EntityNameColor(cmd.config.Username())}))
    73  
    74  	env, err := cmd.appRepo.ReadEnv(app.GUID)
    75  	if err != nil {
    76  		return err
    77  	}
    78  
    79  	cmd.ui.Ok()
    80  	cmd.ui.Say("")
    81  
    82  	err = cmd.displaySystemiAndAppProvidedEnvironment(env.System, env.Application)
    83  	if err != nil {
    84  		return err
    85  	}
    86  	cmd.ui.Say("")
    87  	cmd.displayUserProvidedEnvironment(env.Environment)
    88  	cmd.ui.Say("")
    89  	cmd.displayRunningEnvironment(env.Running)
    90  	cmd.ui.Say("")
    91  	cmd.displayStagingEnvironment(env.Staging)
    92  	cmd.ui.Say("")
    93  	return nil
    94  }
    95  
    96  func (cmd *Env) displaySystemiAndAppProvidedEnvironment(env map[string]interface{}, app map[string]interface{}) error {
    97  	var vcapServices string
    98  	var vcapApplication string
    99  
   100  	servicesAsMap, ok := env["VCAP_SERVICES"].(map[string]interface{})
   101  	if ok && len(servicesAsMap) > 0 {
   102  		jsonBytes, err := json.MarshalIndent(env, "", " ")
   103  		if err != nil {
   104  			return err
   105  		}
   106  		vcapServices = string(jsonBytes)
   107  	}
   108  
   109  	applicationAsMap, ok := app["VCAP_APPLICATION"].(map[string]interface{})
   110  	if ok && len(applicationAsMap) > 0 {
   111  		jsonBytes, err := json.MarshalIndent(app, "", " ")
   112  		if err != nil {
   113  			return err
   114  		}
   115  		vcapApplication = string(jsonBytes)
   116  	}
   117  
   118  	if len(vcapServices) == 0 && len(vcapApplication) == 0 {
   119  		cmd.ui.Say(T("No system-provided env variables have been set"))
   120  		return nil
   121  	}
   122  
   123  	cmd.ui.Say(terminal.EntityNameColor(T("System-Provided:")))
   124  
   125  	cmd.ui.Say(vcapServices)
   126  	cmd.ui.Say("")
   127  	cmd.ui.Say(vcapApplication)
   128  	return nil
   129  }
   130  
   131  func (cmd *Env) displayUserProvidedEnvironment(envVars map[string]interface{}) {
   132  	if len(envVars) == 0 {
   133  		cmd.ui.Say(T("No user-defined env variables have been set"))
   134  		return
   135  	}
   136  
   137  	keys := make([]string, 0, len(envVars))
   138  	for key := range envVars {
   139  		keys = append(keys, key)
   140  	}
   141  	sort.Strings(keys)
   142  
   143  	cmd.ui.Say(terminal.EntityNameColor(T("User-Provided:")))
   144  	for _, key := range keys {
   145  		cmd.ui.Say("%s: %v", key, envVars[key])
   146  	}
   147  }
   148  
   149  func (cmd *Env) displayRunningEnvironment(envVars map[string]interface{}) {
   150  	if len(envVars) == 0 {
   151  		cmd.ui.Say(T("No running env variables have been set"))
   152  		return
   153  	}
   154  
   155  	keys := make([]string, 0, len(envVars))
   156  	for key := range envVars {
   157  		keys = append(keys, key)
   158  	}
   159  	sort.Strings(keys)
   160  
   161  	cmd.ui.Say(terminal.EntityNameColor(T("Running Environment Variable Groups:")))
   162  	for _, key := range keys {
   163  		cmd.ui.Say("%s: %v", key, envVars[key])
   164  	}
   165  }
   166  
   167  func (cmd *Env) displayStagingEnvironment(envVars map[string]interface{}) {
   168  	if len(envVars) == 0 {
   169  		cmd.ui.Say(T("No staging env variables have been set"))
   170  		return
   171  	}
   172  
   173  	keys := make([]string, 0, len(envVars))
   174  	for key := range envVars {
   175  		keys = append(keys, key)
   176  	}
   177  	sort.Strings(keys)
   178  
   179  	cmd.ui.Say(terminal.EntityNameColor(T("Staging Environment Variable Groups:")))
   180  	for _, key := range keys {
   181  		cmd.ui.Say("%s: %v", key, envVars[key])
   182  	}
   183  }