Skip to content

Commit

Permalink
Add logout command to CLI (yorkie-team#571)
Browse files Browse the repository at this point in the history
  • Loading branch information
blurfx authored Jul 3, 2023
1 parent 3e34fc6 commit 39db60d
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
15 changes: 15 additions & 0 deletions cmd/yorkie/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,18 @@ func Save(config *Config) error {

return nil
}

// Delete deletes the configuration file.
func Delete() error {
configPathValue, err := configPath()
if err != nil {
fmt.Fprintln(os.Stderr, "get config path: %w", err)
os.Exit(1)
}

if err := os.Remove(filepath.Clean(configPathValue)); err != nil {
return fmt.Errorf("remove config file: %w", err)
}

return nil
}
52 changes: 52 additions & 0 deletions cmd/yorkie/logout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package main

import (
"errors"
"fmt"

"github.com/spf13/cobra"

"github.com/yorkie-team/yorkie/cmd/yorkie/config"
)

var (
flagForce bool
)

func newLogoutCmd() *cobra.Command {
return &cobra.Command{
Use: "logout",
Short: "Log out from the Yorkie server",
RunE: func(cmd *cobra.Command, args []string) error {
conf, err := config.Load()
if err != nil {
return err
}
if flagForce {
return config.Delete()
}
if config.RPCAddr == "" {
return errors.New("you must specify the server address to log out")
}
if _, ok := conf.Auths[config.RPCAddr]; !ok {
return fmt.Errorf("you are not logged in to %s", config.RPCAddr)
}
if len(conf.Auths) <= 1 {
return config.Delete()
}
delete(conf.Auths, config.RPCAddr)
return config.Save(conf)
},
}
}

func init() {
cmd := newLogoutCmd()
cmd.Flags().BoolVar(
&flagForce,
"force",
false,
"force log out from all servers",
)
rootCmd.AddCommand(cmd)
}

0 comments on commit 39db60d

Please sign in to comment.