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

     1  package environmentvariablegroup
     2  
     3  import (
     4  	"sort"
     5  
     6  	"code.cloudfoundry.org/cli/cf/api/environmentvariablegroups"
     7  	"code.cloudfoundry.org/cli/cf/commandregistry"
     8  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     9  	"code.cloudfoundry.org/cli/cf/flags"
    10  	. "code.cloudfoundry.org/cli/cf/i18n"
    11  	"code.cloudfoundry.org/cli/cf/models"
    12  	"code.cloudfoundry.org/cli/cf/requirements"
    13  	"code.cloudfoundry.org/cli/cf/terminal"
    14  )
    15  
    16  type RunningEnvironmentVariableGroup struct {
    17  	ui                           terminal.UI
    18  	config                       coreconfig.ReadWriter
    19  	environmentVariableGroupRepo environmentvariablegroups.Repository
    20  }
    21  
    22  func init() {
    23  	commandregistry.Register(&RunningEnvironmentVariableGroup{})
    24  }
    25  
    26  func (cmd *RunningEnvironmentVariableGroup) MetaData() commandregistry.CommandMetadata {
    27  	return commandregistry.CommandMetadata{
    28  		Name:        "running-environment-variable-group",
    29  		Description: T("Retrieve the contents of the running environment variable group"),
    30  		ShortName:   "revg",
    31  		Usage: []string{
    32  			T("CF_NAME running-environment-variable-group"),
    33  		},
    34  	}
    35  }
    36  
    37  func (cmd *RunningEnvironmentVariableGroup) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    38  	usageReq := requirements.NewUsageRequirement(commandregistry.CLICommandUsagePresenter(cmd),
    39  		T("No argument required"),
    40  		func() bool {
    41  			return len(fc.Args()) != 0
    42  		},
    43  	)
    44  
    45  	reqs := []requirements.Requirement{
    46  		usageReq,
    47  		requirementsFactory.NewLoginRequirement(),
    48  	}
    49  	return reqs, nil
    50  }
    51  
    52  func (cmd *RunningEnvironmentVariableGroup) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    53  	cmd.ui = deps.UI
    54  	cmd.config = deps.Config
    55  	cmd.environmentVariableGroupRepo = deps.RepoLocator.GetEnvironmentVariableGroupsRepository()
    56  	return cmd
    57  }
    58  
    59  func (cmd *RunningEnvironmentVariableGroup) Execute(c flags.FlagContext) error {
    60  	cmd.ui.Say(T("Retrieving the contents of the running environment variable group as {{.Username}}...", map[string]interface{}{
    61  		"Username": terminal.EntityNameColor(cmd.config.Username())}))
    62  
    63  	runningEnvVars, err := cmd.environmentVariableGroupRepo.ListRunning()
    64  	if err != nil {
    65  		return err
    66  	}
    67  
    68  	cmd.ui.Ok()
    69  
    70  	table := cmd.ui.Table([]string{T("Variable Name"), T("Assigned Value")})
    71  	sortedEnvVars := models.EnvironmentVariableList(runningEnvVars)
    72  	sort.Sort(sortedEnvVars)
    73  	for _, envVar := range sortedEnvVars {
    74  		table.Add(envVar.Name, envVar.Value)
    75  	}
    76  	err = table.Print()
    77  	if err != nil {
    78  		return err
    79  	}
    80  	return nil
    81  }