Browse Source

Call moby if the command is unknown

Will also check if the context is an original docker context
Djordje Lukic 5 years ago
parent
commit
10bc4b93f6
3 changed files with 10 additions and 6 deletions
  1. 8 2
      cli/main.go
  2. 2 2
      context/store/store.go
  3. 0 2
      context/store/store_test.go

+ 8 - 2
cli/main.go

@@ -34,6 +34,7 @@ import (
 	"os"
 	"os/exec"
 	"path/filepath"
+	"strings"
 
 	"github.com/docker/api/cli/cmd"
 	apicontext "github.com/docker/api/context"
@@ -73,8 +74,9 @@ func isContextCommand(cmd *cobra.Command) bool {
 func main() {
 	var opts mainOpts
 	root := &cobra.Command{
-		Use:  "docker",
-		Long: "docker for the 2020s",
+		Use:           "docker",
+		Long:          "docker for the 2020s",
+		SilenceErrors: true,
 		PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
 			if !isContextCommand(cmd) {
 				execMoby(cmd.Context())
@@ -123,6 +125,10 @@ func main() {
 	ctx = store.WithContextStore(ctx, s)
 
 	if err = root.ExecuteContext(ctx); err != nil {
+		if strings.Contains(err.Error(), "unknown command") {
+			execMoby(ctx)
+		}
+		fmt.Println(err)
 		os.Exit(1)
 	}
 }

+ 2 - 2
context/store/store.go

@@ -58,8 +58,8 @@ func ContextStore(ctx context.Context) Store {
 
 // Store
 type Store interface {
-	// Get returns the context with with name, it returns an error if the
-	// context doesn't exist
+	// Get returns the context with name, it returns an error if the  context
+	// doesn't exist
 	Get(name string) (*Metadata, error)
 	// Create creates a new context, it returns an error if a context with the
 	// same name exists already.

+ 0 - 2
context/store/store_test.go

@@ -29,7 +29,6 @@ package store
 
 import (
 	_ "crypto/sha256"
-	"fmt"
 	"io/ioutil"
 	"os"
 	"testing"
@@ -78,7 +77,6 @@ func TestGet(t *testing.T) {
 
 		m, ok := meta.Metadata.(TypeContext)
 		assert.Equal(t, ok, true)
-		fmt.Printf("%#v\n", meta)
 		assert.Equal(t, "description", m.Description)
 		assert.Equal(t, "type", m.Type)
 	})