-
-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathlogout.go
More file actions
67 lines (54 loc) · 1.43 KB
/
Copy pathlogout.go
File metadata and controls
67 lines (54 loc) · 1.43 KB
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
package cmd
import (
"fmt"
"log"
"os"
"github.com/microcks/microcks-cli/pkg/config"
"github.com/microcks/microcks-cli/pkg/connectors"
"github.com/spf13/cobra"
)
func NewLogoutCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command {
logoutCmd := &cobra.Command{
Use: "logout CONTEXT",
Short: "Log out from Microcks",
Long: "Log out from Microcks",
Example: `# Log out from a Microcks server URL
microcks logout http://localhost:8080
# Log out from a named context
microcks logout dev-context`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.HelpFunc()(cmd, args)
os.Exit(1)
}
target := args[0]
err := logoutContext(target, globalClientOpts.ConfigPath)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Logged out from '%s'\n", target)
},
}
return logoutCmd
}
func logoutContext(target, configPath string) error {
localCfg, err := config.ReadLocalConfig(configPath)
if err != nil {
return err
}
if localCfg == nil {
return fmt.Errorf("Nothing to logout from")
}
userName := target
if ctx, err := localCfg.ResolveContext(target); err == nil {
userName = ctx.User.Name
}
if ok := localCfg.RemoveToken(userName); !ok {
return fmt.Errorf("Context %s does not exist", target)
}
err = config.ValidateLocalConfig(*localCfg)
if err != nil {
return fmt.Errorf("Error in loging out: %s", err)
}
return config.WriteLocalConfig(*localCfg, configPath)
}