Skip to content

Commit 09c5b2e

Browse files
authored
Fix multihost printing (#646)
A previous commit fda38e9 prefixed all output on stdout with the hostname when in multi-host mode. This broke clients (cyber-dojo) making CLI calls with the --output=json which of course no longer produced JSON. Doh! This PR fixes this so stdout is prefixed with the hostname (in multi-host mode) only in debug mode.
1 parent 3131810 commit 09c5b2e

2 files changed

Lines changed: 16 additions & 9 deletions

File tree

cmd/kosli/multiHost.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func isMultiHost() bool {
3737
func runMultiHost(args []string) (string, error) {
3838
// Calls "innerMain" at least twice:
3939
// - with the 0th host/api-token (primary)
40-
// - with the n-th host/api-token (subsidiary)
40+
// - with the 1st, 2nd, 3rd, etc host/api-token (secondaries)
4141

4242
opts := getMultiOpts()
4343

@@ -51,23 +51,24 @@ func runMultiHost(args []string) (string, error) {
5151
}
5252

5353
args0 := argsAppendHostApiTokenFlags(0)
54-
output0, err0 := runBufferedInnerMain(args0)
54+
stdOut, stdErr := runBufferedInnerMain(args0)
5555

56-
stdOut := fmt.Sprintf("[%s]\n", opts.hosts[0]) + output0
5756
var errorMessage string
5857

59-
if err0 != nil {
60-
errorMessage += err0.Error()
58+
if stdErr != nil {
59+
errorMessage = fmt.Sprintf("[%s]\n", opts.hosts[0])
60+
errorMessage += stdErr.Error()
6161
}
6262

63+
secondariesOut := ""
6364
for i := 1; i < len(opts.hosts); i++ {
6465
argsN := argsAppendHostApiTokenFlags(i)
6566
outputN, errN := runBufferedInnerMain(argsN)
6667

6768
// Return subsidiary-call's output in debug mode only.
6869
if opts.debug && outputN != "" {
69-
stdOut += fmt.Sprintf("\n[debug] [%s]", opts.hosts[i])
70-
stdOut += fmt.Sprintf("\n%s", outputN)
70+
secondariesOut += fmt.Sprintf("\n[debug] [%s]", opts.hosts[i])
71+
secondariesOut += fmt.Sprintf("\n%s", outputN)
7172
}
7273

7374
// Make origin of subsidiary-call failure clear.
@@ -77,6 +78,12 @@ func runMultiHost(args []string) (string, error) {
7778
}
7879
}
7980

81+
if secondariesOut != "" {
82+
// Ensure stdOut is prefixed with the primary hostname ONLY if we are
83+
// in debug mode - we could be producing JSON.
84+
stdOut = fmt.Sprintf("\n[debug] [%s]", opts.hosts[0]) + stdOut + "\n" + secondariesOut
85+
}
86+
8087
var err error
8188
if errorMessage != "" {
8289
err = errors.New(errorMessage)

cmd/kosli/multiHost_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func (suite *MultiHostTestSuite) TestRunDoubledHost() {
118118
{
119119
name: "only returns primary call output when both (2) calls succeed",
120120
args: doubledArgs([]string{"kosli", "status"}),
121-
stdOut: []string{"[http://localhost:8001]", "OK", ""},
121+
stdOut: []string{"OK", ""},
122122
err: error(nil),
123123
},
124124
} {
@@ -153,7 +153,7 @@ func (suite *MultiHostTestSuite) TestRunTripledHost() {
153153
{
154154
name: "only returns primary call output when all three calls succeed",
155155
args: tripledArgs([]string{"kosli", "status"}),
156-
stdOut: []string{"[http://localhost:8001]", "OK", ""},
156+
stdOut: []string{"OK", ""},
157157
err: error(nil),
158158
},
159159
} {

0 commit comments

Comments
 (0)