Skip to content

Commit

Permalink
cluster: Add JSON output option for 'display'
Browse files Browse the repository at this point in the history
  • Loading branch information
dveeden committed May 11, 2021
1 parent a229992 commit 8d19108
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
Empty file added 1
Empty file.
1 change: 1 addition & 0 deletions components/cluster/command/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
31 changes: 31 additions & 0 deletions pkg/cluster/manager/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package manager

import (
"context"
"encoding/json"
"errors"
"fmt"
"math"
Expand Down Expand Up @@ -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
InstanceInfos []InstInfo
}

// Display cluster meta and topology.
func (m *Manager) Display(name string, opt operator.Options) error {
if err := clusterutil.ValidateClusterNameOrError(name); err != nil {
Expand All @@ -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))
Expand Down
4 changes: 3 additions & 1 deletion pkg/cluster/operation/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8d19108

Please sign in to comment.