From 4685b66455e130461c8fddc0e6c8717b2472323b Mon Sep 17 00:00:00 2001 From: Metalnem Date: Mon, 26 Sep 2016 17:28:41 +0200 Subject: [PATCH] Read credentials from stdio if they are not available from other sources --- main.go | 46 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index b4b008b..0a98b03 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "archive/zip" + "bufio" "bytes" "context" "crypto/sha1" @@ -19,7 +20,10 @@ import ( "os" "strconv" "strings" + "syscall" "time" + + "golang.org/x/crypto/ssh/terminal" ) const ( @@ -152,21 +156,40 @@ func setHeaders(header http.Header) { header.Set(headerDate, t.Format(timeFormat)) } -func getCredentials() (string, string) { +func getCredentials() (string, string, error) { email := *email password := *password - if email == "" && password == "" { - email = os.Getenv("RUNTASTIC_EMAIL") - password = os.Getenv("RUNTASTIC_PASSWORD") + if email != "" && password != "" { + return email, password, nil + } + + email = os.Getenv("RUNTASTIC_EMAIL") + password = os.Getenv("RUNTASTIC_PASSWORD") + + if email != "" && password != "" { + return email, password, nil + } + + fmt.Print("Email: ") + email, err := bufio.NewReader(os.Stdin).ReadString('\n') + + if err != nil { + return "", "", err } - if email == "" || password == "" { - flag.Usage() - os.Exit(1) + fmt.Print("Password: ") + pass, err := terminal.ReadPassword(syscall.Stdin) + fmt.Println() + + if err != nil { + return "", "", err } - return email, password + email = email[0 : len(email)-1] + password = string(pass) + + return email, password, nil } func loginApp(ctx context.Context, email, password string) (*appUser, error) { @@ -510,7 +533,12 @@ func archive(filename string, sessions []sessionData) (err error) { func main() { flag.Parse() - email, password := getCredentials() + email, password, err := getCredentials() + + if err != nil { + log.Fatal(err) + } + format, err := getFormat(*format) if err != nil {