func runLogin(cmd *Command, args []string) { if len(args) != 0 { cmd.PrintUsage() os.Exit(2) } oldEmail := client.Username var email string if oldEmail == "" { fmt.Printf("Enter email: ") } else { fmt.Printf("Enter email [%s]: ", oldEmail) } _, err := fmt.Scanln(&email) switch { case err != nil && err.Error() != "unexpected newline": printFatal(err.Error()) case email == "" && oldEmail == "": printFatal("email is required.") case email == "": email = oldEmail } // NOTE: gopass doesn't support multi-byte chars on Windows password, err := readPassword("Enter password: "******"unexpected newline": printFatal("password is required.") default: printFatal(err.Error()) } address, token, err := attemptLogin(email, password, "") if err != nil { if herror, ok := err.(heroku.Error); ok && herror.Id == "two_factor" { // 2FA requested, attempt 2FA login var twoFactorCode string fmt.Printf("Enter two-factor auth code: ") if _, err := fmt.Scanln(&twoFactorCode); err != nil { printFatal("reading two-factor auth code: " + err.Error()) } address, token, err = attemptLogin(email, password, twoFactorCode) must(err) } else { must(err) } } nrc, err = hkclient.LoadNetRc() if err != nil { printFatal("loading netrc: " + err.Error()) } err = nrc.SaveCreds(address, email, token) if err != nil { printFatal("saving new token: " + err.Error()) } fmt.Println("Logged in.") }
func loadNetrc() { var err error if nrc == nil { if nrc, err = hkclient.LoadNetRc(); err != nil { if os.IsNotExist(err) { nrc = &hkclient.NetRc{} return } printFatal("loading netrc: " + err.Error()) } } }
func runCreds(cmd *Command, args []string) { var err error nrc, err = hkclient.LoadNetRc() if err != nil { printFatal(err.Error()) } u, err := url.Parse(apiURL) if err != nil { printFatal("could not parse API url: " + err.Error()) } user, pass, err := nrc.GetCreds(u) if err != nil { printFatal("could not get credentials: " + err.Error()) } fmt.Println(user, pass) }
func runLogout(cmd *Command, args []string) { if len(args) != 0 { cmd.PrintUsage() os.Exit(2) } u, err := url.Parse(client.URL) if err != nil { printFatal("couldn't parse client URL: " + err.Error()) } nrc, err = hkclient.LoadNetRc() if err != nil { printError(err.Error()) } err = removeCreds(strings.Split(u.Host, ":")[0]) if err != nil { printFatal("saving new netrc: " + err.Error()) } fmt.Println("Logged out.") }