github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/environmentvariablegroup/set_running_environment_variable_group.go (about)

     1  package environmentvariablegroup
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"code.cloudfoundry.org/cli/cf/api/environmentvariablegroups"
     8  	"code.cloudfoundry.org/cli/cf/commandregistry"
     9  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    10  	cf_errors "code.cloudfoundry.org/cli/cf/errors"
    11  	"code.cloudfoundry.org/cli/cf/flags"
    12  	. "code.cloudfoundry.org/cli/cf/i18n"
    13  	"code.cloudfoundry.org/cli/cf/requirements"
    14  	"code.cloudfoundry.org/cli/cf/terminal"
    15  )
    16  
    17  type SetRunningEnvironmentVariableGroup struct {
    18  	ui                           terminal.UI
    19  	config                       coreconfig.ReadWriter
    20  	environmentVariableGroupRepo environmentvariablegroups.Repository
    21  }
    22  
    23  func init() {
    24  	commandregistry.Register(&SetRunningEnvironmentVariableGroup{})
    25  }
    26  
    27  func (cmd *SetRunningEnvironmentVariableGroup) MetaData() commandregistry.CommandMetadata {
    28  	return commandregistry.CommandMetadata{
    29  		Name:        "set-running-environment-variable-group",
    30  		Description: T("Pass parameters as JSON to create a running environment variable group"),
    31  		ShortName:   "srevg",
    32  		Usage: []string{
    33  			T(`CF_NAME set-running-environment-variable-group '{"name":"value","name":"value"}'`),
    34  		},
    35  	}
    36  }
    37  
    38  func (cmd *SetRunningEnvironmentVariableGroup) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    39  	if len(fc.Args()) != 1 {
    40  		cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("set-running-environment-variable-group"))
    41  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1)
    42  	}
    43  
    44  	reqs := []requirements.Requirement{
    45  		requirementsFactory.NewLoginRequirement(),
    46  	}
    47  	return reqs, nil
    48  }
    49  
    50  func (cmd *SetRunningEnvironmentVariableGroup) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    51  	cmd.ui = deps.UI
    52  	cmd.config = deps.Config
    53  	cmd.environmentVariableGroupRepo = deps.RepoLocator.GetEnvironmentVariableGroupsRepository()
    54  	return cmd
    55  }
    56  
    57  func (cmd *SetRunningEnvironmentVariableGroup) Execute(c flags.FlagContext) error {
    58  	cmd.ui.Say(T("Setting the contents of the running environment variable group as {{.Username}}...", map[string]interface{}{
    59  		"Username": terminal.EntityNameColor(cmd.config.Username())}))
    60  
    61  	err := cmd.environmentVariableGroupRepo.SetRunning(c.Args()[0])
    62  	if err != nil {
    63  		suggestionText := ""
    64  
    65  		httpError, ok := err.(cf_errors.HTTPError)
    66  		if ok && httpError.ErrorCode() == cf_errors.MessageParseError {
    67  			suggestionText = T(`
    68  
    69  Your JSON string syntax is invalid.  Proper syntax is this:  cf set-running-environment-variable-group '{"name":"value","name":"value"}'`)
    70  		}
    71  		return errors.New(err.Error() + suggestionText)
    72  	}
    73  
    74  	cmd.ui.Ok()
    75  	return nil
    76  }