github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/cf/commands/featureflag/feature_flag.go (about)

     1  package featureflag
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/cf/api/featureflags"
     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/requirements"
    12  	"code.cloudfoundry.org/cli/cf/terminal"
    13  )
    14  
    15  type ShowFeatureFlag struct {
    16  	ui       terminal.UI
    17  	config   coreconfig.ReadWriter
    18  	flagRepo featureflags.FeatureFlagRepository
    19  }
    20  
    21  func init() {
    22  	commandregistry.Register(&ShowFeatureFlag{})
    23  }
    24  
    25  func (cmd *ShowFeatureFlag) MetaData() commandregistry.CommandMetadata {
    26  	return commandregistry.CommandMetadata{
    27  		Name:        "feature-flag",
    28  		Description: T("Retrieve an individual feature flag with status"),
    29  		Usage: []string{
    30  			T("CF_NAME feature-flag FEATURE_NAME"),
    31  		},
    32  	}
    33  }
    34  
    35  func (cmd *ShowFeatureFlag) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    36  	if len(fc.Args()) != 1 {
    37  		cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("feature-flag"))
    38  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1)
    39  	}
    40  
    41  	reqs := []requirements.Requirement{
    42  		requirementsFactory.NewLoginRequirement(),
    43  	}
    44  
    45  	return reqs, nil
    46  }
    47  
    48  func (cmd *ShowFeatureFlag) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    49  	cmd.ui = deps.UI
    50  	cmd.config = deps.Config
    51  	cmd.flagRepo = deps.RepoLocator.GetFeatureFlagRepository()
    52  	return cmd
    53  }
    54  
    55  func (cmd *ShowFeatureFlag) Execute(c flags.FlagContext) error {
    56  	flagName := c.Args()[0]
    57  
    58  	cmd.ui.Say(T("Retrieving status of {{.FeatureFlag}} as {{.Username}}...", map[string]interface{}{
    59  		"FeatureFlag": terminal.EntityNameColor(flagName),
    60  		"Username":    terminal.EntityNameColor(cmd.config.Username())}))
    61  
    62  	flag, err := cmd.flagRepo.FindByName(flagName)
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	cmd.ui.Ok()
    68  	cmd.ui.Say("")
    69  
    70  	table := cmd.ui.Table([]string{T("Features"), T("State")})
    71  	table.Add(flag.Name, cmd.flagBoolToString(flag.Enabled))
    72  
    73  	err = table.Print()
    74  	if err != nil {
    75  		return err
    76  	}
    77  	return nil
    78  }
    79  
    80  func (cmd ShowFeatureFlag) flagBoolToString(enabled bool) string {
    81  	if enabled {
    82  		return "enabled"
    83  	}
    84  	return "disabled"
    85  }