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

     1  package serviceauthtoken
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/cf"
     5  	"code.cloudfoundry.org/cli/cf/api"
     6  	"code.cloudfoundry.org/cli/cf/commandregistry"
     7  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     8  	"code.cloudfoundry.org/cli/cf/flags"
     9  	. "code.cloudfoundry.org/cli/cf/i18n"
    10  	"code.cloudfoundry.org/cli/cf/requirements"
    11  	"code.cloudfoundry.org/cli/cf/terminal"
    12  )
    13  
    14  type ListServiceAuthTokens struct {
    15  	ui            terminal.UI
    16  	config        coreconfig.Reader
    17  	authTokenRepo api.ServiceAuthTokenRepository
    18  }
    19  
    20  func init() {
    21  	commandregistry.Register(&ListServiceAuthTokens{})
    22  }
    23  
    24  func (cmd *ListServiceAuthTokens) MetaData() commandregistry.CommandMetadata {
    25  	return commandregistry.CommandMetadata{
    26  		Name:        "service-auth-tokens",
    27  		Description: T("List service auth tokens"),
    28  		Usage: []string{
    29  			T("CF_NAME service-auth-tokens"),
    30  		},
    31  	}
    32  }
    33  
    34  func (cmd *ListServiceAuthTokens) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    35  	usageReq := requirements.NewUsageRequirement(commandregistry.CLICommandUsagePresenter(cmd),
    36  		T("No argument required"),
    37  		func() bool {
    38  			return len(fc.Args()) != 0
    39  		},
    40  	)
    41  
    42  	reqs := []requirements.Requirement{
    43  		usageReq,
    44  		requirementsFactory.NewLoginRequirement(),
    45  		requirementsFactory.NewMaxAPIVersionRequirement(
    46  			"service-auth-tokens",
    47  			cf.ServiceAuthTokenMaximumAPIVersion,
    48  		),
    49  	}
    50  
    51  	return reqs, nil
    52  }
    53  
    54  func (cmd *ListServiceAuthTokens) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    55  	cmd.ui = deps.UI
    56  	cmd.config = deps.Config
    57  	cmd.authTokenRepo = deps.RepoLocator.GetServiceAuthTokenRepository()
    58  	return cmd
    59  }
    60  
    61  func (cmd *ListServiceAuthTokens) Execute(c flags.FlagContext) error {
    62  	cmd.ui.Say(T("Getting service auth tokens as {{.CurrentUser}}...",
    63  		map[string]interface{}{
    64  			"CurrentUser": terminal.EntityNameColor(cmd.config.Username()),
    65  		}))
    66  	authTokens, err := cmd.authTokenRepo.FindAll()
    67  	if err != nil {
    68  		return err
    69  	}
    70  	cmd.ui.Ok()
    71  	cmd.ui.Say("")
    72  
    73  	table := cmd.ui.Table([]string{T("label"), T("provider")})
    74  
    75  	for _, authToken := range authTokens {
    76  		table.Add(authToken.Label, authToken.Provider)
    77  	}
    78  
    79  	err = table.Print()
    80  	if err != nil {
    81  		return err
    82  	}
    83  	return nil
    84  }