Skip to content

Commit

Permalink
fix json output for check results (#1720)
Browse files Browse the repository at this point in the history
  • Loading branch information
AstroProfundis authored Jan 12, 2022
1 parent 6fe71fe commit 467736d
Showing 1 changed file with 34 additions and 14 deletions.
48 changes: 34 additions & 14 deletions pkg/cluster/manager/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package manager

import (
"context"
"encoding/json"
"fmt"
"path/filepath"
"strings"
Expand Down Expand Up @@ -150,6 +151,14 @@ func (m *Manager) CheckCluster(clusterOrTopoName, scaleoutTopo string, opt Check
return m.checkRegionsInfo(clusterOrTopoName, &topo, &gOpt)
}

// HostCheckResult represents the check result of each node
type HostCheckResult struct {
Node string `json:"node"`
Name string `json:"name"`
Status string `json:"status"`
Message string `json:"message"`
}

// checkSystemInfo performs series of checks and tests of the deploy server
func checkSystemInfo(
ctx context.Context,
Expand Down Expand Up @@ -435,7 +444,6 @@ func checkSystemInfo(
return perrs.Trace(err)
}

// FIXME: add fix result to output
checkResultTable := [][]string{
// Header
{"Node", "Check", "Result", "Message"},
Expand Down Expand Up @@ -469,12 +477,32 @@ func checkSystemInfo(
checkResults = append(checkResults, res...)
applyFixTasks = append(applyFixTasks, tf.BuildAsStep(fmt.Sprintf(" - Applying changes on %s", host)))
}
resLines := formatHostCheckResults(checkResults)
checkResultTable = append(checkResultTable, resLines...)

// print check results *before* trying to applying checks
// FIXME: add fix result to output, and display the table after fixing
tui.PrintTable(checkResultTable, true)
if gOpt.DisplayMode == "json" {
checkResultStruct := make([]HostCheckResult, 0)

for _, r := range checkResults {
checkResultStruct = append(checkResultStruct, HostCheckResult{
r.Node,
r.Name,
r.Status,
r.Message,
})
}
data, err := json.Marshal(struct {
Result []HostCheckResult `json:"result"`
}{Result: checkResultStruct})
if err != nil {
return err
}
fmt.Println(string(data))
} else {
resLines := formatHostCheckResults(checkResults)
checkResultTable = append(checkResultTable, resLines...)
// print check results *before* trying to applying checks
// FIXME: add fix result to output, and display the table after fixing
tui.PrintTable(checkResultTable, true)
}

if opt.ApplyFix {
tc := task.NewBuilder(logger).
Expand All @@ -492,14 +520,6 @@ func checkSystemInfo(
return nil
}

// HostCheckResult represents the check result of each node
type HostCheckResult struct {
Node string `json:"node"`
Name string `json:"name"`
Status string `json:"status"`
Message string `json:"message"`
}

// handleCheckResults parses the result of checks
func handleCheckResults(ctx context.Context, host string, opt *CheckOptions, t *task.Builder) ([]HostCheckResult, error) {
rr, _ := ctxt.GetInner(ctx).GetCheckResults(host)
Expand Down

0 comments on commit 467736d

Please sign in to comment.