Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1353 from ulyssessouza/project-dir
Browse files Browse the repository at this point in the history
Fix --project-directory mix with --workdir
  • Loading branch information
ndeloof authored Mar 1, 2021
2 parents 6f31d8e + 98bb20f commit dd843bd
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
21 changes: 17 additions & 4 deletions cli/cmd/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -32,7 +36,8 @@ type projectOptions struct {
ProjectName string
Profiles []string
ConfigPaths []string
WorkingDir string
WorkDir string
ProjectDir string
EnvFile string
}

Expand All @@ -41,8 +46,9 @@ 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)")
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) {
Expand Down Expand Up @@ -87,7 +93,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))
}

Expand All @@ -99,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"
Expand Down
10 changes: 5 additions & 5 deletions local/e2e/compose/compose_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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"})
Expand All @@ -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")
})
Expand Down
2 changes: 1 addition & 1 deletion local/e2e/compose/compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
2 changes: 1 addition & 1 deletion local/e2e/compose/volumes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit dd843bd

Please sign in to comment.