Bläddra i källkod

Merge pull request #401 from docker/compose_cmd

Error out  for contexts not implementing compose command
Anca Iordache 5 år sedan
förälder
incheckning
3bad2d4004
4 ändrade filer med 36 tillägg och 3 borttagningar
  1. 21 0
      cli/cmd/compose/compose.go
  2. 3 3
      cli/main.go
  3. 3 0
      context/store/store.go
  4. 9 0
      tests/e2e/e2e_test.go

+ 21 - 0
cli/cmd/compose/compose.go

@@ -17,7 +17,12 @@
 package compose
 
 import (
+	"github.com/pkg/errors"
 	"github.com/spf13/cobra"
+
+	apicontext "github.com/docker/api/context"
+	"github.com/docker/api/context/store"
+	"github.com/docker/api/errdefs"
 )
 
 // Command returns the compose command with its child commands
@@ -25,6 +30,22 @@ func Command() *cobra.Command {
 	command := &cobra.Command{
 		Short: "Docker Compose",
 		Use:   "compose",
+		PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
+			currentContext := apicontext.CurrentContext(cmd.Context())
+			s := store.ContextStore(cmd.Context())
+			cc, err := s.Get(currentContext)
+			if err != nil {
+				return err
+			}
+			switch cc.Type() {
+			case store.AciContextType:
+				return nil
+			case store.AwsContextType:
+				return errors.New("use 'docker ecs compose' on context type " + cc.Type())
+			default:
+				return errors.Wrapf(errdefs.ErrNotImplemented, "compose command not supported on context type %q", cc.Type())
+			}
+		},
 	}
 
 	command.AddCommand(

+ 3 - 3
cli/main.go

@@ -59,6 +59,7 @@ var (
 
 var (
 	ownCommands = map[string]struct{}{
+		"compose": {},
 		"context": {},
 		"login":   {},
 		"logout":  {},
@@ -181,7 +182,6 @@ func main() {
 		if errors.Is(ctx.Err(), context.Canceled) {
 			os.Exit(130)
 		}
-
 		// Context should always be handled by new CLI
 		requiredCmd, _, _ := root.Find(os.Args[1:])
 		if requiredCmd != nil && isOwnCommand(requiredCmd) {
@@ -196,7 +196,7 @@ func main() {
 
 func exit(err error) {
 	if errors.Is(err, errdefs.ErrLoginRequired) {
-		fmt.Fprintln(os.Stderr, fmt.Errorf("%v", err))
+		fmt.Fprintln(os.Stderr, err)
 		os.Exit(errdefs.ExitCodeLoginRequired)
 	}
 	fatal(err)
@@ -242,6 +242,6 @@ func determineCurrentContext(flag string, configDir string) string {
 }
 
 func fatal(err error) {
-	fmt.Fprint(os.Stderr, err)
+	fmt.Fprintln(os.Stderr, err)
 	os.Exit(1)
 }

+ 3 - 0
context/store/store.go

@@ -140,6 +140,9 @@ func New(opts ...Opt) (Store, error) {
 
 // Get returns the context with the given name
 func (s *store) Get(name string) (*DockerContext, error) {
+	if name == "default" {
+		return dockerDefaultContext()
+	}
 	meta := filepath.Join(s.root, contextsDir, metadataDir, contextDirOf(name), metaFile)
 	m, err := read(meta)
 	if os.IsNotExist(err) {

+ 9 - 0
tests/e2e/e2e_test.go

@@ -86,6 +86,15 @@ func (s *E2eSuite) TestContextLsFormat() {
 	Expect(output).To(ContainSubstring(`"Name":"default"`))
 }
 
+func (s *E2eSuite) TestComposeOnDefaultContext() {
+	s.NewDockerCommand("context", "use", "default").ExecOrDie()
+	output := s.NewDockerCommand("context", "inspect").ExecOrDie()
+	Expect(output).To(ContainSubstring(`"Name": "default"`))
+	output, err := s.NewDockerCommand("compose", "up").Exec()
+	Expect(err).NotTo(BeNil())
+	Expect(output).To(ContainSubstring(`compose command not supported on context type`))
+}
+
 func (s *E2eSuite) TestContextCreateParseErrorDoesNotDelegateToLegacy() {
 	s.Step("should dispay new cli error when parsing context create flags", func() {
 		_, err := s.NewDockerCommand("context", "create", "aci", "--subscription-id", "titi").Exec()