Skip to content

Commit

Permalink
Fix error printing when login command fails (#32)
Browse files Browse the repository at this point in the history
Currently, when the login command fails, it looks like this -

```
> github nelson nelson.example.com
/ Post https://nelson.example.com/auth/github: dial tcp 10.10.10.10:443: connect: operation timed oSuccessfully logged in to nelson.example.com
```

This is confusing as the login actually failed yet it says "Successfully logged in", and it also truncates the error message.

This PR fixes that by ensuring all the errors are printed properly without the spinner interfering. It also honours the global timeout setting, and moves the error to the end of the return arg list as per Go convention.

There is a minor change in timeout behaviour too - previously, the login command would wait until the server times out. Now, it has a default timeout of 60 seconds, in line with all the other commands.
  • Loading branch information
slai authored Sep 21, 2020
1 parent 8d97458 commit 16bdf1a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
23 changes: 9 additions & 14 deletions src/github.com/getnelson/nelson/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package main

import (
"encoding/json"
"fmt"

"github.com/parnurzeal/gorequest"
)

Expand All @@ -36,9 +36,9 @@ type Session struct {

func Login(client *gorequest.SuperAgent, githubToken string, nelsonHost string, disableTLS bool) []error {
baseURL := createEndpointURL(nelsonHost, !disableTLS)
e, sess := createSession(client, githubToken, baseURL)
if e != nil {
return []error{e}
sess, errs := createSession(client, githubToken, baseURL)
if errs != nil {
return errs
}
writeConfigFile(sess, baseURL, defaultConfigPath()) // TIM: side-effect, discarding errors seems wrong
return nil
Expand All @@ -56,7 +56,7 @@ func createEndpointURL(host string, useTLS bool) string {
}

/* TODO: any error handling here... would be nice */
func createSession(client *gorequest.SuperAgent, githubToken string, baseURL string) (error, Session) {
func createSession(client *gorequest.SuperAgent, githubToken string, baseURL string) (Session, []error) {
ver := CreateSessionRequest{AccessToken: githubToken}
url := baseURL + "/auth/github"
_, bytes, errs := client.
Expand All @@ -65,22 +65,17 @@ func createSession(client *gorequest.SuperAgent, githubToken string, baseURL str
Send(ver).
SetCurlCommand(globalEnableCurl).
SetDebug(globalEnableDebug).
Timeout(GetTimeout(globalTimeoutSeconds)).
EndBytes()

if len(errs) > 0 {
errStrs := make([]string, len(errs))
for i, e := range errs {
errStrs[i] = e.Error()
fmt.Print(e.Error())
}

return errs[0], Session{}
return Session{}, errs
}

var result Session
if err := json.Unmarshal(bytes, &result); err != nil {
return err, Session{}
return Session{}, []error{err}
}

return nil, result
return result, nil
}
7 changes: 6 additions & 1 deletion src/github.com/getnelson/nelson/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,13 @@ func main() {
// fmt.Println("token: ", userGithubToken)
// fmt.Println("host: ", host)
pi.Start()
Login(http, userGithubToken, host, disableTLS)
errs := Login(http, userGithubToken, host, disableTLS)
pi.Stop()
if len(errs) != 0 {
PrintTerminalErrors(errs)
return cli.NewExitError("Login failed.", 1)
}

fmt.Println("Successfully logged in to " + host)
return nil
},
Expand Down

0 comments on commit 16bdf1a

Please sign in to comment.