github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/servicekey/service_keys.go (about)

     1  package servicekey
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/cf/api"
     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/requirements"
    11  	"code.cloudfoundry.org/cli/cf/terminal"
    12  
    13  	. "code.cloudfoundry.org/cli/cf/i18n"
    14  )
    15  
    16  type ServiceKeys struct {
    17  	ui                         terminal.UI
    18  	config                     coreconfig.Reader
    19  	serviceRepo                api.ServiceRepository
    20  	serviceKeyRepo             api.ServiceKeyRepository
    21  	serviceInstanceRequirement requirements.ServiceInstanceRequirement
    22  }
    23  
    24  func init() {
    25  	commandregistry.Register(&ServiceKeys{})
    26  }
    27  
    28  func (cmd *ServiceKeys) MetaData() commandregistry.CommandMetadata {
    29  	return commandregistry.CommandMetadata{
    30  		Name:        "service-keys",
    31  		ShortName:   "sk",
    32  		Description: T("List keys for a service instance"),
    33  		Usage: []string{
    34  			T("CF_NAME service-keys SERVICE_INSTANCE"),
    35  		},
    36  		Examples: []string{
    37  			"CF_NAME service-keys mydb",
    38  		},
    39  	}
    40  }
    41  
    42  func (cmd *ServiceKeys) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    43  	if len(fc.Args()) != 1 {
    44  		cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("service-keys"))
    45  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1)
    46  	}
    47  
    48  	loginRequirement := requirementsFactory.NewLoginRequirement()
    49  	cmd.serviceInstanceRequirement = requirementsFactory.NewServiceInstanceRequirement(fc.Args()[0])
    50  	targetSpaceRequirement := requirementsFactory.NewTargetedSpaceRequirement()
    51  
    52  	reqs := []requirements.Requirement{loginRequirement, cmd.serviceInstanceRequirement, targetSpaceRequirement}
    53  
    54  	return reqs, nil
    55  }
    56  
    57  func (cmd *ServiceKeys) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    58  	cmd.ui = deps.UI
    59  	cmd.config = deps.Config
    60  	cmd.serviceRepo = deps.RepoLocator.GetServiceRepository()
    61  	cmd.serviceKeyRepo = deps.RepoLocator.GetServiceKeyRepository()
    62  	return cmd
    63  }
    64  
    65  func (cmd *ServiceKeys) Execute(c flags.FlagContext) error {
    66  	serviceInstance := cmd.serviceInstanceRequirement.GetServiceInstance()
    67  
    68  	cmd.ui.Say(T("Getting keys for service instance {{.ServiceInstanceName}} as {{.CurrentUser}}...",
    69  		map[string]interface{}{
    70  			"ServiceInstanceName": terminal.EntityNameColor(serviceInstance.Name),
    71  			"CurrentUser":         terminal.EntityNameColor(cmd.config.Username()),
    72  		}))
    73  
    74  	serviceKeys, err := cmd.serviceKeyRepo.ListServiceKeys(serviceInstance.GUID)
    75  	if err != nil {
    76  		return err
    77  	}
    78  
    79  	table := cmd.ui.Table([]string{T("name")})
    80  
    81  	for _, serviceKey := range serviceKeys {
    82  		table.Add(serviceKey.Fields.Name)
    83  	}
    84  
    85  	if len(serviceKeys) == 0 {
    86  		cmd.ui.Say(T("No service key for service instance {{.ServiceInstanceName}}",
    87  			map[string]interface{}{"ServiceInstanceName": terminal.EntityNameColor(serviceInstance.Name)}))
    88  		return nil
    89  	}
    90  
    91  	cmd.ui.Say("")
    92  	err = table.Print()
    93  	if err != nil {
    94  		return err
    95  	}
    96  	return nil
    97  }