From bc234bdcdf43834a5a07c296818f86548e5da5a3 Mon Sep 17 00:00:00 2001 From: Ulysses Souza Date: Thu, 25 Feb 2021 17:37:11 -0300 Subject: [PATCH 1/2] Fix --project-directory mix with --workdir Signed-off-by: Ulysses Souza --- cli/cmd/compose/compose.go | 7 +++---- local/e2e/compose/compose_build_test.go | 10 +++++----- local/e2e/compose/compose_test.go | 2 +- local/e2e/compose/volumes_test.go | 2 +- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/cli/cmd/compose/compose.go b/cli/cmd/compose/compose.go index 7cf9b0c17..ca3921fc9 100644 --- a/cli/cmd/compose/compose.go +++ b/cli/cmd/compose/compose.go @@ -32,7 +32,7 @@ type projectOptions struct { ProjectName string Profiles []string ConfigPaths []string - WorkingDir string + ProjectDir string EnvFile string } @@ -41,8 +41,7 @@ func (o *projectOptions) addProjectFlags(f *pflag.FlagSet) { f.StringVarP(&o.ProjectName, "project-name", "p", "", "Project name") f.StringArrayVarP(&o.ConfigPaths, "file", "f", []string{}, "Compose configuration files") f.StringVar(&o.EnvFile, "env-file", "", "Specify an alternate environment file.") - f.StringVar(&o.WorkingDir, "workdir", "", "Specify an alternate working directory") - // TODO make --project-directory an alias + f.StringVar(&o.ProjectDir, "project-directory", "", "Specify an alternate working directory\n(default: the path of the Compose file)") } func (o *projectOptions) toProjectName() (string, error) { @@ -87,7 +86,7 @@ func (o *projectOptions) toProjectOptions() (*cli.ProjectOptions, error) { cli.WithEnvFile(o.EnvFile), cli.WithDotEnv, cli.WithOsEnv, - cli.WithWorkingDirectory(o.WorkingDir), + cli.WithWorkingDirectory(o.ProjectDir), cli.WithName(o.ProjectName)) } diff --git a/local/e2e/compose/compose_build_test.go b/local/e2e/compose/compose_build_test.go index b587c2ef9..707f7562c 100644 --- a/local/e2e/compose/compose_build_test.go +++ b/local/e2e/compose/compose_build_test.go @@ -36,7 +36,7 @@ func TestLocalComposeBuild(t *testing.T) { c.RunDockerOrExitError("rmi", "build-test_nginx") c.RunDockerOrExitError("rmi", "custom-nginx") - res := c.RunDockerCmd("compose", "--workdir", "fixtures/build-test", "build") + res := c.RunDockerCmd("compose", "--project-directory", "fixtures/build-test", "build") res.Assert(t, icmd.Expected{Out: "COPY static /usr/share/nginx/html"}) c.RunDockerCmd("image", "inspect", "build-test_nginx") @@ -47,9 +47,9 @@ func TestLocalComposeBuild(t *testing.T) { c.RunDockerOrExitError("rmi", "build-test_nginx") c.RunDockerOrExitError("rmi", "custom-nginx") - res := c.RunDockerCmd("compose", "--workdir", "fixtures/build-test", "up", "-d") + res := c.RunDockerCmd("compose", "--project-directory", "fixtures/build-test", "up", "-d") t.Cleanup(func() { - c.RunDockerCmd("compose", "--workdir", "fixtures/build-test", "down") + c.RunDockerCmd("compose", "--project-directory", "fixtures/build-test", "down") }) res.Assert(t, icmd.Expected{Out: "COPY static /usr/share/nginx/html"}) @@ -62,13 +62,13 @@ func TestLocalComposeBuild(t *testing.T) { }) t.Run("no rebuild when up again", func(t *testing.T) { - res := c.RunDockerCmd("compose", "--workdir", "fixtures/build-test", "up", "-d") + res := c.RunDockerCmd("compose", "--project-directory", "fixtures/build-test", "up", "-d") assert.Assert(t, !strings.Contains(res.Stdout(), "COPY static /usr/share/nginx/html"), res.Stdout()) }) t.Run("cleanup build project", func(t *testing.T) { - c.RunDockerCmd("compose", "--workdir", "fixtures/build-test", "down") + c.RunDockerCmd("compose", "--project-directory", "fixtures/build-test", "down") c.RunDockerCmd("rmi", "build-test_nginx") c.RunDockerCmd("rmi", "custom-nginx") }) diff --git a/local/e2e/compose/compose_test.go b/local/e2e/compose/compose_test.go index 37b9355bf..dc8008f2a 100644 --- a/local/e2e/compose/compose_test.go +++ b/local/e2e/compose/compose_test.go @@ -118,7 +118,7 @@ func TestLocalComposeUp(t *testing.T) { func TestComposePull(t *testing.T) { c := NewParallelE2eCLI(t, binDir) - res := c.RunDockerOrExitError("compose", "--workdir", "fixtures/simple-composefile", "pull") + res := c.RunDockerOrExitError("compose", "--project-directory", "fixtures/simple-composefile", "pull") output := res.Combined() assert.Assert(t, strings.Contains(output, "simple Pulled")) diff --git a/local/e2e/compose/volumes_test.go b/local/e2e/compose/volumes_test.go index b129170cb..1078b6a32 100644 --- a/local/e2e/compose/volumes_test.go +++ b/local/e2e/compose/volumes_test.go @@ -37,7 +37,7 @@ func TestLocalComposeVolume(t *testing.T) { c.RunDockerOrExitError("rmi", "compose-e2e-volume_nginx") c.RunDockerOrExitError("volume", "rm", projectName+"_staticVol") c.RunDockerOrExitError("volume", "rm", "myvolume") - c.RunDockerCmd("compose", "--workdir", "fixtures/volume-test", "--project-name", projectName, "up", "-d") + c.RunDockerCmd("compose", "--project-directory", "fixtures/volume-test", "--project-name", projectName, "up", "-d") }) t.Run("access bind mount data", func(t *testing.T) { From 98bb20f2d174851962926a2fc41bd2e95fde444a Mon Sep 17 00:00:00 2001 From: Ulysses Souza Date: Mon, 1 Mar 2021 12:43:00 -0300 Subject: [PATCH 2/2] Keep --workdir at compose level for retro compatibility This also checks for conflicts with --project-directory at `docker compose` command level Signed-off-by: Ulysses Souza --- cli/cmd/compose/compose.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/cli/cmd/compose/compose.go b/cli/cmd/compose/compose.go index ca3921fc9..7f01f8c5e 100644 --- a/cli/cmd/compose/compose.go +++ b/cli/cmd/compose/compose.go @@ -17,8 +17,12 @@ package compose import ( + "fmt" + "github.com/compose-spec/compose-go/cli" "github.com/compose-spec/compose-go/types" + "github.com/morikuni/aec" + "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -32,6 +36,7 @@ type projectOptions struct { ProjectName string Profiles []string ConfigPaths []string + WorkDir string ProjectDir string EnvFile string } @@ -42,6 +47,8 @@ func (o *projectOptions) addProjectFlags(f *pflag.FlagSet) { f.StringArrayVarP(&o.ConfigPaths, "file", "f", []string{}, "Compose configuration files") f.StringVar(&o.EnvFile, "env-file", "", "Specify an alternate environment file.") f.StringVar(&o.ProjectDir, "project-directory", "", "Specify an alternate working directory\n(default: the path of the Compose file)") + f.StringVar(&o.WorkDir, "workdir", "", "DEPRECATED! USE --project-directory INSTEAD.\nSpecify an alternate working directory\n(default: the path of the Compose file)") + _ = f.MarkHidden("workdir") } func (o *projectOptions) toProjectName() (string, error) { @@ -98,6 +105,13 @@ func Command(contextType string) *cobra.Command { Use: "compose", TraverseChildren: true, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + if opts.WorkDir != "" { + if opts.ProjectDir != "" { + return errors.New(aec.Apply(`cannot specify DEPRECATED "--workdir" and "--project-directory". Please use only "--project-directory" instead.`, aec.RedF)) + } + opts.ProjectDir = opts.WorkDir + fmt.Println(aec.Apply(`option "--workdir" is DEPRECATED at root level! Please use "--project-directory" instead.`, aec.RedF)) + } if contextType == store.DefaultContextType || contextType == store.LocalContextType { Warning = "The new 'docker compose' command is currently experimental. " + "To provide feedback or request new features please open issues at https://github.com/docker/compose-cli"