-
Notifications
You must be signed in to change notification settings - Fork 111
/
logout.go
56 lines (47 loc) · 1.3 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 auth
import (
"fmt"
"github.com/fatih/color"
"github.com/rilldata/rill/cli/pkg/cmdutil"
"github.com/rilldata/rill/cli/pkg/config"
"github.com/rilldata/rill/cli/pkg/dotrill"
adminv1 "github.com/rilldata/rill/proto/gen/rill/admin/v1"
"github.com/spf13/cobra"
)
// LogoutCmd is the command for logging out of a Rill account.
func LogoutCmd(cfg *config.Config) *cobra.Command {
cmd := &cobra.Command{
Use: "logout",
Short: "Logout of the Rill API",
RunE: func(cmd *cobra.Command, args []string) error {
warn := color.New(color.Bold).Add(color.FgYellow)
ctx := cmd.Context()
token := cfg.AdminToken()
if token == "" {
warn.Println("You are already logged out.")
return nil
}
client, err := cmdutil.Client(cfg)
if err != nil {
return err
}
defer client.Close()
_, err = client.RevokeCurrentAuthToken(ctx, &adminv1.RevokeCurrentAuthTokenRequest{})
if err != nil {
fmt.Printf("Failed to revoke token (did you revoke it manually?). Clearing local token anyway.\n")
}
err = dotrill.SetAccessToken("")
if err != nil {
return err
}
// Clear the state during logout
err = dotrill.SetDefaultOrg("")
if err != nil {
return err
}
color.New(color.FgGreen).Println("Successfully logged out.")
return nil
},
}
return cmd
}