forked from helm/helm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
100 lines (81 loc) · 2.71 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
/*
Copyright The Helm Authors.
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,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"io"
"text/template"
"github.com/spf13/cobra"
"helm.sh/helm/v3/cmd/helm/require"
"helm.sh/helm/v3/internal/version"
)
const versionDesc = `
Show the version for Helm.
This will print a representation the version of Helm.
The output will look something like this:
version.BuildInfo{Version:"v3.2.1", GitCommit:"fe51cd1e31e6a202cba7dead9552a6d418ded79a", GitTreeState:"clean", GoVersion:"go1.13.10"}
- Version is the semantic version of the release.
- GitCommit is the SHA for the commit that this version was built from.
- GitTreeState is "clean" if there are no local code changes when this binary was
built, and "dirty" if the binary was built from locally modified code.
- GoVersion is the version of Go that was used to compile Helm.
When using the --template flag the following properties are available to use in
the template:
- .Version contains the semantic version of Helm
- .GitCommit is the git commit
- .GitTreeState is the state of the git tree when Helm was built
- .GoVersion contains the version of Go that Helm was compiled with
`
type versionOptions struct {
short bool
template string
}
func newVersionCmd(out io.Writer) *cobra.Command {
o := &versionOptions{}
cmd := &cobra.Command{
Use: "version",
Short: "print the client version information",
Long: versionDesc,
Args: require.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return o.run(out)
},
}
f := cmd.Flags()
f.BoolVar(&o.short, "short", false, "print the version number")
f.StringVar(&o.template, "template", "", "template for version string format")
f.BoolP("client", "c", true, "display client version information")
f.MarkHidden("client")
return cmd
}
func (o *versionOptions) run(out io.Writer) error {
if o.template != "" {
tt, err := template.New("_").Parse(o.template)
if err != nil {
return err
}
return tt.Execute(out, version.Get())
}
fmt.Fprintln(out, formatVersion(o.short))
return nil
}
func formatVersion(short bool) string {
v := version.Get()
if short {
if len(v.GitCommit) >= 7 {
return fmt.Sprintf("%s+g%s", v.Version, v.GitCommit[:7])
}
return version.GetVersion()
}
return fmt.Sprintf("%#v", v)
}