generated from brevdev/seed
-
Notifications
You must be signed in to change notification settings - Fork 9
/
logout.go
81 lines (69 loc) · 1.74 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Package logout is for the logout command
package logout
import (
"fmt"
"strings"
"github.com/hashicorp/go-multierror"
"github.com/spf13/cobra"
"github.com/brevdev/brev-cli/pkg/cmd/cmderrors"
breverrors "github.com/brevdev/brev-cli/pkg/errors"
)
type LogoutOptions struct {
auth Auth
store LogoutStore
}
type Auth interface {
Logout() error
}
type LogoutStore interface {
ClearDefaultOrganization() error
GetCurrentWorkspaceID() (string, error)
}
func NewCmdLogout(auth Auth, store LogoutStore) *cobra.Command {
opts := LogoutOptions{
auth: auth,
store: store,
}
cmd := &cobra.Command{
Annotations: map[string]string{"housekeeping": ""},
Use: "logout",
DisableFlagsInUseLine: true,
Short: "Log out of brev",
Long: "Log out of brev by deleting the credential file",
Example: "brev logout",
Args: cmderrors.TransformToValidationError(cobra.NoArgs),
RunE: func(cmd *cobra.Command, args []string) error {
err := opts.RunLogout()
if err != nil {
return breverrors.WrapAndTrace(err)
}
return nil
},
}
return cmd
}
func (o *LogoutOptions) RunLogout() error {
workspaceID, err := o.store.GetCurrentWorkspaceID()
if err != nil {
return breverrors.WrapAndTrace(err)
}
if workspaceID != "" {
return fmt.Errorf("can not logout of workspace")
}
// best effort
var allErr error
err = o.auth.Logout()
if err != nil {
if !strings.Contains(err.Error(), ".brev/credentials.json: no such file or directory") {
allErr = multierror.Append(err)
}
}
err = o.store.ClearDefaultOrganization()
if err != nil {
allErr = multierror.Append(err)
}
if allErr != nil {
return breverrors.WrapAndTrace(allErr)
}
return nil
}