-
Notifications
You must be signed in to change notification settings - Fork 787
/
version.go
214 lines (192 loc) · 5.04 KB
/
version.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package cmd
import (
"fmt"
"io"
"regexp"
"strings"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/jenkins-x/jx/pkg/version"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
)
const (
jxChartPrefix = "jenkins-x-platform-"
)
type VersionOptions struct {
CommonOptions
Container string
Namespace string
HelmTLS bool
NoVersionCheck bool
}
func NewCmdVersion(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &VersionOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
}
cmd := &cobra.Command{
Use: "version",
Short: "Print the version information",
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
/*
cmd.Flags().BoolP("client", "c", false, "Client version only (no server required).")
cmd.Flags().BoolP("short", "", false, "Print just the version number.")
*/
options.addCommonFlags(cmd)
cmd.Flags().MarkShorthandDeprecated("client", "please use --client instead.")
cmd.Flags().BoolVarP(&options.HelmTLS, "helm-tls", "", false, "Whether to use TLS with helm")
cmd.Flags().BoolVarP(&options.NoVersionCheck, "no-version-check", "n", false, "Disable checking of version upgrade checks")
return cmd
}
func (o *VersionOptions) Run() error {
info := util.ColorInfo
table := o.CreateTable()
table.AddRow("NAME", "VERSION")
table.AddRow("jx", info(version.GetVersion()))
// Jenkins X version
output, err := o.Helm().ListCharts()
if err != nil {
log.Warnf("Failed to find helm installs: %s\n", err)
} else {
for _, line := range strings.Split(output, "\n") {
fields := strings.Split(line, "\t")
if len(fields) > 4 && strings.TrimSpace(fields[0]) == "jenkins-x" {
for _, f := range fields[4:] {
f = strings.TrimSpace(f)
if strings.HasPrefix(f, jxChartPrefix) {
chart := strings.TrimPrefix(f, jxChartPrefix)
table.AddRow("jenkins x platform", info(chart))
}
}
}
}
}
// Kubernetes version
client, _, err := o.KubeClient()
if err != nil {
log.Warnf("Failed to connect to Kubernetes: %s\n", err)
} else {
serverVersion, err := client.Discovery().ServerVersion()
if err != nil {
log.Warnf("Failed to get Kubernetes server version: %s\n", err)
} else if serverVersion != nil {
table.AddRow("Kubernetes cluster", info(serverVersion.String()))
}
}
// kubectl version
output, err = o.getCommandOutput("", "kubectl", "version", "--short")
if err != nil {
log.Warnf("Failed to get kubectl version: %s\n", err)
} else {
for i, line := range strings.Split(output, "\n") {
fields := strings.Fields(line)
if len(fields) > 1 {
v := fields[2]
if v != "" {
switch i {
case 0:
table.AddRow("kubectl", info(v))
case 1:
// Ignore K8S server details as we have these above
}
}
}
}
}
// helm version
output, err = o.Helm().Version(o.HelmTLS)
if err != nil {
log.Warnf("Failed to get helm version: %s\n", err)
} else {
helmBinary := o.Helm().HelmBinary()
if helmBinary == "helm3" {
table.AddRow("helm client", info(output))
} else {
for i, line := range strings.Split(output, "\n") {
fields := strings.Fields(line)
if len(fields) > 1 {
v := fields[1]
if v != "" {
switch i {
case 0:
table.AddRow("helm client", info(v))
case 1:
table.AddRow("helm server", info(v))
}
}
}
}
}
}
// git version
version, err := o.Git().Version()
if err != nil {
log.Warnf("Failed to get git version: %s\n", err)
} else {
table.AddRow("git", info(version))
}
table.Render()
if !o.NoVersionCheck {
return o.VersionCheck()
}
return nil
}
func (o *VersionOptions) VersionCheck() error {
newVersion, err := o.GetLatestJXVersion()
if err != nil {
return err
}
currentVersion, err := version.GetSemverVersion()
if err != nil {
return err
}
if newVersion.GT(currentVersion) {
// Do not ask to update if we are using a dev build...
for _, x := range currentVersion.Pre {
if x.VersionStr == "dev" {
return nil
}
}
app := util.ColorInfo("jx")
log.Warnf("\nA new %s version is available: %s\n", app, util.ColorInfo(newVersion.String()))
if o.BatchMode {
log.Warnf("To upgrade to this new version use: %s\n", util.ColorInfo("jx upgrade cli"))
} else {
message := fmt.Sprintf("Would you like to upgrade to the new %s version?", app)
if util.Confirm(message, true, "Please indicate if you would like to upgrade the binary version.", o.In, o.Out, o.Err) {
return o.UpgradeCli()
}
}
}
return nil
}
func (o *VersionOptions) UpgradeCli() error {
options := &UpgradeCLIOptions{
CreateOptions: CreateOptions{
CommonOptions: o.CommonOptions,
},
}
return options.Run()
}
func extractSemVer(text string) string {
re, err := regexp.Compile(".*SemVer:\"(.*)\"")
if err != nil {
return ""
}
matches := re.FindStringSubmatch(text)
if len(matches) > 1 {
return matches[1]
}
return ""
}