-
Notifications
You must be signed in to change notification settings - Fork 55
/
logout.go
77 lines (60 loc) · 1.49 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
package cmd
import (
"os"
"path/filepath"
"github.com/infrahq/infra/internal"
"github.com/infrahq/infra/internal/api"
"github.com/infrahq/infra/internal/logging"
)
type LogoutOptions struct {
internal.Options `mapstructure:",squash"`
}
func cleanupKubeconfig(config *ClientHostConfig) error {
if config.Current {
logging.L.Debug("cleaning up kubeconfig")
_ = updateKubeconfig(api.User{})
infraDir, err := infraHomeDir()
if err == nil {
logging.L.Debug("cleaning up cache")
os.RemoveAll(filepath.Join(infraDir, "cache"))
}
}
return removeHostConfig(config.Host)
}
func logoutOne(config *ClientHostConfig) error {
logging.S.Debugf("logging out %s", config.Host)
client, err := apiClientFromConfig(config.Host)
if err != nil {
logging.S.Warnf("%s", err.Error())
return cleanupKubeconfig(config)
}
ctx, err := apiContextFromConfig(config.Host)
if err != nil {
logging.S.Warnf("%s", err.Error())
return cleanupKubeconfig(config)
}
_, err = client.AuthAPI.Logout(ctx).Execute()
if err != nil {
logging.S.Warnf("%s", err.Error())
return cleanupKubeconfig(config)
}
return cleanupKubeconfig(config)
}
func logout(options *LogoutOptions) error {
if options.Host == "" {
configs, _ := readConfig()
if configs != nil {
for i := range configs.Hosts {
_ = logoutOne(&configs.Hosts[i])
}
}
return nil
}
config, err := readHostConfig(options.Host)
if err != nil {
logging.S.Warnf("%s", err.Error())
return nil
}
_ = logoutOne(config)
return nil
}