verflag.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Package verflag defines utility functions to handle command line flags
  14. // related to version of Kubernetes.
  15. package verflag
  16. import (
  17. "fmt"
  18. "os"
  19. "strconv"
  20. flag "github.com/spf13/pflag"
  21. "k8s.io/kubernetes/pkg/version"
  22. )
  23. type versionValue int
  24. const (
  25. VersionFalse versionValue = 0
  26. VersionTrue versionValue = 1
  27. VersionRaw versionValue = 2
  28. )
  29. const strRawVersion string = "raw"
  30. func (v *versionValue) IsBoolFlag() bool {
  31. return true
  32. }
  33. func (v *versionValue) Get() interface{} {
  34. return versionValue(*v)
  35. }
  36. func (v *versionValue) Set(s string) error {
  37. if s == strRawVersion {
  38. *v = VersionRaw
  39. return nil
  40. }
  41. boolVal, err := strconv.ParseBool(s)
  42. if boolVal {
  43. *v = VersionTrue
  44. } else {
  45. *v = VersionFalse
  46. }
  47. return err
  48. }
  49. func (v *versionValue) String() string {
  50. if *v == VersionRaw {
  51. return strRawVersion
  52. }
  53. return fmt.Sprintf("%v", bool(*v == VersionTrue))
  54. }
  55. // The type of the flag as required by the pflag.Value interface
  56. func (v *versionValue) Type() string {
  57. return "version"
  58. }
  59. func VersionVar(p *versionValue, name string, value versionValue, usage string) {
  60. *p = value
  61. flag.Var(p, name, usage)
  62. // "--version" will be treated as "--version=true"
  63. flag.Lookup(name).NoOptDefVal = "true"
  64. }
  65. func Version(name string, value versionValue, usage string) *versionValue {
  66. p := new(versionValue)
  67. VersionVar(p, name, value, usage)
  68. return p
  69. }
  70. const versionFlagName = "version"
  71. var (
  72. versionFlag = Version(versionFlagName, VersionFalse, "Print version information and quit")
  73. )
  74. // AddFlags registers this package's flags on arbitrary FlagSets, such that they point to the
  75. // same value as the global flags.
  76. func AddFlags(fs *flag.FlagSet) {
  77. fs.AddFlag(flag.Lookup(versionFlagName))
  78. }
  79. // PrintAndExitIfRequested will check if the -version flag was passed
  80. // and, if so, print the version and exit.
  81. func PrintAndExitIfRequested() {
  82. if *versionFlag == VersionRaw {
  83. fmt.Printf("%#v\n", version.Get())
  84. os.Exit(0)
  85. } else if *versionFlag == VersionTrue {
  86. fmt.Printf("Kubernetes %s\n", version.Get())
  87. os.Exit(0)
  88. }
  89. }