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

     1  package commands
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/cf/flags"
     7  	. "code.cloudfoundry.org/cli/cf/i18n"
     8  
     9  	"code.cloudfoundry.org/cli/cf/api/authentication"
    10  	"code.cloudfoundry.org/cli/cf/commandregistry"
    11  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    12  	"code.cloudfoundry.org/cli/cf/requirements"
    13  	"code.cloudfoundry.org/cli/cf/terminal"
    14  )
    15  
    16  //go:generate counterfeiter . SSHCodeGetter
    17  
    18  type SSHCodeGetter interface {
    19  	commandregistry.Command
    20  	Get() (string, error)
    21  }
    22  
    23  type OneTimeSSHCode struct {
    24  	ui           terminal.UI
    25  	config       coreconfig.ReadWriter
    26  	authRepo     authentication.Repository
    27  	endpointRepo coreconfig.EndpointRepository
    28  }
    29  
    30  func init() {
    31  	commandregistry.Register(&OneTimeSSHCode{})
    32  }
    33  
    34  func (cmd *OneTimeSSHCode) MetaData() commandregistry.CommandMetadata {
    35  	return commandregistry.CommandMetadata{
    36  		Name:        "ssh-code",
    37  		Description: T("Get a one time password for ssh clients"),
    38  		Usage: []string{
    39  			T("CF_NAME ssh-code"),
    40  		},
    41  	}
    42  }
    43  
    44  func (cmd *OneTimeSSHCode) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    45  	usageReq := requirements.NewUsageRequirement(commandregistry.CLICommandUsagePresenter(cmd),
    46  		T("No argument required"),
    47  		func() bool {
    48  			return len(fc.Args()) != 0
    49  		},
    50  	)
    51  
    52  	reqs := []requirements.Requirement{
    53  		usageReq,
    54  		requirementsFactory.NewAPIEndpointRequirement(),
    55  	}
    56  
    57  	return reqs, nil
    58  }
    59  
    60  func (cmd *OneTimeSSHCode) SetDependency(deps commandregistry.Dependency, _ bool) commandregistry.Command {
    61  	cmd.ui = deps.UI
    62  	cmd.config = deps.Config
    63  	cmd.authRepo = deps.RepoLocator.GetAuthenticationRepository()
    64  	cmd.endpointRepo = deps.RepoLocator.GetEndpointRepository()
    65  
    66  	return cmd
    67  }
    68  
    69  func (cmd *OneTimeSSHCode) Execute(c flags.FlagContext) error {
    70  	code, err := cmd.Get()
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	cmd.ui.Say(code)
    76  	return nil
    77  }
    78  
    79  func (cmd *OneTimeSSHCode) Get() (string, error) {
    80  	refresher := coreconfig.APIConfigRefresher{
    81  		Endpoint:     cmd.config.APIEndpoint(),
    82  		EndpointRepo: cmd.endpointRepo,
    83  		Config:       cmd.config,
    84  	}
    85  
    86  	_, err := refresher.Refresh()
    87  	if err != nil {
    88  		return "", errors.New("Error refreshing config: " + err.Error())
    89  	}
    90  
    91  	token, err := cmd.authRepo.RefreshAuthToken()
    92  	if err != nil {
    93  		return "", errors.New(T("Error refreshing oauth token: ") + err.Error())
    94  	}
    95  
    96  	sshCode, err := cmd.authRepo.Authorize(token)
    97  	if err != nil {
    98  		return "", errors.New(T("Error getting SSH code: ") + err.Error())
    99  	}
   100  
   101  	return sshCode, nil
   102  }