/
logout.go
56 lines (44 loc) · 1.13 KB
/
logout.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
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/session"
)
func init() {
logoutCmd := cli.Command{
Name: "logout",
Usage: "Log out of your account",
Category: "ADMINISTRATIVE",
Action: logout,
}
cmds = append(cmds, logoutCmd)
}
func logout(_ *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() {
return errs.ErrAlreadyLoggedOut
}
a, err := analytics.New(cfg, s)
if err != nil {
return cli.NewExitError("Something went horribly wrong: "+err.Error(), -1)
}
a.Track(ctx, "Logout", nil)
err = session.Destroy(ctx, cfg)
if err != nil {
return cli.NewExitError("Failed to logout: "+err.Error(), -1)
}
fmt.Printf("You are now logged out!\n")
return nil
}