@@ -250,6 +250,63 @@ func buildPodConfig(context *cli.Context) (vc.PodConfig, error) {
250
250
return podConfig , nil
251
251
}
252
252
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
+
253
310
func runPod (context * cli.Context ) error {
254
311
podConfig , err := buildPodConfig (context )
255
312
if err != nil {
@@ -280,6 +337,22 @@ func createPod(context *cli.Context) error {
280
337
return nil
281
338
}
282
339
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
+
283
356
func deletePod (context * cli.Context ) error {
284
357
_ , err := vc .DeletePod (context .String ("id" ))
285
358
if err != nil {
@@ -354,7 +427,7 @@ var runPodCommand = cli.Command{
354
427
Usage : "run a pod" ,
355
428
Flags : podConfigFlags ,
356
429
Action : func (context * cli.Context ) error {
357
- return runPod (context )
430
+ return checkPodArgs (context , runPod )
358
431
},
359
432
}
360
433
@@ -363,7 +436,7 @@ var createPodCommand = cli.Command{
363
436
Usage : "create a pod" ,
364
437
Flags : podConfigFlags ,
365
438
Action : func (context * cli.Context ) error {
366
- return createPod (context )
439
+ return checkPodArgs (context , createPod )
367
440
},
368
441
}
369
442
@@ -378,7 +451,7 @@ var deletePodCommand = cli.Command{
378
451
},
379
452
},
380
453
Action : func (context * cli.Context ) error {
381
- return deletePod (context )
454
+ return checkPodArgs (context , deletePod )
382
455
},
383
456
}
384
457
@@ -393,7 +466,7 @@ var startPodCommand = cli.Command{
393
466
},
394
467
},
395
468
Action : func (context * cli.Context ) error {
396
- return startPod (context )
469
+ return checkPodArgs (context , startPod )
397
470
},
398
471
}
399
472
@@ -408,15 +481,15 @@ var stopPodCommand = cli.Command{
408
481
},
409
482
},
410
483
Action : func (context * cli.Context ) error {
411
- return stopPod (context )
484
+ return checkPodArgs (context , stopPod )
412
485
},
413
486
}
414
487
415
488
var listPodsCommand = cli.Command {
416
489
Name : "list" ,
417
490
Usage : "list all existing pods" ,
418
491
Action : func (context * cli.Context ) error {
419
- return listPods (context )
492
+ return checkPodArgs (context , listPods )
420
493
},
421
494
}
422
495
@@ -431,7 +504,7 @@ var statusPodCommand = cli.Command{
431
504
},
432
505
},
433
506
Action : func (context * cli.Context ) error {
434
- return statusPod (context )
507
+ return checkPodArgs (context , statusPod )
435
508
},
436
509
}
437
510
@@ -577,7 +650,7 @@ var createContainerCommand = cli.Command{
577
650
},
578
651
},
579
652
Action : func (context * cli.Context ) error {
580
- return createContainer (context )
653
+ return checkContainerArgs (context , createContainer )
581
654
},
582
655
}
583
656
@@ -597,7 +670,7 @@ var deleteContainerCommand = cli.Command{
597
670
},
598
671
},
599
672
Action : func (context * cli.Context ) error {
600
- return deleteContainer (context )
673
+ return checkContainerArgs (context , deleteContainer )
601
674
},
602
675
}
603
676
@@ -617,7 +690,7 @@ var startContainerCommand = cli.Command{
617
690
},
618
691
},
619
692
Action : func (context * cli.Context ) error {
620
- return startContainer (context )
693
+ return checkContainerArgs (context , startContainer )
621
694
},
622
695
}
623
696
@@ -637,7 +710,7 @@ var stopContainerCommand = cli.Command{
637
710
},
638
711
},
639
712
Action : func (context * cli.Context ) error {
640
- return stopContainer (context )
713
+ return checkContainerArgs (context , stopContainer )
641
714
},
642
715
}
643
716
@@ -662,7 +735,7 @@ var enterContainerCommand = cli.Command{
662
735
},
663
736
},
664
737
Action : func (context * cli.Context ) error {
665
- return enterContainer (context )
738
+ return checkContainerArgs (context , enterContainer )
666
739
},
667
740
}
668
741
@@ -682,7 +755,7 @@ var statusContainerCommand = cli.Command{
682
755
},
683
756
},
684
757
Action : func (context * cli.Context ) error {
685
- return statusContainer (context )
758
+ return checkContainerArgs (context , statusContainer )
686
759
},
687
760
}
688
761
0 commit comments