mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 17:30:00 +00:00
Refactor bool_flag into sub pkg
This commit is contained in:
parent
b6924a322a
commit
bc422f077a
@ -29,6 +29,7 @@ import (
|
|||||||
"k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
|
"k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
|
||||||
clientcmdapi "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api"
|
clientcmdapi "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api"
|
||||||
"k8s.io/kubernetes/pkg/util"
|
"k8s.io/kubernetes/pkg/util"
|
||||||
|
"k8s.io/kubernetes/pkg/util/flag"
|
||||||
)
|
)
|
||||||
|
|
||||||
type createAuthInfoOptions struct {
|
type createAuthInfoOptions struct {
|
||||||
@ -40,7 +41,7 @@ type createAuthInfoOptions struct {
|
|||||||
token util.StringFlag
|
token util.StringFlag
|
||||||
username util.StringFlag
|
username util.StringFlag
|
||||||
password util.StringFlag
|
password util.StringFlag
|
||||||
embedCertData util.BoolFlag
|
embedCertData flag.Tristate
|
||||||
}
|
}
|
||||||
|
|
||||||
var create_authinfo_long = fmt.Sprintf(`Sets a user entry in kubeconfig
|
var create_authinfo_long = fmt.Sprintf(`Sets a user entry in kubeconfig
|
||||||
|
@ -28,6 +28,7 @@ import (
|
|||||||
"k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
|
"k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
|
||||||
clientcmdapi "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api"
|
clientcmdapi "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api"
|
||||||
"k8s.io/kubernetes/pkg/util"
|
"k8s.io/kubernetes/pkg/util"
|
||||||
|
"k8s.io/kubernetes/pkg/util/flag"
|
||||||
)
|
)
|
||||||
|
|
||||||
type createClusterOptions struct {
|
type createClusterOptions struct {
|
||||||
@ -35,9 +36,9 @@ type createClusterOptions struct {
|
|||||||
name string
|
name string
|
||||||
server util.StringFlag
|
server util.StringFlag
|
||||||
apiVersion util.StringFlag
|
apiVersion util.StringFlag
|
||||||
insecureSkipTLSVerify util.BoolFlag
|
insecureSkipTLSVerify flag.Tristate
|
||||||
certificateAuthority util.StringFlag
|
certificateAuthority util.StringFlag
|
||||||
embedCAData util.BoolFlag
|
embedCAData flag.Tristate
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -28,12 +28,12 @@ import (
|
|||||||
"k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api/latest"
|
"k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api/latest"
|
||||||
"k8s.io/kubernetes/pkg/kubectl"
|
"k8s.io/kubernetes/pkg/kubectl"
|
||||||
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
|
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
|
||||||
"k8s.io/kubernetes/pkg/util"
|
"k8s.io/kubernetes/pkg/util/flag"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ViewOptions struct {
|
type ViewOptions struct {
|
||||||
ConfigAccess ConfigAccess
|
ConfigAccess ConfigAccess
|
||||||
Merge util.BoolFlag
|
Merge flag.Tristate
|
||||||
Flatten bool
|
Flatten bool
|
||||||
Minify bool
|
Minify bool
|
||||||
RawByteData bool
|
RawByteData bool
|
||||||
|
@ -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
83
pkg/util/flag/tristate.go
Normal 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
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user