-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
86 lines (69 loc) · 2.17 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
/*
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"
"k8s.io/helm/cmd/helm/require"
"k8s.io/helm/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:"v2.0.0", GitCommit:"ff52399e51bb880526e9cd0ed8386f6433b74da1", GitTreeState:"clean"}
- 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.
`
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")
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 {
return fmt.Sprintf("%s+g%s", v.Version, v.GitCommit[:7])
}
return fmt.Sprintf("%#v", v)
}