forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
printer.go
161 lines (141 loc) · 4.46 KB
/
printer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright 2015 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package printer
import (
"bytes"
"encoding/json"
"fmt"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/mysql"
log "github.com/sirupsen/logrus"
)
// Version information.
var (
TiDBBuildTS = "None"
TiDBGitHash = "None"
TiDBGitBranch = "None"
GoVersion = "None"
// TiKVMinVersion is the minimum version of TiKV that can be compatible with the current TiDB.
TiKVMinVersion = "2.0.0-rc.4.1"
)
// PrintTiDBInfo prints the TiDB version information.
func PrintTiDBInfo() {
log.Infof("Welcome to TiDB.")
log.Infof("Release Version: %s", mysql.TiDBReleaseVersion)
log.Infof("Git Commit Hash: %s", TiDBGitHash)
log.Infof("Git Branch: %s", TiDBGitBranch)
log.Infof("UTC Build Time: %s", TiDBBuildTS)
log.Infof("GoVersion: %s", GoVersion)
log.Infof("TiKV Min Version: %s", TiKVMinVersion)
configJSON, err := json.Marshal(config.GetGlobalConfig())
if err != nil {
panic(err)
}
log.Infof("Config: %s", configJSON)
}
// PrintRawTiDBInfo prints the TiDB version information without log info.
func PrintRawTiDBInfo() {
fmt.Println("Release Version: ", mysql.TiDBReleaseVersion)
fmt.Println("Git Commit Hash: ", TiDBGitHash)
fmt.Println("Git Commit Branch: ", TiDBGitBranch)
fmt.Println("UTC Build Time: ", TiDBBuildTS)
fmt.Println("GoVersion: ", GoVersion)
fmt.Println("TiKV Min Version: ", TiKVMinVersion)
}
// GetTiDBInfo returns the git hash and build time of this tidb-server binary.
func GetTiDBInfo() string {
return fmt.Sprintf("Release Version: %s\nGit Commit Hash: %s\nGit Branch: %s\nUTC Build Time: %s\nGoVersion: %s\nTiKV Min Version: %s",
mysql.TiDBReleaseVersion, TiDBGitHash, TiDBGitBranch, TiDBBuildTS, GoVersion, TiKVMinVersion)
}
// checkValidity checks whether cols and every data have the same length.
func checkValidity(cols []string, datas [][]string) bool {
colLen := len(cols)
if len(datas) == 0 || colLen == 0 {
return false
}
for _, data := range datas {
if colLen != len(data) {
return false
}
}
return true
}
func getMaxColLen(cols []string, datas [][]string) []int {
maxColLen := make([]int, len(cols))
for i, col := range cols {
maxColLen[i] = len(col)
}
for _, data := range datas {
for i, v := range data {
if len(v) > maxColLen[i] {
maxColLen[i] = len(v)
}
}
}
return maxColLen
}
func getPrintDivLine(maxColLen []int) []byte {
var value = make([]byte, 0)
for _, v := range maxColLen {
value = append(value, '+')
value = append(value, bytes.Repeat([]byte{'-'}, v+2)...)
}
value = append(value, '+')
value = append(value, '\n')
return value
}
func getPrintCol(cols []string, maxColLen []int) []byte {
var value = make([]byte, 0)
for i, v := range cols {
value = append(value, '|')
value = append(value, ' ')
value = append(value, []byte(v)...)
value = append(value, bytes.Repeat([]byte{' '}, maxColLen[i]+1-len(v))...)
}
value = append(value, '|')
value = append(value, '\n')
return value
}
func getPrintRow(data []string, maxColLen []int) []byte {
var value = make([]byte, 0)
for i, v := range data {
value = append(value, '|')
value = append(value, ' ')
value = append(value, []byte(v)...)
value = append(value, bytes.Repeat([]byte{' '}, maxColLen[i]+1-len(v))...)
}
value = append(value, '|')
value = append(value, '\n')
return value
}
func getPrintRows(datas [][]string, maxColLen []int) []byte {
var value = make([]byte, 0)
for _, data := range datas {
value = append(value, getPrintRow(data, maxColLen)...)
}
return value
}
// GetPrintResult gets a result with a formatted string.
func GetPrintResult(cols []string, datas [][]string) (string, bool) {
if !checkValidity(cols, datas) {
return "", false
}
var value = make([]byte, 0)
maxColLen := getMaxColLen(cols, datas)
value = append(value, getPrintDivLine(maxColLen)...)
value = append(value, getPrintCol(cols, maxColLen)...)
value = append(value, getPrintDivLine(maxColLen)...)
value = append(value, getPrintRows(datas, maxColLen)...)
value = append(value, getPrintDivLine(maxColLen)...)
return string(value), true
}