forked from manifoldco/torus-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logout.go
46 lines (38 loc) · 948 Bytes
/
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
package cmd
import (
"context"
"fmt"
"github.com/urfave/cli"
"github.com/manifoldco/torus-cli/api"
"github.com/manifoldco/torus-cli/apitypes"
"github.com/manifoldco/torus-cli/config"
"github.com/manifoldco/torus-cli/errs"
)
func init() {
logout := cli.Command{
Name: "logout",
Usage: "Destroy the current active session, unauthenticating the CLI",
Category: "ACCOUNT",
Action: chain(ensureDaemon, logoutCmd),
}
Cmds = append(Cmds, logout)
}
func logoutCmd(ctx *cli.Context) error {
cfg, err := config.LoadConfig()
if err != nil {
return err
}
client := api.NewClient(cfg)
err = client.Session.Logout(context.Background())
if err != nil {
if herr, ok := err.(*apitypes.Error); ok {
if herr.StatusCode == 404 {
fmt.Println("You are not logged in.")
return nil
}
}
return errs.NewErrorExitError("Logout failed.", err)
}
fmt.Println("You have successfully logged out. o/")
return nil
}