github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/serviceauthtoken/service_auth_tokens.go (about)

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