Call original docker on moby context

Signed-off-by: Guillaume Lours <guillaume.lours@docker.com>
This commit is contained in:
Djordje Lukic 2020-04-20 18:06:38 +02:00 committed by Guillaume Lours
parent f5aa9638af
commit 4b2c8ae9cf
2 changed files with 31 additions and 30 deletions

View File

@ -29,6 +29,7 @@ package main
import ( import (
"fmt" "fmt"
"io"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
@ -64,24 +65,22 @@ func main() {
context.ContextFlag, context.ContextFlag,
} }
/*cli.HelpPrinter = func(w io.Writer, templ string, data interface{}) { // Make a copy of the default HelpPrinter function
originalHelpPrinter := cli.HelpPrinter
// Change the HelpPrinter function to shell out to the Moby CLI help
// when the current context is pointing to Docker engine
// else we use the copy of the original HelpPrinter
cli.HelpPrinter = func(w io.Writer, templ string, data interface{}) {
ctx, err := context.GetContext() ctx, err := context.GetContext()
if err != nil { if err != nil {
logrus.Fatal(err) logrus.Fatal(err)
} }
fmt.Println(ctx.Metadata.Type)
if ctx.Metadata.Type == "Moby" { if ctx.Metadata.Type == "Moby" {
err := shellOutToDefaultEngine() shellOutToDefaultEngine()
if err != nil {
if exiterr, ok:= err.(*exec.ExitError); ok {
os.Exit(exiterr.ExitCode())
}
os.Exit(1)
}
} else { } else {
fmt.Fprintf(w, templ, data) originalHelpPrinter(w, templ, data)
} }
}*/ }
app.Before = func(clix *cli.Context) error { app.Before = func(clix *cli.Context) error {
if clix.GlobalBool("debug") { if clix.GlobalBool("debug") {
@ -92,14 +91,7 @@ func main() {
logrus.Fatal(err) logrus.Fatal(err)
} }
if ctx.Metadata.Type == "Moby" { if ctx.Metadata.Type == "Moby" {
err := shellOutToDefaultEngine() shellOutToDefaultEngine()
if err != nil {
if exiterr, ok:= err.(*exec.ExitError); ok {
os.Exit(exiterr.ExitCode())
}
os.Exit(1)
}
os.Exit(0)
} }
// TODO select backend based on context.Metadata.Type // TODO select backend based on context.Metadata.Type
return nil return nil
@ -118,14 +110,18 @@ func main() {
} }
} }
func shellOutToDefaultEngine() error { func shellOutToDefaultEngine() {
cmd :=exec.Command(" /Applications/Docker.app/Contents/Resources/bin/docker", os.Args[1:]...) cmd := exec.Command("/Applications/Docker.app/Contents/Resources/bin/docker", os.Args[1:]...)
cmd.Stdin = os.Stdin cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
fmt.Println("Shellout") if err := cmd.Run(); err != nil {
if err:= cmd.Run(); err != nil { if err != nil {
return err if exiterr, ok := err.(*exec.ExitError); ok {
os.Exit(exiterr.ExitCode())
}
os.Exit(1)
}
} }
return cmd.Wait() os.Exit(0)
} }

View File

@ -3,7 +3,7 @@ package context
import ( import (
"path/filepath" "path/filepath"
"github.com/docker/docker/pkg/homedir" "github.com/mitchellh/go-homedir"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -14,16 +14,16 @@ const (
) )
var ( var (
ConfigDir string ConfigDir string
ConfigFlag = cli.StringFlag{ ContextName string
ConfigFlag = cli.StringFlag{
Name: "config", Name: "config",
Usage: "Location of client config files `DIRECTORY`", Usage: "Location of client config files `DIRECTORY`",
EnvVar: "DOCKER_CONFIG", EnvVar: "DOCKER_CONFIG",
Value: filepath.Join(homedir.Get(), configFileDir), Value: filepath.Join(home(), configFileDir),
Destination: &ConfigDir, Destination: &ConfigDir,
} }
ContextName string
ContextFlag = cli.StringFlag{ ContextFlag = cli.StringFlag{
Name: "context, c", Name: "context, c",
Usage: "Name of the context `CONTEXT` to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with \"docker context use\")", Usage: "Name of the context `CONTEXT` to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with \"docker context use\")",
@ -31,3 +31,8 @@ var (
Destination: &ContextName, Destination: &ContextName,
} }
) )
func home() string {
home, _ := homedir.Dir()
return home
}