github.com/swisscom/cloudfoundry-cli@v7.1.0+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}}' for app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...",
    70  		map[string]interface{}{
    71  			"VarName":     terminal.EntityNameColor(varName),
    72  			"AppName":     terminal.EntityNameColor(app.Name),
    73  			"OrgName":     terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
    74  			"SpaceName":   terminal.EntityNameColor(cmd.config.SpaceFields().Name),
    75  			"CurrentUser": terminal.EntityNameColor(cmd.config.Username())}))
    76  
    77  	if len(app.EnvironmentVars) == 0 {
    78  		app.EnvironmentVars = map[string]interface{}{}
    79  	}
    80  	envParams := app.EnvironmentVars
    81  	envParams[varName] = varValue
    82  
    83  	_, err := cmd.appRepo.Update(app.GUID, models.AppParams{EnvironmentVars: &envParams})
    84  
    85  	if err != nil {
    86  		return err
    87  	}
    88  
    89  	cmd.ui.Ok()
    90  	cmd.ui.Say(T("TIP: Use '{{.Command}}' to ensure your env variable changes take effect",
    91  		map[string]interface{}{"Command": terminal.CommandColor(cf.Name + " restage " + app.Name)}))
    92  	return nil
    93  }