Permalink
Browse files

Check args numbers before application start

Add a general args number validator for all client commands.

Signed-off-by: Zhang Wei <zhangwei555@huawei.com>
  • Loading branch information...
1 parent f156f73 commit b517076907b7c18d7a1f296ec390cf3f275e2375 @WeiZhang555 WeiZhang555 committed Oct 28, 2016
Showing with 74 additions and 20 deletions.
  1. +3 −0 checkpoint.go
  2. +2 −5 create.go
  3. +3 −3 delete.go
  4. +3 −0 events.go
  5. +3 −0 exec.go
  6. +3 −0 kill.go
  7. +3 −0 list.go
  8. +6 −8 pause.go
  9. +3 −0 ps.go
  10. +3 −0 restore.go
  11. +3 −0 run.go
  12. +3 −0 spec.go
  13. +3 −4 start.go
  14. +3 −0 state.go
  15. +3 −0 update.go
  16. +27 −0 utils.go
View
@@ -34,6 +34,9 @@ checkpointed.`,
cli.StringSliceFlag{Name: "empty-ns", Usage: "create a namespace, but don't restore its properies"},
},
Action: func(context *cli.Context) error {
+ if err := checkArgs(context, 1, exactArgs); err != nil {
+ return err
+ }
container, err := getContainer(context)
if err != nil {
return err
View
@@ -1,7 +1,6 @@
package main
import (
- "fmt"
"os"
"github.com/urfave/cli"
@@ -49,10 +48,8 @@ command(s) that get executed on start, edit the args parameter of the spec. See
},
},
Action: func(context *cli.Context) error {
- if context.NArg() != 1 {
- fmt.Printf("Incorrect Usage.\n\n")
- cli.ShowCommandHelp(context, "create")
- return fmt.Errorf("runc: \"create\" requires exactly one argument")
+ if err := checkArgs(context, 1, exactArgs); err != nil {
+ return err
}
if err := revisePidFile(context); err != nil {
return err
View
@@ -45,11 +45,11 @@ status of "ubuntu01" as "stopped" the following will delete resources held for
},
},
Action: func(context *cli.Context) error {
- hasError := false
- if !context.Args().Present() {
- return fmt.Errorf("runc: \"delete\" requires a minimum of 1 argument")
+ if err := checkArgs(context, 1, minArgs); err != nil {
+ return err
}
+ hasError := false
factory, err := loadFactory(context)
if err != nil {
return err
View
@@ -108,6 +108,9 @@ information is displayed once every 5 seconds.`,
cli.BoolFlag{Name: "stats", Usage: "display the container's stats then exit"},
},
Action: func(context *cli.Context) error {
+ if err := checkArgs(context, 1, exactArgs); err != nil {
+ return err
+ }
container, err := getContainer(context)
if err != nil {
return err
View
@@ -86,6 +86,9 @@ following will output a list of processes running in the container:
},
},
Action: func(context *cli.Context) error {
+ if err := checkArgs(context, 2, minArgs); err != nil {
+ return err
+ }
if os.Geteuid() != 0 {
return fmt.Errorf("runc should be run as root")
}
View
@@ -69,6 +69,9 @@ signal to the init process of the "ubuntu01" container:
},
},
Action: func(context *cli.Context) error {
+ if err := checkArgs(context, 2, exactArgs); err != nil {
+ return err
+ }
container, err := getContainer(context)
if err != nil {
return err
View
@@ -67,6 +67,9 @@ To list containers created using a non-default value for "--root":
},
},
Action: func(context *cli.Context) error {
+ if err := checkArgs(context, 0, exactArgs); err != nil {
+ return err
+ }
s, err := getContainers(context)
if err != nil {
return err
View
@@ -20,11 +20,10 @@ paused. `,
Use runc list to identiy instances of containers and their current status.`,
Action: func(context *cli.Context) error {
- hasError := false
- if !context.Args().Present() {
- return fmt.Errorf("runc: \"pause\" requires a minimum of 1 argument")
+ if err := checkArgs(context, 1, minArgs); err != nil {
+ return err
}
-
+ hasError := false
factory, err := loadFactory(context)
if err != nil {
return err
@@ -61,11 +60,10 @@ resumed.`,
Use runc list to identiy instances of containers and their current status.`,
Action: func(context *cli.Context) error {
- hasError := false
- if !context.Args().Present() {
- return fmt.Errorf("runc: \"resume\" requires a minimum of 1 argument")
+ if err := checkArgs(context, 1, minArgs); err != nil {
+ return err
}
-
+ hasError := false
factory, err := loadFactory(context)
if err != nil {
return err
View
@@ -25,6 +25,9 @@ var psCommand = cli.Command{
},
},
Action: func(context *cli.Context) error {
+ if err := checkArgs(context, 1, minArgs); err != nil {
+ return err
+ }
container, err := getContainer(context)
if err != nil {
return err
View
@@ -83,6 +83,9 @@ using the runc checkpoint command.`,
},
},
Action: func(context *cli.Context) error {
+ if err := checkArgs(context, 1, exactArgs); err != nil {
+ return err
+ }
imagePath := context.String("image-path")
id := context.Args().First()
if id == "" {
View
@@ -59,6 +59,9 @@ command(s) that get executed on start, edit the args parameter of the spec. See
},
},
Action: func(context *cli.Context) error {
+ if err := checkArgs(context, 1, exactArgs); err != nil {
+ return err
+ }
if err := revisePidFile(context); err != nil {
return err
}
View
@@ -65,6 +65,9 @@ container on your host.`,
},
},
Action: func(context *cli.Context) error {
+ if err := checkArgs(context, 0, exactArgs); err != nil {
+ return err
+ }
spec := specs.Spec{
Version: specs.Version,
Platform: specs.Platform{
View
@@ -18,11 +18,10 @@ are starting. The name you provide for the container instance must be unique on
your host.`,
Description: `The start command executes the user defined process in a created container .`,
Action: func(context *cli.Context) error {
- hasError := false
- if !context.Args().Present() {
- return fmt.Errorf("runc: \"start\" requires a minimum of 1 argument")
+ if err := checkArgs(context, 1, minArgs); err != nil {
+ return err
}
-
+ hasError := false
factory, err := loadFactory(context)
if err != nil {
return err
View
@@ -20,6 +20,9 @@ Where "<container-id>" is your name for the instance of the container.`,
Description: `The state command outputs current state information for the
instance of a container.`,
Action: func(context *cli.Context) error {
+ if err := checkArgs(context, 1, exactArgs); err != nil {
+ return err
+ }
container, err := getContainer(context)
if err != nil {
return err
View
@@ -109,6 +109,9 @@ other options are ignored.
},
},
Action: func(context *cli.Context) error {
+ if err := checkArgs(context, 1, exactArgs); err != nil {
+ return err
+ }
container, err := getContainer(context)
if err != nil {
return err
View
@@ -10,6 +10,33 @@ import (
"github.com/urfave/cli"
)
+const (
+ exactArgs = iota
+ minArgs
+)
+
+func checkArgs(context *cli.Context, expected, checkType int) error {
+ var err error
+ cmdName := context.Command.Name
+ switch checkType {
+ case exactArgs:
+ if context.NArg() != expected {
+ err = fmt.Errorf("%s: %q requires exactly %d argument(s)", os.Args[0], cmdName, expected)
+ }
+ case minArgs:
+ if context.NArg() < expected {
+ err = fmt.Errorf("%s: %q requires a minimum of %d argument(s)", os.Args[0], cmdName, expected)
+ }
+ }
+
+ if err != nil {
+ fmt.Printf("Incorrect Usage.\n\n")
+ cli.ShowCommandHelp(context, cmdName)
+ return err
+ }
+ return nil
+}
+
// fatal prints the error's details if it is a libcontainer specific error type
// then exits the program with an exit status of 1.
func fatal(err error) {

0 comments on commit b517076

Please sign in to comment.