github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/command/v2/auth_command.go (about)

     1  package v2
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/liamawhite/cli-with-i18n/actor/v2action"
     7  	"github.com/liamawhite/cli-with-i18n/command"
     8  	"github.com/liamawhite/cli-with-i18n/command/flag"
     9  	"github.com/liamawhite/cli-with-i18n/command/v2/shared"
    10  )
    11  
    12  //go:generate counterfeiter . AuthActor
    13  
    14  type AuthActor interface {
    15  	Authenticate(config v2action.Config, username string, password string) error
    16  }
    17  
    18  type AuthCommand struct {
    19  	RequiredArgs    flag.Authentication `positional-args:"yes"`
    20  	usage           interface{}         `usage:"CF_NAME auth USERNAME PASSWORD\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 auth name@example.com \"my password\" (use quotes for passwords with a space)\n   CF_NAME auth name@example.com \"\\\"password\\\"\" (escape quotes if used in password)"`
    21  	relatedCommands interface{}         `related_commands:"api, login, target"`
    22  
    23  	UI     command.UI
    24  	Config command.Config
    25  	Actor  AuthActor
    26  }
    27  
    28  func (cmd *AuthCommand) Setup(config command.Config, ui command.UI) error {
    29  	cmd.UI = ui
    30  	cmd.Config = config
    31  
    32  	ccClient, uaaClient, err := shared.NewClients(config, ui, true)
    33  	if err != nil {
    34  		return err
    35  	}
    36  	cmd.Actor = v2action.NewActor(ccClient, uaaClient, config)
    37  
    38  	return nil
    39  }
    40  
    41  func (cmd AuthCommand) Execute(args []string) error {
    42  	err := command.WarnAPIVersionCheck(cmd.Config, cmd.UI)
    43  	if err != nil {
    44  		return err
    45  	}
    46  
    47  	cmd.UI.DisplayTextWithFlavor(
    48  		"API endpoint: {{.Endpoint}}",
    49  		map[string]interface{}{
    50  			"Endpoint": cmd.Config.Target(),
    51  		})
    52  	cmd.UI.DisplayText("Authenticating...")
    53  
    54  	err = cmd.Actor.Authenticate(cmd.Config, cmd.RequiredArgs.Username, cmd.RequiredArgs.Password)
    55  	if err != nil {
    56  		return shared.HandleError(err)
    57  	}
    58  
    59  	cmd.UI.DisplayOK()
    60  	cmd.UI.DisplayTextWithFlavor(
    61  		"Use '{{.Command}}' to view or set your target org and space.",
    62  		map[string]interface{}{
    63  			"Command": fmt.Sprintf("%s target", cmd.Config.BinaryName()),
    64  		})
    65  
    66  	return nil
    67  }