-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_cli.go
63 lines (58 loc) · 1.44 KB
/
auth_cli.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
52
53
54
55
56
57
58
59
60
61
62
63
package statushub
import (
"errors"
"fmt"
"io"
"os"
"github.com/howeyc/gopass"
"github.com/unixpickle/essentials"
)
const (
RootEnvVar = "STATUSHUB_ROOT"
PassEnvVar = "STATUSHUB_PASS"
)
// AuthCLI uses environment variables (and potentially
// user input) to obtain an authenticated Client.
func AuthCLI() (*Client, error) {
rootURL := os.Getenv(RootEnvVar)
if rootURL == "" {
return nil, errors.New("authenticate: missing " + RootEnvVar +
" environment variable")
}
client, err := NewClient(rootURL)
if err != nil {
return nil, essentials.AddCtx("authenticate", err)
}
pass := os.Getenv(PassEnvVar)
if pass == "" {
fmt.Print("StatusHub password: ")
passBytes, err := gopass.GetPasswd()
if err != nil {
return nil, err
}
pass = string(passBytes)
}
if err := client.Login(pass); err != nil {
return nil, essentials.AddCtx("authenticate", err)
}
return client, nil
}
// PrintEnvUsage prints usage messages about the
// authentication environment variables.
func PrintEnvUsage(w io.Writer) error {
messages := []string{
"Set the " + RootEnvVar + " environment variable",
"to the URL of the StatusHub server",
"(e.g. http://localhost:8080).",
"",
"Set the " + PassEnvVar + " environment variable",
"to the StatusHub password to avoid manual",
"entry.",
}
for _, msg := range messages {
if _, err := fmt.Fprintln(w, msg); err != nil {
return essentials.AddCtx("print environment usage", err)
}
}
return nil
}