2016-06-22 14:28:45 -04:00
|
|
|
/*
|
2018-08-24 15:03:55 -04:00
|
|
|
Copyright The Helm Authors.
|
2016-06-22 14:28:45 -04:00
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
*/
|
|
|
|
|
|
2025-02-24 10:11:54 -05:00
|
|
|
package cmd
|
2016-05-25 15:30:25 -04:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2016-08-31 14:57:13 -04:00
|
|
|
"io"
|
2018-05-09 11:37:20 -04:00
|
|
|
"text/template"
|
2016-05-25 15:30:25 -04:00
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2016-06-03 18:35:01 -04:00
|
|
|
|
2024-12-26 16:33:51 -05:00
|
|
|
"helm.sh/helm/v4/internal/version"
|
2025-02-24 10:11:54 -05:00
|
|
|
"helm.sh/helm/v4/pkg/cmd/require"
|
2016-05-25 15:30:25 -04:00
|
|
|
)
|
|
|
|
|
|
2016-10-24 15:13:32 -04:00
|
|
|
const versionDesc = `
|
2018-04-13 22:55:47 -04:00
|
|
|
Show the version for Helm.
|
2016-10-24 15:13:32 -04:00
|
|
|
|
2018-04-13 22:55:47 -04:00
|
|
|
This will print a representation the version of Helm.
|
|
|
|
|
The output will look something like this:
|
2016-10-24 15:13:32 -04:00
|
|
|
|
2020-05-18 10:40:40 -04:00
|
|
|
version.BuildInfo{Version:"v3.2.1", GitCommit:"fe51cd1e31e6a202cba7dead9552a6d418ded79a", GitTreeState:"clean", GoVersion:"go1.13.10"}
|
2016-10-24 15:13:32 -04:00
|
|
|
|
2018-05-02 18:26:26 -04:00
|
|
|
- Version is the semantic version of the release.
|
2016-10-24 15:13:32 -04:00
|
|
|
- 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.
|
2020-05-18 10:40:40 -04:00
|
|
|
- GoVersion is the version of Go that was used to compile Helm.
|
2020-04-10 12:15:27 -04:00
|
|
|
|
|
|
|
|
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
|
2021-05-07 15:12:09 -04:00
|
|
|
|
2021-05-11 15:39:06 -04:00
|
|
|
For example, --template='Version: {{.Version}}' outputs 'Version: v3.2.1'.
|
2016-10-24 15:13:32 -04:00
|
|
|
`
|
|
|
|
|
|
2018-05-09 13:30:32 -04:00
|
|
|
type versionOptions struct {
|
2018-04-12 18:27:56 -04:00
|
|
|
short bool
|
|
|
|
|
template string
|
2016-05-25 15:30:25 -04:00
|
|
|
}
|
|
|
|
|
|
2018-04-13 22:55:47 -04:00
|
|
|
func newVersionCmd(out io.Writer) *cobra.Command {
|
2018-05-09 13:30:32 -04:00
|
|
|
o := &versionOptions{}
|
2016-10-14 23:05:04 -04:00
|
|
|
|
2016-08-31 14:57:13 -04:00
|
|
|
cmd := &cobra.Command{
|
2020-08-08 03:43:34 -04:00
|
|
|
Use: "version",
|
2025-09-16 17:26:39 -04:00
|
|
|
Short: "print the helm version information",
|
2020-08-08 03:43:34 -04:00
|
|
|
Long: versionDesc,
|
|
|
|
|
Args: require.NoArgs,
|
2024-08-17 10:00:23 -04:00
|
|
|
ValidArgsFunction: noMoreArgsCompFunc,
|
2024-03-11 17:13:34 -04:00
|
|
|
RunE: func(_ *cobra.Command, _ []string) error {
|
2018-05-09 13:30:32 -04:00
|
|
|
return o.run(out)
|
2016-08-31 14:57:13 -04:00
|
|
|
},
|
|
|
|
|
}
|
2016-10-24 13:57:17 -04:00
|
|
|
f := cmd.Flags()
|
2018-05-09 13:30:32 -04:00
|
|
|
f.BoolVar(&o.short, "short", false, "print the version number")
|
|
|
|
|
f.StringVar(&o.template, "template", "", "template for version string format")
|
2016-10-14 23:05:04 -04:00
|
|
|
|
2016-08-31 14:57:13 -04:00
|
|
|
return cmd
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-09 13:30:32 -04:00
|
|
|
func (o *versionOptions) run(out io.Writer) error {
|
|
|
|
|
if o.template != "" {
|
|
|
|
|
tt, err := template.New("_").Parse(o.template)
|
2018-05-09 11:37:20 -04:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2019-01-08 16:37:55 -05:00
|
|
|
return tt.Execute(out, version.Get())
|
2017-09-11 17:12:25 -04:00
|
|
|
}
|
2018-05-09 13:30:32 -04:00
|
|
|
fmt.Fprintln(out, formatVersion(o.short))
|
2018-04-12 18:27:56 -04:00
|
|
|
return nil
|
2017-09-11 17:12:25 -04:00
|
|
|
}
|
|
|
|
|
|
2018-05-09 11:37:20 -04:00
|
|
|
func formatVersion(short bool) string {
|
2019-01-08 16:37:55 -05:00
|
|
|
v := version.Get()
|
2016-12-16 17:41:35 -05:00
|
|
|
if short {
|
2019-06-10 18:05:21 -04:00
|
|
|
if len(v.GitCommit) >= 7 {
|
|
|
|
|
return fmt.Sprintf("%s+g%s", v.Version, v.GitCommit[:7])
|
|
|
|
|
}
|
|
|
|
|
return version.GetVersion()
|
2016-12-16 17:41:35 -05:00
|
|
|
}
|
|
|
|
|
return fmt.Sprintf("%#v", v)
|
|
|
|
|
}
|