github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/application/unset_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 UnsetEnv 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(&UnsetEnv{})
    26  }
    27  
    28  func (cmd *UnsetEnv) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    29  	cmd.ui = deps.UI
    30  	cmd.config = deps.Config
    31  	cmd.appRepo = deps.RepoLocator.GetApplicationRepository()
    32  	return cmd
    33  }
    34  
    35  func (cmd *UnsetEnv) MetaData() commandregistry.CommandMetadata {
    36  	return commandregistry.CommandMetadata{
    37  		Name:        "unset-env",
    38  		Description: T("Remove an env variable"),
    39  		Usage: []string{
    40  			T("CF_NAME unset-env APP_NAME ENV_VAR_NAME"),
    41  		},
    42  	}
    43  }
    44  
    45  func (cmd *UnsetEnv) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    46  	if len(fc.Args()) != 2 {
    47  		cmd.ui.Failed(T("Incorrect Usage. Requires 'app-name env-name' as arguments\n\n") + commandregistry.Commands.CommandUsage("unset-env"))
    48  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 2)
    49  	}
    50  
    51  	cmd.appReq = requirementsFactory.NewApplicationRequirement(fc.Args()[0])
    52  
    53  	reqs := []requirements.Requirement{
    54  		requirementsFactory.NewLoginRequirement(),
    55  		requirementsFactory.NewTargetedSpaceRequirement(),
    56  		cmd.appReq,
    57  	}
    58  
    59  	return reqs, nil
    60  }
    61  
    62  func (cmd *UnsetEnv) Execute(c flags.FlagContext) error {
    63  	varName := c.Args()[1]
    64  	app := cmd.appReq.GetApplication()
    65  
    66  	cmd.ui.Say(T("Removing env variable {{.VarName}} from app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...",
    67  		map[string]interface{}{
    68  			"VarName":     terminal.EntityNameColor(varName),
    69  			"AppName":     terminal.EntityNameColor(app.Name),
    70  			"OrgName":     terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
    71  			"SpaceName":   terminal.EntityNameColor(cmd.config.SpaceFields().Name),
    72  			"CurrentUser": terminal.EntityNameColor(cmd.config.Username())}))
    73  
    74  	envParams := app.EnvironmentVars
    75  
    76  	if _, ok := envParams[varName]; !ok {
    77  		cmd.ui.Ok()
    78  		cmd.ui.Warn(T("Env variable {{.VarName}} was not set.", map[string]interface{}{"VarName": varName}))
    79  		return nil
    80  	}
    81  
    82  	delete(envParams, varName)
    83  
    84  	_, err := cmd.appRepo.Update(app.GUID, models.AppParams{EnvironmentVars: &envParams})
    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")}))
    92  	return nil
    93  }