Skip to content

Commit 16bdf1a

Browse files
authored
Fix error printing when login command fails (#32)
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.
1 parent 8d97458 commit 16bdf1a

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

src/github.com/getnelson/nelson/login.go

+9-14
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package main
1818

1919
import (
2020
"encoding/json"
21-
"fmt"
21+
2222
"github.com/parnurzeal/gorequest"
2323
)
2424

@@ -36,9 +36,9 @@ type Session struct {
3636

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

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

7071
if len(errs) > 0 {
71-
errStrs := make([]string, len(errs))
72-
for i, e := range errs {
73-
errStrs[i] = e.Error()
74-
fmt.Print(e.Error())
75-
}
76-
77-
return errs[0], Session{}
72+
return Session{}, errs
7873
}
7974

8075
var result Session
8176
if err := json.Unmarshal(bytes, &result); err != nil {
82-
return err, Session{}
77+
return Session{}, []error{err}
8378
}
8479

85-
return nil, result
80+
return result, nil
8681
}

src/github.com/getnelson/nelson/main.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,13 @@ func main() {
123123
// fmt.Println("token: ", userGithubToken)
124124
// fmt.Println("host: ", host)
125125
pi.Start()
126-
Login(http, userGithubToken, host, disableTLS)
126+
errs := Login(http, userGithubToken, host, disableTLS)
127127
pi.Stop()
128+
if len(errs) != 0 {
129+
PrintTerminalErrors(errs)
130+
return cli.NewExitError("Login failed.", 1)
131+
}
132+
128133
fmt.Println("Successfully logged in to " + host)
129134
return nil
130135
},

0 commit comments

Comments
 (0)