github.com/canthefason/helm@v2.2.1-0.20170221172616-16b043b8d505+incompatible/cmd/helm/version.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     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  	"errors"
    21  	"fmt"
    22  	"io"
    23  	"os"
    24  
    25  	"github.com/spf13/cobra"
    26  	"google.golang.org/grpc"
    27  	"google.golang.org/grpc/codes"
    28  
    29  	"k8s.io/helm/pkg/helm"
    30  	pb "k8s.io/helm/pkg/proto/hapi/version"
    31  	"k8s.io/helm/pkg/version"
    32  )
    33  
    34  const versionDesc = `
    35  Show the client and server versions for Helm and tiller.
    36  
    37  This will print a representation of the client and server versions of Helm and
    38  Tiller. The output will look something like this:
    39  
    40  Client: &version.Version{SemVer:"v2.0.0", GitCommit:"ff52399e51bb880526e9cd0ed8386f6433b74da1", GitTreeState:"clean"}
    41  Server: &version.Version{SemVer:"v2.0.0", GitCommit:"b0c113dfb9f612a9add796549da66c0d294508a3", GitTreeState:"clean"}
    42  
    43  - SemVer is the semantic version of the release.
    44  - GitCommit is the SHA for the commit that this version was built from.
    45  - GitTreeState is "clean" if there are no local code changes when this binary was
    46    built, and "dirty" if the binary was built from locally modified code.
    47  
    48  To print just the client version, use '--client'. To print just the server version,
    49  use '--server'.
    50  `
    51  
    52  type versionCmd struct {
    53  	out        io.Writer
    54  	client     helm.Interface
    55  	showClient bool
    56  	showServer bool
    57  	short      bool
    58  }
    59  
    60  func newVersionCmd(c helm.Interface, out io.Writer) *cobra.Command {
    61  	version := &versionCmd{
    62  		client: c,
    63  		out:    out,
    64  	}
    65  
    66  	cmd := &cobra.Command{
    67  		Use:   "version",
    68  		Short: "print the client/server version information",
    69  		Long:  versionDesc,
    70  		RunE: func(cmd *cobra.Command, args []string) error {
    71  			// If neither is explicitly set, show both.
    72  			if !version.showClient && !version.showServer {
    73  				version.showClient, version.showServer = true, true
    74  			}
    75  			if version.showServer {
    76  				// We do this manually instead of in PreRun because we only
    77  				// need a tunnel if server version is requested.
    78  				setupConnection(cmd, args)
    79  			}
    80  			version.client = ensureHelmClient(version.client)
    81  			return version.run()
    82  		},
    83  	}
    84  	f := cmd.Flags()
    85  	f.BoolVarP(&version.showClient, "client", "c", false, "client version only")
    86  	f.BoolVarP(&version.showServer, "server", "s", false, "server version only")
    87  	f.BoolVar(&version.short, "short", false, "print the version number")
    88  
    89  	return cmd
    90  }
    91  
    92  func (v *versionCmd) run() error {
    93  
    94  	if v.showClient {
    95  		cv := version.GetVersionProto()
    96  		fmt.Fprintf(v.out, "Client: %s\n", formatVersion(cv, v.short))
    97  	}
    98  
    99  	if !v.showServer {
   100  		return nil
   101  	}
   102  
   103  	resp, err := v.client.GetVersion()
   104  	if err != nil {
   105  		if grpc.Code(err) == codes.Unimplemented {
   106  			return errors.New("server is too old to know its version")
   107  		}
   108  		if flagDebug {
   109  			fmt.Fprintln(os.Stderr, err)
   110  		}
   111  		return errors.New("cannot connect to Tiller")
   112  	}
   113  	fmt.Fprintf(v.out, "Server: %s\n", formatVersion(resp.Version, v.short))
   114  	return nil
   115  }
   116  
   117  func formatVersion(v *pb.Version, short bool) string {
   118  	if short {
   119  		return fmt.Sprintf("%s+g%s", v.SemVer, v.GitCommit[:7])
   120  	}
   121  	return fmt.Sprintf("%#v", v)
   122  }