Merge pull request #1353 from ulyssessouza/project-dir

Fix --project-directory mix with --workdir
This commit is contained in:
Nicolas De loof 2021-03-01 17:58:47 +01:00 committed by GitHub
commit dd843bded8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 11 deletions

View File

@ -17,8 +17,12 @@
package compose package compose
import ( import (
"fmt"
"github.com/compose-spec/compose-go/cli" "github.com/compose-spec/compose-go/cli"
"github.com/compose-spec/compose-go/types" "github.com/compose-spec/compose-go/types"
"github.com/morikuni/aec"
"github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/pflag" "github.com/spf13/pflag"
@ -32,7 +36,8 @@ type projectOptions struct {
ProjectName string ProjectName string
Profiles []string Profiles []string
ConfigPaths []string ConfigPaths []string
WorkingDir string WorkDir string
ProjectDir string
EnvFile string EnvFile string
} }
@ -41,8 +46,9 @@ func (o *projectOptions) addProjectFlags(f *pflag.FlagSet) {
f.StringVarP(&o.ProjectName, "project-name", "p", "", "Project name") f.StringVarP(&o.ProjectName, "project-name", "p", "", "Project name")
f.StringArrayVarP(&o.ConfigPaths, "file", "f", []string{}, "Compose configuration files") f.StringArrayVarP(&o.ConfigPaths, "file", "f", []string{}, "Compose configuration files")
f.StringVar(&o.EnvFile, "env-file", "", "Specify an alternate environment file.") f.StringVar(&o.EnvFile, "env-file", "", "Specify an alternate environment file.")
f.StringVar(&o.WorkingDir, "workdir", "", "Specify an alternate working directory") f.StringVar(&o.ProjectDir, "project-directory", "", "Specify an alternate working directory\n(default: the path of the Compose file)")
// TODO make --project-directory an alias 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) { func (o *projectOptions) toProjectName() (string, error) {
@ -87,7 +93,7 @@ func (o *projectOptions) toProjectOptions() (*cli.ProjectOptions, error) {
cli.WithEnvFile(o.EnvFile), cli.WithEnvFile(o.EnvFile),
cli.WithDotEnv, cli.WithDotEnv,
cli.WithOsEnv, cli.WithOsEnv,
cli.WithWorkingDirectory(o.WorkingDir), cli.WithWorkingDirectory(o.ProjectDir),
cli.WithName(o.ProjectName)) cli.WithName(o.ProjectName))
} }
@ -99,6 +105,13 @@ func Command(contextType string) *cobra.Command {
Use: "compose", Use: "compose",
TraverseChildren: true, TraverseChildren: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error { 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 { if contextType == store.DefaultContextType || contextType == store.LocalContextType {
Warning = "The new 'docker compose' command is currently experimental. " + 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" "To provide feedback or request new features please open issues at https://github.com/docker/compose-cli"

View File

@ -36,7 +36,7 @@ func TestLocalComposeBuild(t *testing.T) {
c.RunDockerOrExitError("rmi", "build-test_nginx") c.RunDockerOrExitError("rmi", "build-test_nginx")
c.RunDockerOrExitError("rmi", "custom-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"}) res.Assert(t, icmd.Expected{Out: "COPY static /usr/share/nginx/html"})
c.RunDockerCmd("image", "inspect", "build-test_nginx") 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", "build-test_nginx")
c.RunDockerOrExitError("rmi", "custom-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() { 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"}) 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) { 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()) assert.Assert(t, !strings.Contains(res.Stdout(), "COPY static /usr/share/nginx/html"), res.Stdout())
}) })
t.Run("cleanup build project", func(t *testing.T) { 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", "build-test_nginx")
c.RunDockerCmd("rmi", "custom-nginx") c.RunDockerCmd("rmi", "custom-nginx")
}) })

View File

@ -118,7 +118,7 @@ func TestLocalComposeUp(t *testing.T) {
func TestComposePull(t *testing.T) { func TestComposePull(t *testing.T) {
c := NewParallelE2eCLI(t, binDir) 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() output := res.Combined()
assert.Assert(t, strings.Contains(output, "simple Pulled")) assert.Assert(t, strings.Contains(output, "simple Pulled"))

View File

@ -37,7 +37,7 @@ func TestLocalComposeVolume(t *testing.T) {
c.RunDockerOrExitError("rmi", "compose-e2e-volume_nginx") c.RunDockerOrExitError("rmi", "compose-e2e-volume_nginx")
c.RunDockerOrExitError("volume", "rm", projectName+"_staticVol") c.RunDockerOrExitError("volume", "rm", projectName+"_staticVol")
c.RunDockerOrExitError("volume", "rm", "myvolume") 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) { t.Run("access bind mount data", func(t *testing.T) {