|
@@ -29,9 +29,13 @@ package main
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
"fmt"
|
|
"fmt"
|
|
|
|
|
+ "io"
|
|
|
"os"
|
|
"os"
|
|
|
|
|
+ "os/exec"
|
|
|
"path/filepath"
|
|
"path/filepath"
|
|
|
|
|
+ "sort"
|
|
|
|
|
|
|
|
|
|
+ "github.com/docker/api/context"
|
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/sirupsen/logrus"
|
|
|
"github.com/urfave/cli"
|
|
"github.com/urfave/cli"
|
|
|
)
|
|
)
|
|
@@ -57,19 +61,67 @@ func main() {
|
|
|
Name: "debug",
|
|
Name: "debug",
|
|
|
Usage: "enable debug output in the logs",
|
|
Usage: "enable debug output in the logs",
|
|
|
},
|
|
},
|
|
|
|
|
+ context.ConfigFlag,
|
|
|
|
|
+ context.ContextFlag,
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ // 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()
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ logrus.Fatal(err)
|
|
|
|
|
+ }
|
|
|
|
|
+ if ctx.Metadata.Type == "Moby" {
|
|
|
|
|
+ shellOutToDefaultEngine()
|
|
|
|
|
+ } else {
|
|
|
|
|
+ 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") {
|
|
|
logrus.SetLevel(logrus.DebugLevel)
|
|
logrus.SetLevel(logrus.DebugLevel)
|
|
|
}
|
|
}
|
|
|
|
|
+ ctx, err := context.GetContext()
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ logrus.Fatal(err)
|
|
|
|
|
+ }
|
|
|
|
|
+ if ctx.Metadata.Type == "Moby" {
|
|
|
|
|
+ shellOutToDefaultEngine()
|
|
|
|
|
+ }
|
|
|
|
|
+ // TODO select backend based on context.Metadata.Type
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
app.Commands = []cli.Command{
|
|
app.Commands = []cli.Command{
|
|
|
contextCommand,
|
|
contextCommand,
|
|
|
exampleCommand,
|
|
exampleCommand,
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ sort.Sort(cli.FlagsByName(app.Flags))
|
|
|
|
|
+ sort.Sort(cli.CommandsByName(app.Commands))
|
|
|
|
|
+
|
|
|
if err := app.Run(os.Args); err != nil {
|
|
if err := app.Run(os.Args); err != nil {
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
os.Exit(1)
|
|
os.Exit(1)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+func shellOutToDefaultEngine() {
|
|
|
|
|
+ cmd := exec.Command("/Applications/Docker.app/Contents/Resources/bin/docker", os.Args[1:]...)
|
|
|
|
|
+ cmd.Stdin = os.Stdin
|
|
|
|
|
+ cmd.Stdout = os.Stdout
|
|
|
|
|
+ cmd.Stderr = os.Stderr
|
|
|
|
|
+ if err := cmd.Run(); err != nil {
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ if exiterr, ok := err.(*exec.ExitError); ok {
|
|
|
|
|
+ os.Exit(exiterr.ExitCode())
|
|
|
|
|
+ }
|
|
|
|
|
+ os.Exit(1)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ os.Exit(0)
|
|
|
|
|
+}
|