github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/version/verflag/verflag.go (about)

     1  /*
     2  Copyright 2014 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 verflag defines utility functions to handle command line flags
    18  // related to version of Kubernetes.
    19  package verflag
    20  
    21  import (
    22  	"fmt"
    23  	"os"
    24  	"strconv"
    25  
    26  	flag "github.com/spf13/pflag"
    27  	"k8s.io/kubernetes/pkg/version"
    28  )
    29  
    30  type versionValue int
    31  
    32  const (
    33  	VersionFalse versionValue = 0
    34  	VersionTrue  versionValue = 1
    35  	VersionRaw   versionValue = 2
    36  )
    37  
    38  const strRawVersion string = "raw"
    39  
    40  func (v *versionValue) IsBoolFlag() bool {
    41  	return true
    42  }
    43  
    44  func (v *versionValue) Get() interface{} {
    45  	return versionValue(*v)
    46  }
    47  
    48  func (v *versionValue) Set(s string) error {
    49  	if s == strRawVersion {
    50  		*v = VersionRaw
    51  		return nil
    52  	}
    53  	boolVal, err := strconv.ParseBool(s)
    54  	if boolVal {
    55  		*v = VersionTrue
    56  	} else {
    57  		*v = VersionFalse
    58  	}
    59  	return err
    60  }
    61  
    62  func (v *versionValue) String() string {
    63  	if *v == VersionRaw {
    64  		return strRawVersion
    65  	}
    66  	return fmt.Sprintf("%v", bool(*v == VersionTrue))
    67  }
    68  
    69  // The type of the flag as required by the pflag.Value interface
    70  func (v *versionValue) Type() string {
    71  	return "version"
    72  }
    73  
    74  func VersionVar(p *versionValue, name string, value versionValue, usage string) {
    75  	*p = value
    76  	flag.Var(p, name, usage)
    77  }
    78  
    79  func Version(name string, value versionValue, usage string) *versionValue {
    80  	p := new(versionValue)
    81  	VersionVar(p, name, value, usage)
    82  	return p
    83  }
    84  
    85  var (
    86  	versionFlag = Version("version", VersionFalse, "Print version information and quit")
    87  )
    88  
    89  // PrintAndExitIfRequested will check if the -version flag was passed
    90  // and, if so, print the version and exit.
    91  func PrintAndExitIfRequested() {
    92  	if *versionFlag == VersionRaw {
    93  		fmt.Printf("%#v\n", version.Get())
    94  		os.Exit(0)
    95  	} else if *versionFlag == VersionTrue {
    96  		fmt.Printf("Kubernetes %s\n", version.Get())
    97  		os.Exit(0)
    98  	}
    99  }