...

Source file src/k8s.io/component-base/version/verflag/verflag.go

Documentation: k8s.io/component-base/version/verflag

     1  /*
     2  Copyright 2014 The Kubernetes 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 verflag defines utility functions to handle command line flags
    18  // related to version of Kubernetes.
    19  package verflag
    20  
    21  import (
    22  	"fmt"
    23  	"io"
    24  	"os"
    25  	"strconv"
    26  	"strings"
    27  
    28  	flag "github.com/spf13/pflag"
    29  
    30  	"k8s.io/component-base/version"
    31  )
    32  
    33  type versionValue string
    34  
    35  const (
    36  	VersionFalse versionValue = "false"
    37  	VersionTrue  versionValue = "true"
    38  	VersionRaw   versionValue = "raw"
    39  )
    40  
    41  const strRawVersion string = "raw"
    42  
    43  func (v *versionValue) IsBoolFlag() bool {
    44  	return true
    45  }
    46  
    47  func (v *versionValue) Get() interface{} {
    48  	return versionValue(*v)
    49  }
    50  
    51  func (v *versionValue) Set(s string) error {
    52  	if s == strRawVersion {
    53  		*v = VersionRaw
    54  		return nil
    55  	}
    56  
    57  	if strings.HasPrefix(s, "v") {
    58  		err := version.SetDynamicVersion(s)
    59  		if err == nil {
    60  			*v = versionValue(s)
    61  		}
    62  		return err
    63  	}
    64  
    65  	boolVal, err := strconv.ParseBool(s)
    66  	if err == nil {
    67  		if boolVal {
    68  			*v = VersionTrue
    69  		} else {
    70  			*v = VersionFalse
    71  		}
    72  	}
    73  	return err
    74  }
    75  
    76  func (v *versionValue) String() string {
    77  	return string(*v)
    78  }
    79  
    80  // The type of the flag as required by the pflag.Value interface
    81  func (v *versionValue) Type() string {
    82  	return "version"
    83  }
    84  
    85  func VersionVar(p *versionValue, name string, value versionValue, usage string) {
    86  	*p = value
    87  	flag.Var(p, name, usage)
    88  	// "--version" will be treated as "--version=true"
    89  	flag.Lookup(name).NoOptDefVal = "true"
    90  }
    91  
    92  func Version(name string, value versionValue, usage string) *versionValue {
    93  	p := new(versionValue)
    94  	VersionVar(p, name, value, usage)
    95  	return p
    96  }
    97  
    98  const versionFlagName = "version"
    99  
   100  var (
   101  	versionFlag = Version(versionFlagName, VersionFalse, "--version, --version=raw prints version information and quits; --version=vX.Y.Z... sets the reported version")
   102  	programName = "Kubernetes"
   103  )
   104  
   105  // AddFlags registers this package's flags on arbitrary FlagSets, such that they point to the
   106  // same value as the global flags.
   107  func AddFlags(fs *flag.FlagSet) {
   108  	fs.AddFlag(flag.Lookup(versionFlagName))
   109  }
   110  
   111  // variables for unit testing PrintAndExitIfRequested
   112  var (
   113  	output = io.Writer(os.Stdout)
   114  	exit   = os.Exit
   115  )
   116  
   117  // PrintAndExitIfRequested will check if --version or --version=raw was passed
   118  // and, if so, print the version and exit.
   119  func PrintAndExitIfRequested() {
   120  	if *versionFlag == VersionRaw {
   121  		fmt.Fprintf(output, "%#v\n", version.Get())
   122  		exit(0)
   123  	} else if *versionFlag == VersionTrue {
   124  		fmt.Fprintf(output, "%s %s\n", programName, version.Get())
   125  		exit(0)
   126  	}
   127  }
   128  

View as plain text