From e1b968f1981f21470a67ed617c36f1c6802bd0a7 Mon Sep 17 00:00:00 2001 From: Ben Firshman Date: Tue, 16 Sep 2014 10:44:15 -0700 Subject: [PATCH] Add support for multiple level CLI commands E.g. "docker groups create" will attempt to call the function CmdGroupsCreate Signed-off-by: Ben Firshman --- api/client/cli.go | 18 ++++++++++++++---- api/client/commands.go | 7 +++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/api/client/cli.go b/api/client/cli.go index fa3be2d8d9..6346d30cac 100644 --- a/api/client/cli.go +++ b/api/client/cli.go @@ -35,11 +35,15 @@ var funcMap = template.FuncMap{ }, } -func (cli *DockerCli) getMethod(name string) (func(...string) error, bool) { - if len(name) == 0 { - return nil, false +func (cli *DockerCli) getMethod(args ...string) (func(...string) error, bool) { + camelArgs := make([]string, len(args)) + for i, s := range args { + if len(s) == 0 { + return nil, false + } + camelArgs[i] = strings.ToUpper(s[:1]) + strings.ToLower(s[1:]) } - methodName := "Cmd" + strings.ToUpper(name[:1]) + strings.ToLower(name[1:]) + methodName := "Cmd" + strings.Join(camelArgs, "") method := reflect.ValueOf(cli).MethodByName(methodName) if !method.IsValid() { return nil, false @@ -49,6 +53,12 @@ func (cli *DockerCli) getMethod(name string) (func(...string) error, bool) { // Cmd executes the specified command func (cli *DockerCli) Cmd(args ...string) error { + if len(args) > 1 { + method, exists := cli.getMethod(args[:2]...) + if exists { + return method(args[2:]...) + } + } if len(args) > 0 { method, exists := cli.getMethod(args[0]) if !exists { diff --git a/api/client/commands.go b/api/client/commands.go index a41fdc6d85..081ee49093 100644 --- a/api/client/commands.go +++ b/api/client/commands.go @@ -46,6 +46,13 @@ const ( ) func (cli *DockerCli) CmdHelp(args ...string) error { + if len(args) > 1 { + method, exists := cli.getMethod(args[:2]...) + if exists { + method("--help") + return nil + } + } if len(args) > 0 { method, exists := cli.getMethod(args[0]) if !exists {