Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions cmd/kosli/multiHost.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func isMultiHost() bool {
func runMultiHost(args []string) (string, error) {
// Calls "innerMain" at least twice:
// - with the 0th host/api-token (primary)
// - with the n-th host/api-token (subsidiary)
// - with the 1st, 2nd, 3rd, etc host/api-token (secondaries)

opts := getMultiOpts()

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

args0 := argsAppendHostApiTokenFlags(0)
output0, err0 := runBufferedInnerMain(args0)
stdOut, stdErr := runBufferedInnerMain(args0)

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

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

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

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

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

if secondariesOut != "" {
// Ensure stdOut is prefixed with the primary hostname ONLY if we are
// in debug mode - we could be producing JSON.
stdOut = fmt.Sprintf("\n[debug] [%s]", opts.hosts[0]) + stdOut + "\n" + secondariesOut
}

var err error
if errorMessage != "" {
err = errors.New(errorMessage)
Expand Down
4 changes: 2 additions & 2 deletions cmd/kosli/multiHost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (suite *MultiHostTestSuite) TestRunDoubledHost() {
{
name: "only returns primary call output when both (2) calls succeed",
args: doubledArgs([]string{"kosli", "status"}),
stdOut: []string{"[http://localhost:8001]", "OK", ""},
stdOut: []string{"OK", ""},
err: error(nil),
},
} {
Expand Down Expand Up @@ -153,7 +153,7 @@ func (suite *MultiHostTestSuite) TestRunTripledHost() {
{
name: "only returns primary call output when all three calls succeed",
args: tripledArgs([]string{"kosli", "status"}),
stdOut: []string{"[http://localhost:8001]", "OK", ""},
stdOut: []string{"OK", ""},
err: error(nil),
},
} {
Expand Down