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