/
login.go
72 lines (58 loc) · 1.47 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
package main
import (
"context"
"fmt"
"github.com/urfave/cli"
"github.com/manifoldco/manifold-cli/analytics"
"github.com/manifoldco/manifold-cli/config"
"github.com/manifoldco/manifold-cli/errs"
"github.com/manifoldco/manifold-cli/prompts"
"github.com/manifoldco/manifold-cli/session"
)
func init() {
loginCmd := cli.Command{
Name: "login",
Usage: "Log in to your account",
Category: "ADMINISTRATIVE",
Action: login,
}
cmds = append(cmds, loginCmd)
}
func login(_ *cli.Context) error {
ctx := context.Background()
cfg, err := config.Load()
if err != nil {
return cli.NewExitError("Could not load config: "+err.Error(), -1)
}
s, err := session.Retrieve(ctx, cfg)
if err != nil {
return cli.NewExitError("Could not retrieve session: "+err.Error(), -1)
}
if s.Authenticated() {
if s.FromEnvVars() {
fmt.Printf("You are logged in using your Manifold environment " +
"variables, hooray!\n")
return nil
}
return errs.ErrAlreadyLoggedIn
}
email, err := prompts.Email("")
if err != nil {
return err
}
password, err := prompts.Password()
if err != nil {
return err
}
s, err = session.Create(ctx, cfg, email, password)
if err != nil {
return cli.NewExitError("Are you sure the password and email match? "+err.Error(), -1)
}
a, err := analytics.New(cfg, s)
if err != nil {
return cli.NewExitError("A problem occurred: "+err.Error(), -1)
}
a.Track(ctx, "Logged In", nil)
fmt.Printf("You are logged in, hooray!\n")
return nil
}