code.cloudfoundry.org/cli@v7.1.0+incompatible/cf/commands/featureflag/feature_flags.go (about)

     1  package featureflag
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/cf/api/featureflags"
     5  	"code.cloudfoundry.org/cli/cf/commandregistry"
     6  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     7  	"code.cloudfoundry.org/cli/cf/flags"
     8  	. "code.cloudfoundry.org/cli/cf/i18n"
     9  	"code.cloudfoundry.org/cli/cf/requirements"
    10  	"code.cloudfoundry.org/cli/cf/terminal"
    11  )
    12  
    13  type ListFeatureFlags struct {
    14  	ui       terminal.UI
    15  	config   coreconfig.ReadWriter
    16  	flagRepo featureflags.FeatureFlagRepository
    17  }
    18  
    19  func init() {
    20  	commandregistry.Register(&ListFeatureFlags{})
    21  }
    22  
    23  func (cmd *ListFeatureFlags) MetaData() commandregistry.CommandMetadata {
    24  	return commandregistry.CommandMetadata{
    25  		Name:        "feature-flags",
    26  		Description: T("Retrieve list of feature flags with status of each flag-able feature"),
    27  		Usage: []string{
    28  			T("CF_NAME feature-flags"),
    29  		},
    30  	}
    31  }
    32  
    33  func (cmd *ListFeatureFlags) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    34  	usageReq := requirements.NewUsageRequirement(commandregistry.CLICommandUsagePresenter(cmd),
    35  		T("No argument required"),
    36  		func() bool {
    37  			return len(fc.Args()) != 0
    38  		},
    39  	)
    40  
    41  	reqs := []requirements.Requirement{
    42  		usageReq,
    43  		requirementsFactory.NewLoginRequirement(),
    44  	}
    45  
    46  	return reqs, nil
    47  }
    48  
    49  func (cmd *ListFeatureFlags) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    50  	cmd.ui = deps.UI
    51  	cmd.config = deps.Config
    52  	cmd.flagRepo = deps.RepoLocator.GetFeatureFlagRepository()
    53  	return cmd
    54  }
    55  
    56  func (cmd *ListFeatureFlags) Execute(c flags.FlagContext) error {
    57  	cmd.ui.Say(T("Retrieving status of all flagged features as {{.Username}}...", map[string]interface{}{
    58  		"Username": terminal.EntityNameColor(cmd.config.Username())}))
    59  
    60  	flags, err := cmd.flagRepo.List()
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	cmd.ui.Ok()
    66  	cmd.ui.Say("")
    67  
    68  	table := cmd.ui.Table([]string{T("Features"), T("State")})
    69  
    70  	for _, flag := range flags {
    71  		table.Add(
    72  			flag.Name,
    73  			cmd.flagBoolToString(flag.Enabled),
    74  		)
    75  	}
    76  
    77  	err = table.Print()
    78  	if err != nil {
    79  		return err
    80  	}
    81  	return nil
    82  }
    83  
    84  func (cmd ListFeatureFlags) flagBoolToString(enabled bool) string {
    85  	if enabled {
    86  		return "enabled"
    87  	}
    88  	return "disabled"
    89  }