github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+incompatible/command/v6/login_command.go (about)

     1  package v6
     2  
     3  import (
     4  	"net/url"
     5  
     6  	"code.cloudfoundry.org/cli/actor/v2action"
     7  	"code.cloudfoundry.org/cli/command"
     8  	"code.cloudfoundry.org/cli/command/translatableerror"
     9  	"code.cloudfoundry.org/cli/command/v6/shared"
    10  )
    11  
    12  //go:generate counterfeiter . LoginActor
    13  
    14  type LoginActor interface {
    15  	SetTarget(settings v2action.TargetSettings) (v2action.Warnings, error)
    16  }
    17  
    18  type LoginCommand struct {
    19  	APIEndpoint       string      `short:"a" description:"API endpoint (e.g. https://api.example.com)"`
    20  	Organization      string      `short:"o" description:"Org"`
    21  	Password          string      `short:"p" description:"Password"`
    22  	Space             string      `short:"s" description:"Space"`
    23  	SkipSSLValidation bool        `long:"skip-ssl-validation" description:"Skip verification of the API endpoint. Not recommended!"`
    24  	SSO               bool        `long:"sso" description:"Prompt for a one-time passcode to login"`
    25  	SSOPasscode       string      `long:"sso-passcode" description:"One-time passcode"`
    26  	Username          string      `short:"u" description:"Username"`
    27  	usage             interface{} `usage:"CF_NAME login [-a API_URL] [-u USERNAME] [-p PASSWORD] [-o ORG] [-s SPACE] [--sso | --sso-passcode PASSCODE]\n\nWARNING:\n   Providing your password as a command line option is highly discouraged\n   Your password may be visible to others and may be recorded in your shell history\n\nEXAMPLES:\n   CF_NAME login (omit username and password to login interactively -- CF_NAME will prompt for both)\n   CF_NAME login -u name@example.com -p pa55woRD (specify username and password as arguments)\n   CF_NAME login -u name@example.com -p \"my password\" (use quotes for passwords with a space)\n   CF_NAME login -u name@example.com -p \"\\\"password\\\"\" (escape quotes if used in password)\n   CF_NAME login --sso (CF_NAME will provide a url to obtain a one-time passcode to login)"`
    28  	relatedCommands   interface{} `related_commands:"api, auth, target"`
    29  
    30  	UI     command.UI
    31  	Actor  LoginActor
    32  	Config command.Config
    33  }
    34  
    35  func (cmd *LoginCommand) Setup(config command.Config, ui command.UI) error {
    36  	ccClient, uaaClient, err := shared.NewClients(config, ui, false)
    37  	if err != nil {
    38  		return err
    39  	}
    40  	cmd.Actor = v2action.NewActor(ccClient, uaaClient, config)
    41  	cmd.UI = ui
    42  	cmd.Config = config
    43  	return nil
    44  }
    45  
    46  func (cmd *LoginCommand) Execute(args []string) error {
    47  	if cmd.Config.Experimental() {
    48  		if cmd.APIEndpoint == "" {
    49  			apiEndpoint, err := cmd.UI.DisplayTextPrompt("API endpoint")
    50  			if err != nil {
    51  				return err
    52  			}
    53  			cmd.APIEndpoint = apiEndpoint
    54  		} else {
    55  			cmd.UI.DisplayText("API endpoint: {{.APIEndpoint}}", map[string]interface{}{
    56  				"APIEndpoint": cmd.APIEndpoint,
    57  			})
    58  		}
    59  		endpoint, _ := url.Parse(cmd.APIEndpoint)
    60  		if endpoint.Scheme == "" {
    61  			endpoint.Scheme = "https"
    62  		}
    63  		settings := v2action.TargetSettings{
    64  			URL:               endpoint.String(),
    65  			SkipSSLValidation: true,
    66  		}
    67  		_, err := cmd.Actor.SetTarget(settings)
    68  		if err != nil {
    69  			return err
    70  		}
    71  
    72  		cmd.UI.DisplayText("API endpoint: {{.APIEndpoint}} (API Version: {{.APIVersion}})", map[string]interface{}{
    73  			"APIEndpoint": cmd.APIEndpoint,
    74  			"APIVersion":  cmd.Config.APIVersion(),
    75  		})
    76  		return nil
    77  	}
    78  	return translatableerror.UnrefactoredCommandError{}
    79  }