-
Notifications
You must be signed in to change notification settings - Fork 9
/
utils.go
37 lines (32 loc) · 858 Bytes
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package main
import (
"bufio"
"fmt"
"log"
"os"
"regexp"
"strings"
"syscall"
"github.com/urfave/cli/v2"
"golang.org/x/crypto/ssh/terminal"
)
var emailRegex = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
func readCredentials(c *cli.Context) (string, string) {
email := c.String("email")
if email == "" {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Email: ")
email, _ = reader.ReadString('\n')
email = strings.TrimSuffix(email, "\n")
}
if !emailRegex.MatchString(email) {
log.Fatalln("Email not valid")
}
fmt.Print("Password: ")
bytePassword, _ := terminal.ReadPassword(int(syscall.Stdin))
password := string(bytePassword)
if password == "" {
log.Fatalln("\nNo password provided")
}
return email, password
}