github.com/stefanmcshane/helm@v0.0.0-20221213002717-88a4a2c6e77d/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  	"github.com/stefanmcshane/helm/cmd/helm/require"
    27  	"github.com/stefanmcshane/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:"v3.2.1", GitCommit:"fe51cd1e31e6a202cba7dead9552a6d418ded79a", GitTreeState:"clean", GoVersion:"go1.13.10"}
    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  - GoVersion is the version of Go that was used to compile Helm.
    43  
    44  When using the --template flag the following properties are available to use in
    45  the template:
    46  
    47  - .Version contains the semantic version of Helm
    48  - .GitCommit is the git commit
    49  - .GitTreeState is the state of the git tree when Helm was built
    50  - .GoVersion contains the version of Go that Helm was compiled with
    51  
    52  For example, --template='Version: {{.Version}}' outputs 'Version: v3.2.1'.
    53  `
    54  
    55  type versionOptions struct {
    56  	short    bool
    57  	template string
    58  }
    59  
    60  func newVersionCmd(out io.Writer) *cobra.Command {
    61  	o := &versionOptions{}
    62  
    63  	cmd := &cobra.Command{
    64  		Use:               "version",
    65  		Short:             "print the client version information",
    66  		Long:              versionDesc,
    67  		Args:              require.NoArgs,
    68  		ValidArgsFunction: noCompletions,
    69  		RunE: func(cmd *cobra.Command, args []string) error {
    70  			return o.run(out)
    71  		},
    72  	}
    73  	f := cmd.Flags()
    74  	f.BoolVar(&o.short, "short", false, "print the version number")
    75  	f.StringVar(&o.template, "template", "", "template for version string format")
    76  	f.BoolP("client", "c", true, "display client version information")
    77  	f.MarkHidden("client")
    78  
    79  	return cmd
    80  }
    81  
    82  func (o *versionOptions) run(out io.Writer) error {
    83  	if o.template != "" {
    84  		tt, err := template.New("_").Parse(o.template)
    85  		if err != nil {
    86  			return err
    87  		}
    88  		return tt.Execute(out, version.Get())
    89  	}
    90  	fmt.Fprintln(out, formatVersion(o.short))
    91  	return nil
    92  }
    93  
    94  func formatVersion(short bool) string {
    95  	v := version.Get()
    96  	if short {
    97  		if len(v.GitCommit) >= 7 {
    98  			return fmt.Sprintf("%s+g%s", v.Version, v.GitCommit[:7])
    99  		}
   100  		return version.GetVersion()
   101  	}
   102  	return fmt.Sprintf("%#v", v)
   103  }