/
login.go
74 lines (60 loc) · 1.56 KB
/
login.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
64
65
66
67
68
69
70
71
72
73
74
package processors
import (
"fmt"
"os"
"github.com/nanobox-io/nanobox/models"
"github.com/nanobox-io/nanobox/util"
"github.com/nanobox-io/nanobox/util/display"
"github.com/nanobox-io/nanobox/util/odin"
)
// Process ...
func Login(username, password, endpoint string) error {
// request Username/Password if missing
if username == "" && os.Getenv("NANOBOX_USERNAME") != "" {
username = os.Getenv("NANOBOX_USERNAME")
}
if username == "" {
user, err := display.ReadUsername()
if err != nil {
return util.ErrorAppend(err, "unable to retrieve username")
}
username = user
}
if password == "" && os.Getenv("NANOBOX_PASSWORD") != "" {
password = os.Getenv("NANOBOX_PASSWORD")
}
if password == "" {
// ReadPassword prints Password: already
pass, err := display.ReadPassword("Nanobox")
if err != nil {
return util.ErrorAppend(err, "failed to read password")
}
password = pass
}
if endpoint == "" && os.Getenv("NANOBOX_ENDPOINT") != "" {
endpoint = os.Getenv("NANOBOX_ENDPOINT")
}
if endpoint == "" {
endpoint = "nanobox"
}
// set the odin endpoint
odin.SetEndpoint(endpoint)
// verify that the user exists
token, err := odin.Auth(username, password)
if err != nil {
fmt.Println(`! The username/password was incorrect, but we're continuing on.
To reattempt authentication, run 'nanobox login'.
`)
return nil
}
// store the user token
auth := models.Auth{
Endpoint: endpoint,
Key: token,
}
if auth.Save() != nil {
return util.Errorf("unable to save user authentication")
}
display.LoginComplete()
return nil
}