-
Notifications
You must be signed in to change notification settings - Fork 0
/
credentials.go
51 lines (39 loc) · 1.07 KB
/
credentials.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"bufio"
"fmt"
"log"
"os"
"os/exec"
"strings"
"github.com/howeyc/gopass"
"github.com/urfave/cli/v2"
)
func credentials(ctx *cli.Context) (string, string, error) {
reader := bufio.NewReader(os.Stdin)
username := ctx.String("username")
if username == "" {
fmt.Fprint(os.Stderr, "Username: ")
usernameInput, err := reader.ReadString('\n')
if err != nil {
return "", "", fmt.Errorf("reading username: %w", err)
}
username = strings.TrimSpace(usernameInput)
}
var password string
if opPath, err := exec.LookPath("op"); err == nil && ctx.String("1pass") != "" {
fmt.Fprintf(os.Stderr, "Henter Medlemsservice-adgangskode for %s fra 1Password...\n", username)
bytePassword, err := exec.Command(opPath, "read", ctx.String("1pass"), "--no-newline").Output()
if err != nil {
log.Fatal(err)
}
password = string(bytePassword)
} else {
bytePassword, err := gopass.GetPasswdPrompt("Password: ", true, os.Stdin, os.Stderr)
if err != nil {
log.Fatal(err)
}
password = string(bytePassword)
}
return username, password, nil
}