Check args numbers before application start

Add a general args number validator for all client commands.

Signed-off-by: Zhang Wei <zhangwei555@huawei.com>
This commit is contained in:
Zhang Wei 2016-10-28 23:43:10 +08:00
parent f156f73c2a
commit b517076907
16 changed files with 74 additions and 20 deletions

View File

@ -34,6 +34,9 @@ checkpointed.`,
cli.StringSliceFlag{Name: "empty-ns", Usage: "create a namespace, but don't restore its properies"}, cli.StringSliceFlag{Name: "empty-ns", Usage: "create a namespace, but don't restore its properies"},
}, },
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
if err := checkArgs(context, 1, exactArgs); err != nil {
return err
}
container, err := getContainer(context) container, err := getContainer(context)
if err != nil { if err != nil {
return err return err

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"fmt"
"os" "os"
"github.com/urfave/cli" "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 { Action: func(context *cli.Context) error {
if context.NArg() != 1 { if err := checkArgs(context, 1, exactArgs); err != nil {
fmt.Printf("Incorrect Usage.\n\n") return err
cli.ShowCommandHelp(context, "create")
return fmt.Errorf("runc: \"create\" requires exactly one argument")
} }
if err := revisePidFile(context); err != nil { if err := revisePidFile(context); err != nil {
return err return err

View File

@ -45,11 +45,11 @@ status of "ubuntu01" as "stopped" the following will delete resources held for
}, },
}, },
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
hasError := false if err := checkArgs(context, 1, minArgs); err != nil {
if !context.Args().Present() { return err
return fmt.Errorf("runc: \"delete\" requires a minimum of 1 argument")
} }
hasError := false
factory, err := loadFactory(context) factory, err := loadFactory(context)
if err != nil { if err != nil {
return err return err

View File

@ -108,6 +108,9 @@ information is displayed once every 5 seconds.`,
cli.BoolFlag{Name: "stats", Usage: "display the container's stats then exit"}, cli.BoolFlag{Name: "stats", Usage: "display the container's stats then exit"},
}, },
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
if err := checkArgs(context, 1, exactArgs); err != nil {
return err
}
container, err := getContainer(context) container, err := getContainer(context)
if err != nil { if err != nil {
return err return err

View File

@ -86,6 +86,9 @@ following will output a list of processes running in the container:
}, },
}, },
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
if err := checkArgs(context, 2, minArgs); err != nil {
return err
}
if os.Geteuid() != 0 { if os.Geteuid() != 0 {
return fmt.Errorf("runc should be run as root") return fmt.Errorf("runc should be run as root")
} }

View File

@ -69,6 +69,9 @@ signal to the init process of the "ubuntu01" container:
}, },
}, },
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
if err := checkArgs(context, 2, exactArgs); err != nil {
return err
}
container, err := getContainer(context) container, err := getContainer(context)
if err != nil { if err != nil {
return err return err

View File

@ -67,6 +67,9 @@ To list containers created using a non-default value for "--root":
}, },
}, },
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
if err := checkArgs(context, 0, exactArgs); err != nil {
return err
}
s, err := getContainers(context) s, err := getContainers(context)
if err != nil { if err != nil {
return err return err

View File

@ -20,11 +20,10 @@ paused. `,
Use runc list to identiy instances of containers and their current status.`, Use runc list to identiy instances of containers and their current status.`,
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
hasError := false if err := checkArgs(context, 1, minArgs); err != nil {
if !context.Args().Present() { return err
return fmt.Errorf("runc: \"pause\" requires a minimum of 1 argument")
} }
hasError := false
factory, err := loadFactory(context) factory, err := loadFactory(context)
if err != nil { if err != nil {
return err return err
@ -61,11 +60,10 @@ resumed.`,
Use runc list to identiy instances of containers and their current status.`, Use runc list to identiy instances of containers and their current status.`,
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
hasError := false if err := checkArgs(context, 1, minArgs); err != nil {
if !context.Args().Present() { return err
return fmt.Errorf("runc: \"resume\" requires a minimum of 1 argument")
} }
hasError := false
factory, err := loadFactory(context) factory, err := loadFactory(context)
if err != nil { if err != nil {
return err return err

3
ps.go
View File

@ -25,6 +25,9 @@ var psCommand = cli.Command{
}, },
}, },
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
if err := checkArgs(context, 1, minArgs); err != nil {
return err
}
container, err := getContainer(context) container, err := getContainer(context)
if err != nil { if err != nil {
return err return err

View File

@ -83,6 +83,9 @@ using the runc checkpoint command.`,
}, },
}, },
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
if err := checkArgs(context, 1, exactArgs); err != nil {
return err
}
imagePath := context.String("image-path") imagePath := context.String("image-path")
id := context.Args().First() id := context.Args().First()
if id == "" { if id == "" {

3
run.go
View File

@ -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 { Action: func(context *cli.Context) error {
if err := checkArgs(context, 1, exactArgs); err != nil {
return err
}
if err := revisePidFile(context); err != nil { if err := revisePidFile(context); err != nil {
return err return err
} }

View File

@ -65,6 +65,9 @@ container on your host.`,
}, },
}, },
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
if err := checkArgs(context, 0, exactArgs); err != nil {
return err
}
spec := specs.Spec{ spec := specs.Spec{
Version: specs.Version, Version: specs.Version,
Platform: specs.Platform{ Platform: specs.Platform{

View File

@ -18,11 +18,10 @@ are starting. The name you provide for the container instance must be unique on
your host.`, your host.`,
Description: `The start command executes the user defined process in a created container .`, Description: `The start command executes the user defined process in a created container .`,
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
hasError := false if err := checkArgs(context, 1, minArgs); err != nil {
if !context.Args().Present() { return err
return fmt.Errorf("runc: \"start\" requires a minimum of 1 argument")
} }
hasError := false
factory, err := loadFactory(context) factory, err := loadFactory(context)
if err != nil { if err != nil {
return err return err

View File

@ -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 Description: `The state command outputs current state information for the
instance of a container.`, instance of a container.`,
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
if err := checkArgs(context, 1, exactArgs); err != nil {
return err
}
container, err := getContainer(context) container, err := getContainer(context)
if err != nil { if err != nil {
return err return err

View File

@ -109,6 +109,9 @@ other options are ignored.
}, },
}, },
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
if err := checkArgs(context, 1, exactArgs); err != nil {
return err
}
container, err := getContainer(context) container, err := getContainer(context)
if err != nil { if err != nil {
return err return err

View File

@ -10,6 +10,33 @@ import (
"github.com/urfave/cli" "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 // fatal prints the error's details if it is a libcontainer specific error type
// then exits the program with an exit status of 1. // then exits the program with an exit status of 1.
func fatal(err error) { func fatal(err error) {