@@ -32,11 +32,11 @@ import (
32
32
var (
33
33
curlOptions = curl .NewOptionSet ()
34
34
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
40
40
)
41
41
42
42
func init () {
@@ -68,12 +68,15 @@ func init() {
68
68
short = ""
69
69
}
70
70
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
+ }
72
75
}
73
76
74
77
config = genericclioptions .NewConfigFlags (false )
75
78
config .AddFlags (flags )
76
- usage = flags .FlagUsages ()
79
+ options = flags .FlagUsages ()
77
80
}
78
81
79
82
func main () {
@@ -90,7 +93,7 @@ func run(ctx context.Context) error {
90
93
flags .Parse (os .Args [1 :])
91
94
92
95
if help {
93
- printUsage ( )
96
+ fmt . Print ( usageAndOptions ( "Run curl against kubernetes pods" ) )
94
97
return nil
95
98
}
96
99
@@ -111,8 +114,10 @@ func run(ctx context.Context) error {
111
114
query , containerName = args [0 ], args [1 ]
112
115
case 1 :
113
116
query = args [0 ]
117
+ case 0 :
118
+ return usageError ("not enough arguments passed in the command line invocation of kubectl curl" )
114
119
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" )
116
121
}
117
122
118
123
if strings .Index (query , "://" ) < 0 {
@@ -299,13 +304,22 @@ func openPortForwarder(ctx context.Context, fwd portForwarderConfig) (*portforwa
299
304
return portforward .New (dialer , ports , ctx .Done (), make (chan struct {}), fwd .stdout , fwd .stderr )
300
305
}
301
306
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 + `
304
315
305
316
Usage:
306
317
kubectl curl [options] URL [container]
318
+ `
319
+ }
307
320
321
+ func usageAndOptions (msg string ) string {
322
+ return usage (msg ) + `
308
323
Options:
309
- %s
310
- ` , usage )
324
+ ` + options
311
325
}
0 commit comments