Skip to content
This repository was archived by the owner on Apr 3, 2018. It is now read-only.

Commit f9f1bec

Browse files
committed
virtc: Check mandatory command-line arguments.
Ensure that all mandatory arguments are checked early. Note that urfave/cli doesn't actually provide such a feature. The approach taken attempts to avoid duplicated code by checking centrally, rather than peppering checks on podID and containerID in lots of functions. Signed-off-by: James O. D. Hunt <[email protected]>
1 parent 8362442 commit f9f1bec

File tree

1 file changed

+86
-13
lines changed

1 file changed

+86
-13
lines changed

hack/virtc/main.go

+86-13
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,63 @@ func buildPodConfig(context *cli.Context) (vc.PodConfig, error) {
250250
return podConfig, nil
251251
}
252252

253+
// checkRequiredPodArgs checks to ensure the required command-line
254+
// arguments have been specified for the pod sub-command specified by
255+
// the context argument.
256+
func checkRequiredPodArgs(context *cli.Context) error {
257+
if context == nil {
258+
return nil
259+
}
260+
261+
// sub-sub-command name
262+
name := context.Command.Name
263+
264+
switch name {
265+
case "create":
266+
fallthrough
267+
case "list":
268+
fallthrough
269+
case "run":
270+
return nil
271+
}
272+
273+
id := context.String("id")
274+
if id != "" {
275+
return nil
276+
}
277+
278+
return vc.ErrNeedPodID
279+
}
280+
281+
// checkRequiredContainerArgs checks to ensure the required command-line
282+
// arguments have been specified for the container sub-command specified
283+
// by the context argument.
284+
func checkRequiredContainerArgs(context *cli.Context) error {
285+
if context == nil {
286+
return nil
287+
}
288+
289+
// sub-sub-command name
290+
name := context.Command.Name
291+
292+
podID := context.String("pod-id")
293+
if podID == "" {
294+
return vc.ErrNeedPodID
295+
}
296+
297+
rootfs := context.String("rootfs")
298+
if name == "create" && rootfs == "" {
299+
return fmt.Errorf("%s: need rootfs", name)
300+
}
301+
302+
id := context.String("id")
303+
if id == "" {
304+
return vc.ErrNeedContainerID
305+
}
306+
307+
return nil
308+
}
309+
253310
func runPod(context *cli.Context) error {
254311
podConfig, err := buildPodConfig(context)
255312
if err != nil {
@@ -280,6 +337,22 @@ func createPod(context *cli.Context) error {
280337
return nil
281338
}
282339

340+
func checkPodArgs(context *cli.Context, f func(context *cli.Context) error) error {
341+
if err := checkRequiredPodArgs(context); err != nil {
342+
return err
343+
}
344+
345+
return f(context)
346+
}
347+
348+
func checkContainerArgs(context *cli.Context, f func(context *cli.Context) error) error {
349+
if err := checkRequiredContainerArgs(context); err != nil {
350+
return err
351+
}
352+
353+
return f(context)
354+
}
355+
283356
func deletePod(context *cli.Context) error {
284357
_, err := vc.DeletePod(context.String("id"))
285358
if err != nil {
@@ -354,7 +427,7 @@ var runPodCommand = cli.Command{
354427
Usage: "run a pod",
355428
Flags: podConfigFlags,
356429
Action: func(context *cli.Context) error {
357-
return runPod(context)
430+
return checkPodArgs(context, runPod)
358431
},
359432
}
360433

@@ -363,7 +436,7 @@ var createPodCommand = cli.Command{
363436
Usage: "create a pod",
364437
Flags: podConfigFlags,
365438
Action: func(context *cli.Context) error {
366-
return createPod(context)
439+
return checkPodArgs(context, createPod)
367440
},
368441
}
369442

@@ -378,7 +451,7 @@ var deletePodCommand = cli.Command{
378451
},
379452
},
380453
Action: func(context *cli.Context) error {
381-
return deletePod(context)
454+
return checkPodArgs(context, deletePod)
382455
},
383456
}
384457

@@ -393,7 +466,7 @@ var startPodCommand = cli.Command{
393466
},
394467
},
395468
Action: func(context *cli.Context) error {
396-
return startPod(context)
469+
return checkPodArgs(context, startPod)
397470
},
398471
}
399472

@@ -408,15 +481,15 @@ var stopPodCommand = cli.Command{
408481
},
409482
},
410483
Action: func(context *cli.Context) error {
411-
return stopPod(context)
484+
return checkPodArgs(context, stopPod)
412485
},
413486
}
414487

415488
var listPodsCommand = cli.Command{
416489
Name: "list",
417490
Usage: "list all existing pods",
418491
Action: func(context *cli.Context) error {
419-
return listPods(context)
492+
return checkPodArgs(context, listPods)
420493
},
421494
}
422495

@@ -431,7 +504,7 @@ var statusPodCommand = cli.Command{
431504
},
432505
},
433506
Action: func(context *cli.Context) error {
434-
return statusPod(context)
507+
return checkPodArgs(context, statusPod)
435508
},
436509
}
437510

@@ -577,7 +650,7 @@ var createContainerCommand = cli.Command{
577650
},
578651
},
579652
Action: func(context *cli.Context) error {
580-
return createContainer(context)
653+
return checkContainerArgs(context, createContainer)
581654
},
582655
}
583656

@@ -597,7 +670,7 @@ var deleteContainerCommand = cli.Command{
597670
},
598671
},
599672
Action: func(context *cli.Context) error {
600-
return deleteContainer(context)
673+
return checkContainerArgs(context, deleteContainer)
601674
},
602675
}
603676

@@ -617,7 +690,7 @@ var startContainerCommand = cli.Command{
617690
},
618691
},
619692
Action: func(context *cli.Context) error {
620-
return startContainer(context)
693+
return checkContainerArgs(context, startContainer)
621694
},
622695
}
623696

@@ -637,7 +710,7 @@ var stopContainerCommand = cli.Command{
637710
},
638711
},
639712
Action: func(context *cli.Context) error {
640-
return stopContainer(context)
713+
return checkContainerArgs(context, stopContainer)
641714
},
642715
}
643716

@@ -662,7 +735,7 @@ var enterContainerCommand = cli.Command{
662735
},
663736
},
664737
Action: func(context *cli.Context) error {
665-
return enterContainer(context)
738+
return checkContainerArgs(context, enterContainer)
666739
},
667740
}
668741

@@ -682,7 +755,7 @@ var statusContainerCommand = cli.Command{
682755
},
683756
},
684757
Action: func(context *cli.Context) error {
685-
return statusContainer(context)
758+
return checkContainerArgs(context, statusContainer)
686759
},
687760
}
688761

0 commit comments

Comments
 (0)