Browse code

Add support for multiple level CLI commands

E.g. "docker groups create" will attempt to call the function
CmdGroupsCreate

Signed-off-by: Ben Firshman <ben@firshman.co.uk>

Ben Firshman authored on 2014/09/17 02:44:15
Showing 2 changed files
... ...
@@ -35,11 +35,15 @@ var funcMap = template.FuncMap{
35 35
 	},
36 36
 }
37 37
 
38
-func (cli *DockerCli) getMethod(name string) (func(...string) error, bool) {
39
-	if len(name) == 0 {
40
-		return nil, false
38
+func (cli *DockerCli) getMethod(args ...string) (func(...string) error, bool) {
39
+	camelArgs := make([]string, len(args))
40
+	for i, s := range args {
41
+		if len(s) == 0 {
42
+			return nil, false
43
+		}
44
+		camelArgs[i] = strings.ToUpper(s[:1]) + strings.ToLower(s[1:])
41 45
 	}
42
-	methodName := "Cmd" + strings.ToUpper(name[:1]) + strings.ToLower(name[1:])
46
+	methodName := "Cmd" + strings.Join(camelArgs, "")
43 47
 	method := reflect.ValueOf(cli).MethodByName(methodName)
44 48
 	if !method.IsValid() {
45 49
 		return nil, false
... ...
@@ -49,6 +53,12 @@ func (cli *DockerCli) getMethod(name string) (func(...string) error, bool) {
49 49
 
50 50
 // Cmd executes the specified command
51 51
 func (cli *DockerCli) Cmd(args ...string) error {
52
+	if len(args) > 1 {
53
+		method, exists := cli.getMethod(args[:2]...)
54
+		if exists {
55
+			return method(args[2:]...)
56
+		}
57
+	}
52 58
 	if len(args) > 0 {
53 59
 		method, exists := cli.getMethod(args[0])
54 60
 		if !exists {
... ...
@@ -46,6 +46,13 @@ const (
46 46
 )
47 47
 
48 48
 func (cli *DockerCli) CmdHelp(args ...string) error {
49
+	if len(args) > 1 {
50
+		method, exists := cli.getMethod(args[:2]...)
51
+		if exists {
52
+			method("--help")
53
+			return nil
54
+		}
55
+	}
49 56
 	if len(args) > 0 {
50 57
 		method, exists := cli.getMethod(args[0])
51 58
 		if !exists {