From ae865f571033605c2b15b422f06d4f0273ec922e Mon Sep 17 00:00:00 2001 From: Samuel Coleman Date: Wed, 4 Oct 2017 11:55:00 -0400 Subject: [PATCH] Easy logout when only one username is logged in. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change the behaviour of the “logout” command to make the username optional when only one user is logged in. This does not change the behaviour of the command when a username is given. Notably, invocation of the command with no username specified may or may not exit with error; but the command never exits with error when a username is specified. --- command/logout.go | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/command/logout.go b/command/logout.go index 069af58f..6d1d8592 100644 --- a/command/logout.go +++ b/command/logout.go @@ -6,18 +6,22 @@ import ( "runtime" . "github.com/heroku/force/config" + . "github.com/heroku/force/error" . "github.com/heroku/force/lib" ) var cmdLogout = &Command{ - Usage: "logout", - Short: "Log out from force.com", + Usage: "logout [-u=username]", + Short: "Log out from Force.com", Long: ` - force logout -u=username +Log out from Force.com - Example: +The username may be omitted when only one account is logged in, in which case +that single logged-in account will be logged out. - force logout -u=user@example.org +Examples: + force logout + force logout -u=user@example.org `, } @@ -26,17 +30,24 @@ func init() { } var ( - userName1 = cmdLogout.Flag.String("u", "", "Username for Soap Login") + logoutUserName = cmdLogout.Flag.String("u", "", "Username to log out") ) func runLogout(cmd *Command, args []string) { - if *userName1 == "" { - fmt.Println("Missing required argument...") - cmd.Flag.Usage() - return + /* If a username was specified, we will use it regardless of logins. + * Otherwise, if there's only one login, we'll use that. */ + if *logoutUserName == "" { + accounts, _ := Config.List("accounts") + if len(accounts) == 0 { + ErrorAndExit("No logins, so a username cannot be assumed.") + } else if len(accounts) > 1 { + ErrorAndExit("More than one login. Please specify a username.") + } else { + logoutUserName = &accounts[0] + } } - Config.Delete("accounts", *userName1) - if active, _ := Config.Load("current", "account"); active == *userName1 { + Config.Delete("accounts", *logoutUserName) + if active, _ := Config.Load("current", "account"); active == *logoutUserName { Config.Delete("current", "account") SetActiveLoginDefault() }