From 2421c738a6a4c30a02943a43c8c5da21445fb56e Mon Sep 17 00:00:00 2001 From: Filipe Brandenburger Date: Fri, 29 Aug 2014 23:07:18 -0700 Subject: [PATCH 1/2] Add support for -version=raw to print the Go version.Info This can be helpful to print the internal fields of the version information that will contain more details about the git tree than the short -version output will. Tested: - Tested getting the raw version: $ _output/go/bin/kubecfg -version=raw version.Info{Major:"0", Minor:"1+", GitVersion:"v2.2.1-33-gdc4becd9765393", GitCommit:"dc4becd9765393fa05a3eb06f6af9e00068fe0c5", GitTreeState:"dirty"} - Tested getting the simple version: $ _output/go/bin/kubecfg -version Kubernetes version 0.1+, build dc4becd9765393fa05a3eb06f6af9e00068fe0c5 - Tested getting the help output: $ _output/go/bin/kubecfg 2>&1 | grep -- -version -version=false: Print version information and quit - Made sure -version=true and -version=false works, that -version with an invalid argument works as expected (prints usage help) and that -version followed by space will not try to use the next word as its argument (i.e. `-version raw` prints the version but not the raw one.) Signed-off-by: Filipe Brandenburger --- pkg/version/flag/flag.go | 58 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/pkg/version/flag/flag.go b/pkg/version/flag/flag.go index e467a80ca5f..4833f8f0cde 100644 --- a/pkg/version/flag/flag.go +++ b/pkg/version/flag/flag.go @@ -21,18 +21,72 @@ import ( "flag" "fmt" "os" + "strconv" "github.com/GoogleCloudPlatform/kubernetes/pkg/version" ) +type versionValue int + +const ( + VersionFalse versionValue = 0 + VersionTrue versionValue = 1 + VersionRaw versionValue = 2 +) + +const strRawVersion string = "raw" + +func (v *versionValue) IsBoolFlag() bool { + return true +} + +func (v *versionValue) Get() interface{} { + return versionValue(*v) +} + +func (v *versionValue) Set(s string) error { + if s == strRawVersion { + *v = VersionRaw + return nil + } + boolVal, err := strconv.ParseBool(s) + if boolVal { + *v = VersionTrue + } else { + *v = VersionFalse + } + return err +} + +func (v *versionValue) String() string { + if *v == VersionRaw { + return strRawVersion + } + return fmt.Sprintf("%v", bool(*v == VersionTrue)) +} + +func VersionVar(p *versionValue, name string, value versionValue, usage string) { + *p = value + flag.Var(p, name, usage) +} + +func Version(name string, value versionValue, usage string) *versionValue { + p := new(versionValue) + VersionVar(p, name, value, usage) + return p +} + var ( - versionFlag = flag.Bool("version", false, "Print version information and quit") + versionFlag = Version("version", VersionFalse, "Print version information and quit") ) // PrintAndExitIfRequested will check if the -version flag was passed // and, if so, print the version and exit. func PrintAndExitIfRequested() { - if *versionFlag { + if *versionFlag == VersionRaw { + fmt.Printf("%#v\n", version.Get()) + os.Exit(0) + } else if *versionFlag == VersionTrue { fmt.Printf("Kubernetes %s\n", version.Get()) os.Exit(0) } From 1d8067450c23b956ea1aab946e88850fc98558cb Mon Sep 17 00:00:00 2001 From: Filipe Brandenburger Date: Fri, 29 Aug 2014 23:19:32 -0700 Subject: [PATCH 2/2] Rename `pkg/version/flag` to `pkg/version/verflag` This avoids some conflict with the built-in `flag` module in Go. The module was already being renamed to `verflag` on import anyways, so we might as well just call it that. Tested: - hack/build-go.sh and ran the resulting binaries with -version args. Signed-off-by: Filipe Brandenburger --- cmd/apiserver/apiserver.go | 2 +- cmd/controller-manager/controller-manager.go | 2 +- cmd/kubecfg/kubecfg.go | 2 +- cmd/kubelet/kubelet.go | 2 +- cmd/proxy/proxy.go | 2 +- pkg/version/{flag/flag.go => verflag/verflag.go} | 5 +++-- plugin/cmd/scheduler/scheduler.go | 2 +- 7 files changed, 9 insertions(+), 8 deletions(-) rename pkg/version/{flag/flag.go => verflag/verflag.go} (94%) diff --git a/cmd/apiserver/apiserver.go b/cmd/apiserver/apiserver.go index 7a564b219a1..32856352052 100644 --- a/cmd/apiserver/apiserver.go +++ b/cmd/apiserver/apiserver.go @@ -30,7 +30,7 @@ import ( "github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider" "github.com/GoogleCloudPlatform/kubernetes/pkg/master" "github.com/GoogleCloudPlatform/kubernetes/pkg/util" - verflag "github.com/GoogleCloudPlatform/kubernetes/pkg/version/flag" + "github.com/GoogleCloudPlatform/kubernetes/pkg/version/verflag" "github.com/golang/glog" ) diff --git a/cmd/controller-manager/controller-manager.go b/cmd/controller-manager/controller-manager.go index 091e7bddeb1..c1372674c4d 100644 --- a/cmd/controller-manager/controller-manager.go +++ b/cmd/controller-manager/controller-manager.go @@ -27,7 +27,7 @@ import ( "github.com/GoogleCloudPlatform/kubernetes/pkg/client" "github.com/GoogleCloudPlatform/kubernetes/pkg/controller" "github.com/GoogleCloudPlatform/kubernetes/pkg/util" - verflag "github.com/GoogleCloudPlatform/kubernetes/pkg/version/flag" + "github.com/GoogleCloudPlatform/kubernetes/pkg/version/verflag" "github.com/golang/glog" ) diff --git a/cmd/kubecfg/kubecfg.go b/cmd/kubecfg/kubecfg.go index ae03a1a2649..040b46e753a 100644 --- a/cmd/kubecfg/kubecfg.go +++ b/cmd/kubecfg/kubecfg.go @@ -33,7 +33,7 @@ import ( "github.com/GoogleCloudPlatform/kubernetes/pkg/kubecfg" "github.com/GoogleCloudPlatform/kubernetes/pkg/util" "github.com/GoogleCloudPlatform/kubernetes/pkg/version" - verflag "github.com/GoogleCloudPlatform/kubernetes/pkg/version/flag" + "github.com/GoogleCloudPlatform/kubernetes/pkg/version/verflag" "github.com/golang/glog" ) diff --git a/cmd/kubelet/kubelet.go b/cmd/kubelet/kubelet.go index 05166581f7f..21052332c8f 100644 --- a/cmd/kubelet/kubelet.go +++ b/cmd/kubelet/kubelet.go @@ -36,7 +36,7 @@ import ( kconfig "github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/config" "github.com/GoogleCloudPlatform/kubernetes/pkg/tools" "github.com/GoogleCloudPlatform/kubernetes/pkg/util" - verflag "github.com/GoogleCloudPlatform/kubernetes/pkg/version/flag" + "github.com/GoogleCloudPlatform/kubernetes/pkg/version/verflag" "github.com/coreos/go-etcd/etcd" "github.com/fsouza/go-dockerclient" "github.com/golang/glog" diff --git a/cmd/proxy/proxy.go b/cmd/proxy/proxy.go index 982aa3f8795..adfcf3babc7 100644 --- a/cmd/proxy/proxy.go +++ b/cmd/proxy/proxy.go @@ -24,7 +24,7 @@ import ( "github.com/GoogleCloudPlatform/kubernetes/pkg/proxy" "github.com/GoogleCloudPlatform/kubernetes/pkg/proxy/config" "github.com/GoogleCloudPlatform/kubernetes/pkg/util" - verflag "github.com/GoogleCloudPlatform/kubernetes/pkg/version/flag" + "github.com/GoogleCloudPlatform/kubernetes/pkg/version/verflag" "github.com/coreos/go-etcd/etcd" "github.com/golang/glog" ) diff --git a/pkg/version/flag/flag.go b/pkg/version/verflag/verflag.go similarity index 94% rename from pkg/version/flag/flag.go rename to pkg/version/verflag/verflag.go index 4833f8f0cde..85c47bc5214 100644 --- a/pkg/version/flag/flag.go +++ b/pkg/version/verflag/verflag.go @@ -14,8 +14,9 @@ See the License for the specific language governing permissions and limitations under the License. */ -// Package flag defines utility functions to handle command line flags related to version of Kubernetes. -package flag +// Package verflag defines utility functions to handle command line flags +// related to version of Kubernetes. +package verflag import ( "flag" diff --git a/plugin/cmd/scheduler/scheduler.go b/plugin/cmd/scheduler/scheduler.go index 6a58f2420ed..b4ea15939fe 100644 --- a/plugin/cmd/scheduler/scheduler.go +++ b/plugin/cmd/scheduler/scheduler.go @@ -21,7 +21,7 @@ import ( "github.com/GoogleCloudPlatform/kubernetes/pkg/client" "github.com/GoogleCloudPlatform/kubernetes/pkg/util" - verflag "github.com/GoogleCloudPlatform/kubernetes/pkg/version/flag" + "github.com/GoogleCloudPlatform/kubernetes/pkg/version/verflag" "github.com/GoogleCloudPlatform/kubernetes/plugin/pkg/scheduler" "github.com/GoogleCloudPlatform/kubernetes/plugin/pkg/scheduler/factory" "github.com/golang/glog"