diff --git a/pkg/kubectl/cmd/config/create_authinfo.go b/pkg/kubectl/cmd/config/create_authinfo.go index 0f8aa3b6fff..aeabd106d32 100644 --- a/pkg/kubectl/cmd/config/create_authinfo.go +++ b/pkg/kubectl/cmd/config/create_authinfo.go @@ -29,6 +29,7 @@ import ( "k8s.io/kubernetes/pkg/client/unversioned/clientcmd" clientcmdapi "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api" "k8s.io/kubernetes/pkg/util" + "k8s.io/kubernetes/pkg/util/flag" ) type createAuthInfoOptions struct { @@ -40,7 +41,7 @@ type createAuthInfoOptions struct { token util.StringFlag username util.StringFlag password util.StringFlag - embedCertData util.BoolFlag + embedCertData flag.Tristate } var create_authinfo_long = fmt.Sprintf(`Sets a user entry in kubeconfig diff --git a/pkg/kubectl/cmd/config/create_cluster.go b/pkg/kubectl/cmd/config/create_cluster.go index 79edfd0cf37..b3e08414e30 100644 --- a/pkg/kubectl/cmd/config/create_cluster.go +++ b/pkg/kubectl/cmd/config/create_cluster.go @@ -28,6 +28,7 @@ import ( "k8s.io/kubernetes/pkg/client/unversioned/clientcmd" clientcmdapi "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api" "k8s.io/kubernetes/pkg/util" + "k8s.io/kubernetes/pkg/util/flag" ) type createClusterOptions struct { @@ -35,9 +36,9 @@ type createClusterOptions struct { name string server util.StringFlag apiVersion util.StringFlag - insecureSkipTLSVerify util.BoolFlag + insecureSkipTLSVerify flag.Tristate certificateAuthority util.StringFlag - embedCAData util.BoolFlag + embedCAData flag.Tristate } const ( diff --git a/pkg/kubectl/cmd/config/view.go b/pkg/kubectl/cmd/config/view.go index 5d253a118fa..bcdf91788f4 100644 --- a/pkg/kubectl/cmd/config/view.go +++ b/pkg/kubectl/cmd/config/view.go @@ -28,12 +28,12 @@ import ( "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api/latest" "k8s.io/kubernetes/pkg/kubectl" cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" - "k8s.io/kubernetes/pkg/util" + "k8s.io/kubernetes/pkg/util/flag" ) type ViewOptions struct { ConfigAccess ConfigAccess - Merge util.BoolFlag + Merge flag.Tristate Flatten bool Minify bool RawByteData bool diff --git a/pkg/util/bool_flag.go b/pkg/util/bool_flag.go deleted file mode 100644 index 768fc30350b..00000000000 --- a/pkg/util/bool_flag.go +++ /dev/null @@ -1,65 +0,0 @@ -/* -Copyright 2014 The Kubernetes Authors All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package util - -import ( - "fmt" - "strconv" -) - -// BoolFlag is a boolean flag compatible with flags and pflags that keeps track of whether it had a value supplied or not. -// Getting this flag to act like a normal bool, where true/false are not required needs a little bit of extra code, example: -// f := cmd.Flags().VarPF(&BoolFlagVar, "flagname", "", "help about the flag") -// f.NoOptDefVal = "true" -type BoolFlag struct { - // If Set has been invoked this value is true - provided bool - // The exact value provided on the flag - value bool -} - -func (f *BoolFlag) Default(value bool) { - f.value = value -} - -func (f BoolFlag) String() string { - return fmt.Sprintf("%t", f.value) -} - -func (f BoolFlag) Value() bool { - return f.value -} - -func (f *BoolFlag) Set(value string) error { - boolVal, err := strconv.ParseBool(value) - if err != nil { - return err - } - - f.value = boolVal - f.provided = true - - return nil -} - -func (f BoolFlag) Provided() bool { - return f.provided -} - -func (f *BoolFlag) Type() string { - return "bool" -} diff --git a/pkg/util/flag/tristate.go b/pkg/util/flag/tristate.go new file mode 100644 index 00000000000..a9359695f5a --- /dev/null +++ b/pkg/util/flag/tristate.go @@ -0,0 +1,83 @@ +/* +Copyright 2014 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package flag + +import ( + "fmt" + "strconv" +) + +// Tristate is a flag compatible with flags and pflags that +// keeps track of whether it had a value supplied or not. +type Tristate int + +const ( + Unset Tristate = iota // 0 + True + False +) + +func (f *Tristate) Default(value bool) { + *f = triFromBool(value) +} + +func (f Tristate) String() string { + b := boolFromTri(f) + return fmt.Sprintf("%t", b) +} + +func (f Tristate) Value() bool { + b := boolFromTri(f) + return b +} + +func (f *Tristate) Set(value string) error { + boolVal, err := strconv.ParseBool(value) + if err != nil { + return err + } + + *f = triFromBool(boolVal) + return nil +} + +func (f Tristate) Provided() bool { + if f != Unset { + return true + } + return false +} + +func (f *Tristate) Type() string { + return "tristate" +} + +func boolFromTri(t Tristate) bool { + if t == True { + return true + } else { + return false + } +} + +func triFromBool(b bool) Tristate { + if b { + return True + } else { + return False + } +}