-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathpd_version_matrix.go
141 lines (122 loc) · 3.36 KB
/
pd_version_matrix.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
// Copyright 2021 PingCAP, Inc. Licensed under Apache-2.0.
// This proram can be used to generate a version relationship matrix for TiDB Dashboard and PD.
package main
import (
"flag"
"fmt"
"os"
"sort"
"strings"
"github.com/codeskyblue/go-sh"
"github.com/olekukonko/tablewriter"
"github.com/pingcap/log"
"go.uber.org/zap"
"golang.org/x/mod/modfile"
"golang.org/x/mod/semver"
)
var pdGitDir string
var dashboardGitDir = flag.String("dashboard", "", "TiDB Dashboard git path")
var outputFormat = flag.String("format", "mdtable", "Output format, accept values: text, mdtable, mdtable-link")
func mustSuccess(err error) {
if err != nil {
log.Fatal("Execute failed", zap.Error(err))
}
}
func listPDTags() []string {
output, err := sh.Command("git", "tag", sh.Dir(pdGitDir)).Output()
mustSuccess(err)
validTags := make([]string, 0)
pdTags := strings.Split(string(output), "\n")
for _, pdTag := range pdTags {
if !semver.IsValid(pdTag) {
continue
}
validTags = append(validTags, pdTag)
}
sort.SliceStable(validTags, func(i, j int) bool {
return semver.Compare(validTags[i], validTags[j]) < 0
})
return validTags
}
func lookupDashboardCommit(pdTag string) string {
output, err := sh.Command(
"git", "show", fmt.Sprintf("%s:go.mod", pdTag),
sh.Dir(pdGitDir)).Output()
mustSuccess(err)
f, err := modfile.Parse("go.mod", output, nil)
mustSuccess(err)
for _, r := range f.Require {
if r.Mod.Path != "github.com/pingcap-incubator/tidb-dashboard" &&
r.Mod.Path != "github.com/pingcap/tidb-dashboard" {
continue
}
if !semver.IsValid(r.Mod.Version) {
continue
}
versionSegments := strings.Split(r.Mod.Version, "-")
gitHash := versionSegments[2]
return gitHash
}
return ""
}
func lookupDashboardRelease(gitCommit string) string {
sess := sh.NewSession()
sess.Stderr = nil
output, err := sess.
Command("git", "show", fmt.Sprintf("%s:release-version", gitCommit), sh.Dir(*dashboardGitDir)).
Output()
if err != nil {
// It might be possible that the TiDB Dashboard is not using a calver.
return ""
}
lines := strings.Split(string(output), "\n")
for _, line := range lines {
if len(line) > 0 && !strings.HasPrefix(line, "#") {
return line
}
}
return ""
}
func main() {
flag.Parse()
if flag.NArg() < 1 {
fmt.Println("Usage: go run pd_version_matrix.go <pd_git_path>")
os.Exit(1)
return
}
pdGitDir = flag.Arg(0)
output := make([][]string, 0)
pdTags := listPDTags()
for _, pdTag := range pdTags {
if semver.Compare(pdTag, "v4.0.0") < 0 {
continue
}
dashboardCommit := lookupDashboardCommit(pdTag)
if dashboardCommit == "" {
continue
}
dashboardRelease := lookupDashboardRelease(dashboardCommit)
if dashboardRelease == "" {
continue
}
output = append(output, []string{pdTag, dashboardRelease})
}
switch *outputFormat {
case "mdtable", "mdtable-link":
if *outputFormat == "mdtable-link" {
for _, row := range output {
row[1] = fmt.Sprintf("[%s](https://github.com/pingcap/tidb-dashboard/releases/tag/v%s)", row[1], row[1])
}
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"PD Version", "TiDB Dashboard Version"})
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
table.AppendBulk(output)
table.Render()
default:
for _, row := range output {
fmt.Printf("%s: %s\n", row[0], row[1])
}
}
}