From 467736d0fc2f293cc9aefba1f36a8e1c0297fd7c Mon Sep 17 00:00:00 2001 From: Allen Zhong Date: Wed, 12 Jan 2022 15:47:43 +0800 Subject: [PATCH] fix json output for check results (#1720) --- pkg/cluster/manager/check.go | 48 +++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/pkg/cluster/manager/check.go b/pkg/cluster/manager/check.go index ef8a3eef36..b90fa1d8f3 100644 --- a/pkg/cluster/manager/check.go +++ b/pkg/cluster/manager/check.go @@ -15,6 +15,7 @@ package manager import ( "context" + "encoding/json" "fmt" "path/filepath" "strings" @@ -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, @@ -435,7 +444,6 @@ func checkSystemInfo( return perrs.Trace(err) } - // FIXME: add fix result to output checkResultTable := [][]string{ // Header {"Node", "Check", "Result", "Message"}, @@ -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). @@ -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)