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

     1  package commands
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/cf/api/authentication"
     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  	"code.cloudfoundry.org/cli/plugin/models"
    12  )
    13  
    14  type OAuthToken struct {
    15  	ui          terminal.UI
    16  	config      coreconfig.ReadWriter
    17  	authRepo    authentication.Repository
    18  	pluginModel *plugin_models.GetOauthToken_Model
    19  	pluginCall  bool
    20  }
    21  
    22  func init() {
    23  	commandregistry.Register(&OAuthToken{})
    24  }
    25  
    26  func (cmd *OAuthToken) MetaData() commandregistry.CommandMetadata {
    27  	return commandregistry.CommandMetadata{
    28  		Name:        "oauth-token",
    29  		Description: T("Retrieve and display the OAuth token for the current session"),
    30  		Usage: []string{
    31  			T("CF_NAME oauth-token"),
    32  		},
    33  	}
    34  }
    35  
    36  func (cmd *OAuthToken) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    37  	reqs := []requirements.Requirement{
    38  		requirementsFactory.NewLoginRequirement(),
    39  	}
    40  
    41  	return reqs, nil
    42  }
    43  
    44  func (cmd *OAuthToken) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    45  	cmd.ui = deps.UI
    46  	cmd.config = deps.Config
    47  	cmd.authRepo = deps.RepoLocator.GetAuthenticationRepository()
    48  	cmd.pluginCall = pluginCall
    49  	cmd.pluginModel = deps.PluginModels.OauthToken
    50  	return cmd
    51  }
    52  
    53  func (cmd *OAuthToken) Execute(c flags.FlagContext) error {
    54  	token, err := cmd.authRepo.RefreshAuthToken()
    55  	if err != nil {
    56  		return err
    57  	}
    58  
    59  	if cmd.pluginCall {
    60  		cmd.pluginModel.Token = token
    61  	} else {
    62  		cmd.ui.Say(token)
    63  	}
    64  	return nil
    65  }