Improve login handling. See https://issues.apache.org/jira/browse/BROOKLYN-463.
Prompt for user name if not supplied. Check for 401 Unauthorized and give tidier output. Project: http://git-wip-us.apache.org/repos/asf/brooklyn-client/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-client/commit/4d6b4fdf Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-client/tree/4d6b4fdf Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-client/diff/4d6b4fdf Branch: refs/heads/master Commit: 4d6b4fdf57088150b33bf14f642a2cce203ff82f Parents: 052d3bd Author: Geoff Macartney <geoff.macart...@cloudsoftcorp.com> Authored: Tue Apr 4 14:08:18 2017 +0100 Committer: Geoff Macartney <geoff.macart...@cloudsoftcorp.com> Committed: Tue Jul 4 11:06:00 2017 +0100 ---------------------------------------------------------------------- cli/api/version/version.go | 9 +++++---- cli/commands/login.go | 24 ++++++++++++++++++++++-- cli/commands/version.go | 2 +- cli/net/net.go | 4 ++-- 4 files changed, 30 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-client/blob/4d6b4fdf/cli/api/version/version.go ---------------------------------------------------------------------- diff --git a/cli/api/version/version.go b/cli/api/version/version.go index 622068d..243cf5e 100644 --- a/cli/api/version/version.go +++ b/cli/api/version/version.go @@ -24,13 +24,14 @@ import ( "github.com/apache/brooklyn-client/cli/net" ) -func Version(network *net.Network) (models.VersionSummary, error) { +func Version(network *net.Network) (models.VersionSummary, int, error) { url := "/v1/server/version" var versionSummary models.VersionSummary - body, err := network.SendGetRequest(url) + req := network.NewGetRequest(url) + body, code, err := network.SendRequestGetStatusCode(req) if err != nil { - return versionSummary, err + return versionSummary, code, err } err = json.Unmarshal(body, &versionSummary) - return versionSummary, err + return versionSummary, code, err } http://git-wip-us.apache.org/repos/asf/brooklyn-client/blob/4d6b4fdf/cli/commands/login.go ---------------------------------------------------------------------- diff --git a/cli/commands/login.go b/cli/commands/login.go index b0413ce..b264fb0 100644 --- a/cli/commands/login.go +++ b/cli/commands/login.go @@ -21,6 +21,11 @@ package commands import ( "fmt" "syscall" + "bufio" + "os" + "strings" + "net/http" + "errors" "github.com/apache/brooklyn-client/cli/api/version" "github.com/apache/brooklyn-client/cli/command_metadata" @@ -76,6 +81,17 @@ func (cmd *Login) Run(scope scope.Scope, c *cli.Context) { cmd.network.BrooklynUrl = cmd.network.BrooklynUrl[0 : len(cmd.network.BrooklynUrl)-1] } + // Prompt for username if not supplied + if cmd.network.BrooklynUser == "" { + reader := bufio.NewReader(os.Stdin) + fmt.Print("Enter Username: ") + user, err := reader.ReadString('\n') + if err != nil { + error_handler.ErrorExit(err) + } + cmd.network.BrooklynUser = strings.TrimSpace(user) + } + // Prompt for password if not supplied (password is not echoed to screen if cmd.network.BrooklynUser != "" && cmd.network.BrooklynPass == "" { fmt.Print("Enter Password: ") @@ -84,7 +100,8 @@ func (cmd *Login) Run(scope scope.Scope, c *cli.Context) { error_handler.ErrorExit(err) } fmt.Printf("\n") - cmd.network.BrooklynPass = string(bytePassword) + password := string(bytePassword) + cmd.network.BrooklynPass = strings.TrimSpace(password) } if cmd.config.Map == nil { @@ -106,8 +123,11 @@ func (cmd *Login) Run(scope scope.Scope, c *cli.Context) { cmd.config.Map["skipSslChecks"] = cmd.network.SkipSslChecks cmd.config.Write() - loginVersion, err := version.Version(cmd.network) + loginVersion, code, err := version.Version(cmd.network) if nil != err { + if code == http.StatusUnauthorized { + err = errors.New("Unauthorized") + } error_handler.ErrorExit(err) } fmt.Printf("Connected to Brooklyn version %s at %s\n", loginVersion.Version, cmd.network.BrooklynUrl) http://git-wip-us.apache.org/repos/asf/brooklyn-client/blob/4d6b4fdf/cli/commands/version.go ---------------------------------------------------------------------- diff --git a/cli/commands/version.go b/cli/commands/version.go index 6cbdf1d..dae6074 100644 --- a/cli/commands/version.go +++ b/cli/commands/version.go @@ -51,7 +51,7 @@ func (cmd *Version) Run(scope scope.Scope, c *cli.Context) { if err := net.VerifyLoginURL(cmd.network); err != nil { error_handler.ErrorExit(err) } - version, err := version.Version(cmd.network) + version, _, err := version.Version(cmd.network) if nil != err { error_handler.ErrorExit(err) } http://git-wip-us.apache.org/repos/asf/brooklyn-client/blob/4d6b4fdf/cli/net/net.go ---------------------------------------------------------------------- diff --git a/cli/net/net.go b/cli/net/net.go index da86ffe..b8d8f40 100644 --- a/cli/net/net.go +++ b/cli/net/net.go @@ -121,7 +121,7 @@ func (net *Network) SendRequestGetStatusCode(req *http.Request) ([]byte, int, er client := net.makeClient() resp, err := client.Do(req) if err != nil { - return nil, 0, err + return nil, resp.StatusCode, err } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) @@ -129,7 +129,7 @@ func (net *Network) SendRequestGetStatusCode(req *http.Request) ([]byte, int, er body = nil } if failed := unsuccessful(resp.StatusCode); failed { - return nil, 0, makeError(resp, resp.StatusCode, body) + return nil, resp.StatusCode, makeError(resp, resp.StatusCode, body) } return body, resp.StatusCode, err }