Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func NewCommad() *cobra.Command {
command.AddCommand(NewStopCommand())
command.AddCommand(NewContextCommand())
command.AddCommand(NewLoginCommand(&clientOpts))
command.AddCommand(NewLogoutCommand(&clientOpts))

defaultLocalConfigPath, err := config.DefaultLocalConfigPath()
errors.CheckError(err)
Expand Down
54 changes: 54 additions & 0 deletions cmd/logout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package cmd

import (
"fmt"
"log"
"os"

"github.com/microcks/microcks-cli/pkg/config"
"github.com/microcks/microcks-cli/pkg/connectors"
"github.com/microcks/microcks-cli/pkg/errors"
"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: `# To log out of Microcks
$ microcks logout`,

Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.HelpFunc()(cmd, args)
os.Exit(1)
}

context := args[0]
localCfg, err := config.ReadLocalConfig(globalClientOpts.ConfigPath)
errors.CheckError(err)
if localCfg == nil {
log.Fatalf("Nothing to logout from")
}

// Remove authToken
ok := localCfg.RemoveToken(context)
if !ok {
log.Fatalf("Context %s does not exist", context)
}

err = config.ValidateLocalConfig(*localCfg)
if err != nil {
log.Fatalf("Error in loging out: %s", err)
}
err = config.WriteLocalConfig(*localCfg, globalClientOpts.ConfigPath)
errors.CheckError(err)

fmt.Printf("Logged out from '%s'\n", context)
},
}

return logoutCmd
}
12 changes: 12 additions & 0 deletions pkg/config/localconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,18 @@ func (l *LocalConfig) RemoveContext(serverName string) (string, bool) {
return "", false
}

// Returns true if user was removed successfully
func (l *LocalConfig) RemoveToken(serverName string) bool {
for i, u := range l.Users {
if u.Name == serverName {
l.Users[i].RefreshToken = ""
l.Users[i].AuthToken = ""
return true
}
}
return false
}

func (l *LocalConfig) GetUser(name string) (*User, error) {
for _, u := range l.Users {
if u.Name == name {
Expand Down