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

     1  package application
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/cf"
     7  	"code.cloudfoundry.org/cli/cf/api/applications"
     8  	"code.cloudfoundry.org/cli/cf/commandregistry"
     9  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    10  	"code.cloudfoundry.org/cli/cf/flags"
    11  	. "code.cloudfoundry.org/cli/cf/i18n"
    12  	"code.cloudfoundry.org/cli/cf/models"
    13  	"code.cloudfoundry.org/cli/cf/requirements"
    14  	"code.cloudfoundry.org/cli/cf/terminal"
    15  )
    16  
    17  type SetEnv struct {
    18  	ui      terminal.UI
    19  	config  coreconfig.Reader
    20  	appRepo applications.Repository
    21  	appReq  requirements.ApplicationRequirement
    22  }
    23  
    24  func init() {
    25  	commandregistry.Register(&SetEnv{})
    26  }
    27  
    28  func (cmd *SetEnv) MetaData() commandregistry.CommandMetadata {
    29  	return commandregistry.CommandMetadata{
    30  		Name:        "set-env",
    31  		ShortName:   "se",
    32  		Description: T("Set an env variable for an app"),
    33  		Usage: []string{
    34  			T("CF_NAME set-env APP_NAME ENV_VAR_NAME ENV_VAR_VALUE"),
    35  		},
    36  		SkipFlagParsing: true,
    37  	}
    38  }
    39  
    40  func (cmd *SetEnv) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    41  	if len(fc.Args()) != 3 {
    42  		cmd.ui.Failed(T("Incorrect Usage. Requires 'app-name env-name env-value' as arguments\n\n") + commandregistry.Commands.CommandUsage("set-env"))
    43  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 3)
    44  	}
    45  
    46  	cmd.appReq = requirementsFactory.NewApplicationRequirement(fc.Args()[0])
    47  
    48  	reqs := []requirements.Requirement{
    49  		requirementsFactory.NewLoginRequirement(),
    50  		requirementsFactory.NewTargetedSpaceRequirement(),
    51  		cmd.appReq,
    52  	}
    53  
    54  	return reqs, nil
    55  }
    56  
    57  func (cmd *SetEnv) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    58  	cmd.ui = deps.UI
    59  	cmd.config = deps.Config
    60  	cmd.appRepo = deps.RepoLocator.GetApplicationRepository()
    61  	return cmd
    62  }
    63  
    64  func (cmd *SetEnv) Execute(c flags.FlagContext) error {
    65  	varName := c.Args()[1]
    66  	varValue := c.Args()[2]
    67  	app := cmd.appReq.GetApplication()
    68  
    69  	cmd.ui.Say(T("Setting env variable '{{.VarName}}' to '{{.VarValue}}' for app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...",
    70  		map[string]interface{}{
    71  			"VarName":     terminal.EntityNameColor(varName),
    72  			"VarValue":    terminal.EntityNameColor(varValue),
    73  			"AppName":     terminal.EntityNameColor(app.Name),
    74  			"OrgName":     terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
    75  			"SpaceName":   terminal.EntityNameColor(cmd.config.SpaceFields().Name),
    76  			"CurrentUser": terminal.EntityNameColor(cmd.config.Username())}))
    77  
    78  	if len(app.EnvironmentVars) == 0 {
    79  		app.EnvironmentVars = map[string]interface{}{}
    80  	}
    81  	envParams := app.EnvironmentVars
    82  	envParams[varName] = varValue
    83  
    84  	_, err := cmd.appRepo.Update(app.GUID, models.AppParams{EnvironmentVars: &envParams})
    85  
    86  	if err != nil {
    87  		return err
    88  	}
    89  
    90  	cmd.ui.Ok()
    91  	cmd.ui.Say(T("TIP: Use '{{.Command}}' to ensure your env variable changes take effect",
    92  		map[string]interface{}{"Command": terminal.CommandColor(cf.Name + " restage " + app.Name)}))
    93  	return nil
    94  }