From 5e7f8ed6282dd22e1f11ce84b81c5c54a61455af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Tue, 11 May 2021 08:45:23 +0200 Subject: [PATCH] cluster: Add JSON output option for 'display' https://github.com/pingcap/tiup/issues/1355 --- 1 | 0 components/cluster/command/display.go | 1 + pkg/cluster/manager/display.go | 31 +++++++++++++++++++++++++++ pkg/cluster/operation/operation.go | 4 +++- 4 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 1 diff --git a/1 b/1 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/components/cluster/command/display.go b/components/cluster/command/display.go index c76adfc63e..3c6f8b0f18 100644 --- a/components/cluster/command/display.go +++ b/components/cluster/command/display.go @@ -84,6 +84,7 @@ func newDisplayCmd() *cobra.Command { cmd.Flags().BoolVar(&gOpt.ShowUptime, "uptime", false, "Display with uptime") cmd.Flags().BoolVar(&showDashboardOnly, "dashboard", false, "Only display TiDB Dashboard information") cmd.Flags().BoolVar(&showVersionOnly, "version", false, "Only display TiDB cluster version") + cmd.Flags().BoolVar(&gOpt.Json, "json", false, "Output in JSON format when applicable") return cmd } diff --git a/pkg/cluster/manager/display.go b/pkg/cluster/manager/display.go index e3413f66e5..57c3ee1a10 100644 --- a/pkg/cluster/manager/display.go +++ b/pkg/cluster/manager/display.go @@ -15,6 +15,7 @@ package manager import ( "context" + "encoding/json" "errors" "fmt" "math" @@ -53,6 +54,18 @@ type InstInfo struct { Port int } +type DashboardInfo struct { + ClusterType string `json:"cluster_type"` + ClusterName string `json:"cluster_name"` + ClusterVersion string `json:"cluster_version"` + SSHType string `json:"ssh_type"` +} + +type JsonOutput struct { + DashboardInfo DashboardInfo `json:"dashboard"` + InstanceInfos []InstInfo `json:"instances"` +} + // Display cluster meta and topology. func (m *Manager) Display(name string, opt operator.Options) error { if err := clusterutil.ValidateClusterNameOrError(name); err != nil { @@ -68,6 +81,24 @@ func (m *Manager) Display(name string, opt operator.Options) error { topo := metadata.GetTopology() base := metadata.GetBaseMeta() // display cluster meta + if opt.Json { + j := &JsonOutput{ + DashboardInfo: DashboardInfo{ + m.sysName, + name, + base.Version, + string(topo.BaseTopo().GlobalOptions.SSHType), + }, + InstanceInfos: clusterInstInfos, + } + + d, err := json.MarshalIndent(j, "", " ") + if err != nil { + return err + } + fmt.Println(string(d)) + return nil + } cyan := color.New(color.FgCyan, color.Bold) fmt.Printf("Cluster type: %s\n", cyan.Sprint(m.sysName)) fmt.Printf("Cluster name: %s\n", cyan.Sprint(name)) diff --git a/pkg/cluster/operation/operation.go b/pkg/cluster/operation/operation.go index a729dad539..fbb38e3dae 100644 --- a/pkg/cluster/operation/operation.go +++ b/pkg/cluster/operation/operation.go @@ -43,7 +43,9 @@ type Options struct { // Show uptime or not ShowUptime bool - Operation Operation + + Json bool + Operation Operation } // Operation represents the type of cluster operation