diff --git a/auth/auth.go b/auth/auth.go index f52d94f..b2e10ec 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -26,7 +26,8 @@ const ( // An entry will be created in ~/.netrc on successful login. func Login() error { // Create a function to be overriden in tests - email, password, err := getCredentials() + email := getEmail() + password, err := getPassword("Enter password (will be hidden): ") if err != nil { return err } @@ -47,9 +48,9 @@ func Login() error { // Login with the user email and API token // An entry will be created in ~/.netrc on successful login. -func LoginWithAPIToken() error { +func LoginWithAPIToken(token string) (err error) { // Create a function to be overriden in tests - email, token, err := getTokenCredentials() + email := getEmail() if err != nil { return err } @@ -81,26 +82,20 @@ func Logout() error { } // Lambda to be overriden in tests -var getCredentials = func() (email, password string, err error) { +var getEmail = func() (email string) { fmt.Printf("Enter your email: ") fmt.Scanf("%s", &email) - // NOTE: gopass doesn't support multi-byte chars on Windows - password, err = readPassword("Enter password (will be hidden): ") - if err != nil { - return "", "", err - } - return email, password, nil + return email } -var getTokenCredentials = func() (email, token string, err error) { - fmt.Printf("Enter your email: ") - fmt.Scanf("%s", &email) +// Lambda to be overriden in tests +var getPassword = func(prompt string) (password string, err error) { // NOTE: gopass doesn't support multi-byte chars on Windows - token, err = readPassword("Enter your token (API Key, it will be hidden) : ") + password, err = readPassword(prompt) if err != nil { - return "", "", err + return "", err } - return email, token, nil + return password, nil } func readPassword(prompt string) (password string, err error) { diff --git a/auth/auth_test.go b/auth/auth_test.go index eeb67c7..32692f1 100644 --- a/auth/auth_test.go +++ b/auth/auth_test.go @@ -70,8 +70,11 @@ func TestLogin(t *testing.T) { api.APIImpl = api.NewAPIv1(ts.URL, "") // don't try to use stdin - getCredentials = func() (email, password string, err error) { - return "batman@example.com", "secret123", nil + getEmail = func() (email string) { + return "batman@example.com" + } + getPassword = func(prompt string) (password string, err error) { + return "secret123", nil } netrcFile := bytes.NewBufferString("") diff --git a/commands/app.go b/commands/app.go index 88abad4..090c33a 100644 --- a/commands/app.go +++ b/commands/app.go @@ -63,13 +63,14 @@ func App() *cli.App { { Name: "login", Usage: "Login", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "with-api-token", + Usage: "Log in with your API token (API key in the user profile)", + }, + }, Action: Login, }, - { - Name: "with-api-token", - Usage: "Login with your API token (API Key in your profile page)", - Action: LoginWithAPIToken, - }, { Name: "logout", Usage: "Logout", diff --git a/commands/auth.go b/commands/auth.go index a12a691..3b1228d 100644 --- a/commands/auth.go +++ b/commands/auth.go @@ -9,18 +9,20 @@ var login = func() error { return auth.Login() } -var login_with_api_token = func() error { - return auth.LoginWithAPIToken() +var login_with_api_token = func(api_token string) error { + return auth.LoginWithAPIToken(api_token) } // auth.Login wrapper with a cli.Content -func Login(ctx *cli.Context) error { - err := login() - return err -} - -func LoginWithAPIToken(ctx *cli.Context) error { - err := login_with_api_token() +func Login(ctx *cli.Context) (err error) { + if ctx.IsSet("with-api-token") { + // log in with the provided token + api_token := ctx.String("with-api-token") + err = login_with_api_token(api_token) + } else { + // log in with the user and password + err = login() + } return err }