Refactor bool_flag into sub pkg

pull/6/head
harry 2016-03-09 15:25:34 +08:00
parent b6924a322a
commit bc422f077a
5 changed files with 90 additions and 70 deletions

View File

@ -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

View File

@ -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 (

View File

@ -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

View File

@ -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"
}

83
pkg/util/flag/tristate.go Normal file
View File

@ -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
}
}