github.com/jasonkeene/cli@v6.14.1-0.20160816203908-ca5715166dfb+incompatible/cf/commands/oauth_token.go (about)

     1  package commands
     2  
     3  import (
     4  	"github.com/cloudfoundry/cli/cf/api/authentication"
     5  	"github.com/cloudfoundry/cli/cf/commandregistry"
     6  	"github.com/cloudfoundry/cli/cf/configuration/coreconfig"
     7  	"github.com/cloudfoundry/cli/cf/flags"
     8  	. "github.com/cloudfoundry/cli/cf/i18n"
     9  	"github.com/cloudfoundry/cli/cf/requirements"
    10  	"github.com/cloudfoundry/cli/cf/terminal"
    11  	"github.com/cloudfoundry/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  }