Only execute moby if the command is not a context command

This commit is contained in:
Djordje Lukic 2020-04-26 22:13:35 +02:00
parent e2c7370a82
commit 32da9e65e8
1 changed files with 20 additions and 2 deletions

View File

@ -60,13 +60,25 @@ func init() {
} }
} }
func isContextCommand(cmd *cobra.Command) bool {
if cmd == nil {
return false
}
if cmd.Name() == "context" {
return true
}
return isContextCommand(cmd.Parent())
}
func main() { func main() {
var opts mainOpts var opts mainOpts
root := &cobra.Command{ root := &cobra.Command{
Use: "docker", Use: "docker",
Long: "docker for the 2020s", Long: "docker for the 2020s",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error { PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if !isContextCommand(cmd) {
execMoby(cmd.Context()) execMoby(cmd.Context())
}
return nil return nil
}, },
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
@ -76,7 +88,9 @@ func main() {
helpFunc := root.HelpFunc() helpFunc := root.HelpFunc()
root.SetHelpFunc(func(cmd *cobra.Command, args []string) { root.SetHelpFunc(func(cmd *cobra.Command, args []string) {
if !isContextCommand(cmd) {
execMoby(cmd.Context()) execMoby(cmd.Context())
}
helpFunc(cmd, args) helpFunc(cmd, args)
}) })
@ -128,7 +142,9 @@ func withCurrentContext(ctx context.Context, opts mainOpts) (context.Context, er
if currentContext == "" { if currentContext == "" {
currentContext = "default" currentContext = "default"
} }
logrus.Debugf("Current context %q", currentContext) logrus.Debugf("Current context %q", currentContext)
return context.WithValue(ctx, currentContextKey{}, currentContext), nil return context.WithValue(ctx, currentContextKey{}, currentContext), nil
} }
@ -146,6 +162,8 @@ func execMoby(ctx context.Context) {
if err != nil { if err != nil {
logrus.Fatal(err) logrus.Fatal(err)
} }
// Only run original docker command if the current context is not
// ours.
_, ok := cc.Metadata.(store.TypeContext) _, ok := cc.Metadata.(store.TypeContext)
if !ok { if !ok {
cmd := exec.Command("docker", os.Args[1:]...) cmd := exec.Command("docker", os.Args[1:]...)