Skip to content

Commit 0304b91

Browse files
author
Achille
authored
Merge pull request #4 from segmentio/fix-bool-options
fix bool options
2 parents 2589d20 + f21df90 commit 0304b91

File tree

3 files changed

+46
-19
lines changed

3 files changed

+46
-19
lines changed

curl.go

+27-13
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ import (
3232
var (
3333
curlOptions = curl.NewOptionSet()
3434

35-
help bool
36-
debug bool
37-
usage string
38-
flags *pflag.FlagSet
39-
config *genericclioptions.ConfigFlags
35+
help bool
36+
debug bool
37+
options string
38+
flags *pflag.FlagSet
39+
config *genericclioptions.ConfigFlags
4040
)
4141

4242
func init() {
@@ -68,12 +68,15 @@ func init() {
6868
short = ""
6969
}
7070

71-
flags.VarP(opt.Value, name, short, opt.Help)
71+
flag := flags.VarPF(opt.Value, name, short, opt.Help)
72+
if curl.IsBoolFlag(opt.Value) {
73+
flag.NoOptDefVal = "true"
74+
}
7275
}
7376

7477
config = genericclioptions.NewConfigFlags(false)
7578
config.AddFlags(flags)
76-
usage = flags.FlagUsages()
79+
options = flags.FlagUsages()
7780
}
7881

7982
func main() {
@@ -90,7 +93,7 @@ func run(ctx context.Context) error {
9093
flags.Parse(os.Args[1:])
9194

9295
if help {
93-
printUsage()
96+
fmt.Print(usageAndOptions("Run curl against kubernetes pods"))
9497
return nil
9598
}
9699

@@ -111,8 +114,10 @@ func run(ctx context.Context) error {
111114
query, containerName = args[0], args[1]
112115
case 1:
113116
query = args[0]
117+
case 0:
118+
return usageError("not enough arguments passed in the command line invocation of kubectl curl")
114119
default:
115-
return fmt.Errorf("too many arguments passed in the command line invocation of kubectl curl [URL] [container]")
120+
return usageError("too many arguments passed in the command line invocation of kubectl curl")
116121
}
117122

118123
if strings.Index(query, "://") < 0 {
@@ -299,13 +304,22 @@ func openPortForwarder(ctx context.Context, fwd portForwarderConfig) (*portforwa
299304
return portforward.New(dialer, ports, ctx.Done(), make(chan struct{}), fwd.stdout, fwd.stderr)
300305
}
301306

302-
func printUsage() {
303-
fmt.Printf(`Run curl against kubernetes pods
307+
type usageError string
308+
309+
func (e usageError) Error() string {
310+
return usage(string(e))
311+
}
312+
313+
func usage(msg string) string {
314+
return msg + `
304315
305316
Usage:
306317
kubectl curl [options] URL [container]
318+
`
319+
}
307320

321+
func usageAndOptions(msg string) string {
322+
return usage(msg) + `
308323
Options:
309-
%s
310-
`, usage)
324+
` + options
311325
}

curl/basic.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@ func NewBool(value bool) *Bool {
1010
}
1111

1212
func (b *Bool) Set(value string) error {
13-
v, err := strconv.ParseBool(value)
14-
*b = Bool(v)
15-
return err
13+
if value == "" {
14+
*b = true
15+
return nil
16+
} else {
17+
v, err := strconv.ParseBool(value)
18+
*b = Bool(v)
19+
return err
20+
}
1621
}
1722

1823
func (b Bool) Get() interface{} { return bool(b) }
@@ -167,3 +172,7 @@ func formatInt64(i int64) string {
167172
}
168173
return strconv.FormatInt(i, 10)
169174
}
175+
176+
var (
177+
_ boolFlag = (*Bool)(nil)
178+
)

curl/option.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type Option struct {
2727
}
2828

2929
func (opt *Option) String() string {
30-
if isBoolFlag(opt.Value) {
30+
if IsBoolFlag(opt.Value) {
3131
if on, _ := opt.Value.Get().(bool); on {
3232
return opt.Name
3333
}
@@ -37,8 +37,8 @@ func (opt *Option) String() string {
3737
return ""
3838
}
3939

40-
func isBoolFlag(v Value) bool {
41-
x, _ := v.(interface{ IsBoolFlag() bool })
40+
func IsBoolFlag(v Value) bool {
41+
x, _ := v.(boolFlag)
4242
return x != nil && x.IsBoolFlag()
4343
}
4444

@@ -47,3 +47,7 @@ type Value interface {
4747
flag.Getter
4848
Type() string
4949
}
50+
51+
type boolFlag interface {
52+
IsBoolFlag() bool
53+
}

0 commit comments

Comments
 (0)