Skip to content

Commit b517076

Browse files
committed
Check args numbers before application start
Add a general args number validator for all client commands. Signed-off-by: Zhang Wei <[email protected]>
1 parent f156f73 commit b517076

16 files changed

+74
-20
lines changed

checkpoint.go

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ checkpointed.`,
3434
cli.StringSliceFlag{Name: "empty-ns", Usage: "create a namespace, but don't restore its properies"},
3535
},
3636
Action: func(context *cli.Context) error {
37+
if err := checkArgs(context, 1, exactArgs); err != nil {
38+
return err
39+
}
3740
container, err := getContainer(context)
3841
if err != nil {
3942
return err

create.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"fmt"
54
"os"
65

76
"github.com/urfave/cli"
@@ -49,10 +48,8 @@ command(s) that get executed on start, edit the args parameter of the spec. See
4948
},
5049
},
5150
Action: func(context *cli.Context) error {
52-
if context.NArg() != 1 {
53-
fmt.Printf("Incorrect Usage.\n\n")
54-
cli.ShowCommandHelp(context, "create")
55-
return fmt.Errorf("runc: \"create\" requires exactly one argument")
51+
if err := checkArgs(context, 1, exactArgs); err != nil {
52+
return err
5653
}
5754
if err := revisePidFile(context); err != nil {
5855
return err

delete.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ status of "ubuntu01" as "stopped" the following will delete resources held for
4545
},
4646
},
4747
Action: func(context *cli.Context) error {
48-
hasError := false
49-
if !context.Args().Present() {
50-
return fmt.Errorf("runc: \"delete\" requires a minimum of 1 argument")
48+
if err := checkArgs(context, 1, minArgs); err != nil {
49+
return err
5150
}
5251

52+
hasError := false
5353
factory, err := loadFactory(context)
5454
if err != nil {
5555
return err

events.go

+3
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ information is displayed once every 5 seconds.`,
108108
cli.BoolFlag{Name: "stats", Usage: "display the container's stats then exit"},
109109
},
110110
Action: func(context *cli.Context) error {
111+
if err := checkArgs(context, 1, exactArgs); err != nil {
112+
return err
113+
}
111114
container, err := getContainer(context)
112115
if err != nil {
113116
return err

exec.go

+3
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ following will output a list of processes running in the container:
8686
},
8787
},
8888
Action: func(context *cli.Context) error {
89+
if err := checkArgs(context, 2, minArgs); err != nil {
90+
return err
91+
}
8992
if os.Geteuid() != 0 {
9093
return fmt.Errorf("runc should be run as root")
9194
}

kill.go

+3
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ signal to the init process of the "ubuntu01" container:
6969
},
7070
},
7171
Action: func(context *cli.Context) error {
72+
if err := checkArgs(context, 2, exactArgs); err != nil {
73+
return err
74+
}
7275
container, err := getContainer(context)
7376
if err != nil {
7477
return err

list.go

+3
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ To list containers created using a non-default value for "--root":
6767
},
6868
},
6969
Action: func(context *cli.Context) error {
70+
if err := checkArgs(context, 0, exactArgs); err != nil {
71+
return err
72+
}
7073
s, err := getContainers(context)
7174
if err != nil {
7275
return err

pause.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@ paused. `,
2020
2121
Use runc list to identiy instances of containers and their current status.`,
2222
Action: func(context *cli.Context) error {
23-
hasError := false
24-
if !context.Args().Present() {
25-
return fmt.Errorf("runc: \"pause\" requires a minimum of 1 argument")
23+
if err := checkArgs(context, 1, minArgs); err != nil {
24+
return err
2625
}
27-
26+
hasError := false
2827
factory, err := loadFactory(context)
2928
if err != nil {
3029
return err
@@ -61,11 +60,10 @@ resumed.`,
6160
6261
Use runc list to identiy instances of containers and their current status.`,
6362
Action: func(context *cli.Context) error {
64-
hasError := false
65-
if !context.Args().Present() {
66-
return fmt.Errorf("runc: \"resume\" requires a minimum of 1 argument")
63+
if err := checkArgs(context, 1, minArgs); err != nil {
64+
return err
6765
}
68-
66+
hasError := false
6967
factory, err := loadFactory(context)
7068
if err != nil {
7169
return err

ps.go

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ var psCommand = cli.Command{
2525
},
2626
},
2727
Action: func(context *cli.Context) error {
28+
if err := checkArgs(context, 1, minArgs); err != nil {
29+
return err
30+
}
2831
container, err := getContainer(context)
2932
if err != nil {
3033
return err

restore.go

+3
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ using the runc checkpoint command.`,
8383
},
8484
},
8585
Action: func(context *cli.Context) error {
86+
if err := checkArgs(context, 1, exactArgs); err != nil {
87+
return err
88+
}
8689
imagePath := context.String("image-path")
8790
id := context.Args().First()
8891
if id == "" {

run.go

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ command(s) that get executed on start, edit the args parameter of the spec. See
5959
},
6060
},
6161
Action: func(context *cli.Context) error {
62+
if err := checkArgs(context, 1, exactArgs); err != nil {
63+
return err
64+
}
6265
if err := revisePidFile(context); err != nil {
6366
return err
6467
}

spec.go

+3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ container on your host.`,
6565
},
6666
},
6767
Action: func(context *cli.Context) error {
68+
if err := checkArgs(context, 0, exactArgs); err != nil {
69+
return err
70+
}
6871
spec := specs.Spec{
6972
Version: specs.Version,
7073
Platform: specs.Platform{

start.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ are starting. The name you provide for the container instance must be unique on
1818
your host.`,
1919
Description: `The start command executes the user defined process in a created container .`,
2020
Action: func(context *cli.Context) error {
21-
hasError := false
22-
if !context.Args().Present() {
23-
return fmt.Errorf("runc: \"start\" requires a minimum of 1 argument")
21+
if err := checkArgs(context, 1, minArgs); err != nil {
22+
return err
2423
}
25-
24+
hasError := false
2625
factory, err := loadFactory(context)
2726
if err != nil {
2827
return err

state.go

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ Where "<container-id>" is your name for the instance of the container.`,
2020
Description: `The state command outputs current state information for the
2121
instance of a container.`,
2222
Action: func(context *cli.Context) error {
23+
if err := checkArgs(context, 1, exactArgs); err != nil {
24+
return err
25+
}
2326
container, err := getContainer(context)
2427
if err != nil {
2528
return err

update.go

+3
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ other options are ignored.
109109
},
110110
},
111111
Action: func(context *cli.Context) error {
112+
if err := checkArgs(context, 1, exactArgs); err != nil {
113+
return err
114+
}
112115
container, err := getContainer(context)
113116
if err != nil {
114117
return err

utils.go

+27
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,33 @@ import (
1010
"github.com/urfave/cli"
1111
)
1212

13+
const (
14+
exactArgs = iota
15+
minArgs
16+
)
17+
18+
func checkArgs(context *cli.Context, expected, checkType int) error {
19+
var err error
20+
cmdName := context.Command.Name
21+
switch checkType {
22+
case exactArgs:
23+
if context.NArg() != expected {
24+
err = fmt.Errorf("%s: %q requires exactly %d argument(s)", os.Args[0], cmdName, expected)
25+
}
26+
case minArgs:
27+
if context.NArg() < expected {
28+
err = fmt.Errorf("%s: %q requires a minimum of %d argument(s)", os.Args[0], cmdName, expected)
29+
}
30+
}
31+
32+
if err != nil {
33+
fmt.Printf("Incorrect Usage.\n\n")
34+
cli.ShowCommandHelp(context, cmdName)
35+
return err
36+
}
37+
return nil
38+
}
39+
1340
// fatal prints the error's details if it is a libcontainer specific error type
1441
// then exits the program with an exit status of 1.
1542
func fatal(err error) {

0 commit comments

Comments
 (0)