github.com/souleb/helm@v3.0.0-beta.3+incompatible/cmd/helm/version.go (about) 1 /* 2 Copyright The Helm Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "fmt" 21 "io" 22 "text/template" 23 24 "github.com/spf13/cobra" 25 26 "helm.sh/helm/cmd/helm/require" 27 "helm.sh/helm/internal/version" 28 ) 29 30 const versionDesc = ` 31 Show the version for Helm. 32 33 This will print a representation the version of Helm. 34 The output will look something like this: 35 36 version.BuildInfo{Version:"v2.0.0", GitCommit:"ff52399e51bb880526e9cd0ed8386f6433b74da1", GitTreeState:"clean"} 37 38 - Version is the semantic version of the release. 39 - GitCommit is the SHA for the commit that this version was built from. 40 - GitTreeState is "clean" if there are no local code changes when this binary was 41 built, and "dirty" if the binary was built from locally modified code. 42 ` 43 44 type versionOptions struct { 45 short bool 46 template string 47 } 48 49 func newVersionCmd(out io.Writer) *cobra.Command { 50 o := &versionOptions{} 51 52 cmd := &cobra.Command{ 53 Use: "version", 54 Short: "print the client version information", 55 Long: versionDesc, 56 Args: require.NoArgs, 57 RunE: func(cmd *cobra.Command, args []string) error { 58 return o.run(out) 59 }, 60 } 61 f := cmd.Flags() 62 f.BoolVar(&o.short, "short", false, "print the version number") 63 f.StringVar(&o.template, "template", "", "template for version string format") 64 65 return cmd 66 } 67 68 func (o *versionOptions) run(out io.Writer) error { 69 if o.template != "" { 70 tt, err := template.New("_").Parse(o.template) 71 if err != nil { 72 return err 73 } 74 return tt.Execute(out, version.Get()) 75 } 76 fmt.Fprintln(out, formatVersion(o.short)) 77 return nil 78 } 79 80 func formatVersion(short bool) string { 81 v := version.Get() 82 if short { 83 if len(v.GitCommit) >= 7 { 84 return fmt.Sprintf("%s+g%s", v.Version, v.GitCommit[:7]) 85 } 86 return version.GetVersion() 87 } 88 return fmt.Sprintf("%#v", v) 89 }