forked from yorkie-team/yorkie
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add logout command to CLI (yorkie-team#571)
- Loading branch information
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |