浏览代码

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 <[email protected]>
Ulysses Souza 4 年之前
父节点
当前提交
98bb20f2d1
共有 1 个文件被更改,包括 14 次插入0 次删除
  1. 14 0
      cli/cmd/compose/compose.go

+ 14 - 0
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"