mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
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>
This commit is contained in:
parent
e91de29e08
commit
e1b968f198
2 changed files with 21 additions and 4 deletions
|
@ -35,11 +35,15 @@ var funcMap = template.FuncMap{
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *DockerCli) getMethod(name string) (func(...string) error, bool) {
|
func (cli *DockerCli) getMethod(args ...string) (func(...string) error, bool) {
|
||||||
if len(name) == 0 {
|
camelArgs := make([]string, len(args))
|
||||||
return nil, false
|
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)
|
method := reflect.ValueOf(cli).MethodByName(methodName)
|
||||||
if !method.IsValid() {
|
if !method.IsValid() {
|
||||||
return nil, false
|
return nil, false
|
||||||
|
@ -49,6 +53,12 @@ func (cli *DockerCli) getMethod(name string) (func(...string) error, bool) {
|
||||||
|
|
||||||
// Cmd executes the specified command
|
// Cmd executes the specified command
|
||||||
func (cli *DockerCli) Cmd(args ...string) error {
|
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 {
|
if len(args) > 0 {
|
||||||
method, exists := cli.getMethod(args[0])
|
method, exists := cli.getMethod(args[0])
|
||||||
if !exists {
|
if !exists {
|
||||||
|
|
|
@ -46,6 +46,13 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
func (cli *DockerCli) CmdHelp(args ...string) error {
|
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 {
|
if len(args) > 0 {
|
||||||
method, exists := cli.getMethod(args[0])
|
method, exists := cli.getMethod(args[0])
|
||||||
if !exists {
|
if !exists {
|
||||||
|
|
Loading…
Add table
Reference in a new issue