From cad32e0111cec6088cf26cbb122e816fc0a3f6c8 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Wed, 12 Apr 2017 12:42:35 -0400 Subject: [PATCH] Set Composefile WorkingDir to dirname of the composefile. Signed-off-by: Daniel Nephin --- cli/command/stack/deploy_composefile.go | 11 ++++---- cli/command/stack/deploy_composefile_test.go | 28 ++++++++++++++++++++ cli/compose/loader/loader.go | 5 ---- 3 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 cli/command/stack/deploy_composefile_test.go diff --git a/cli/command/stack/deploy_composefile.go b/cli/command/stack/deploy_composefile.go index 10963d1844..700a65dce4 100644 --- a/cli/command/stack/deploy_composefile.go +++ b/cli/command/stack/deploy_composefile.go @@ -4,6 +4,7 @@ import ( "fmt" "io/ioutil" "os" + "path/filepath" "sort" "strings" @@ -20,7 +21,7 @@ import ( ) func deployCompose(ctx context.Context, dockerCli *command.DockerCli, opts deployOptions) error { - configDetails, err := getConfigDetails(opts) + configDetails, err := getConfigDetails(opts.composefile) if err != nil { return err } @@ -108,16 +109,16 @@ func propertyWarnings(properties map[string]string) string { return strings.Join(msgs, "\n\n") } -func getConfigDetails(opts deployOptions) (composetypes.ConfigDetails, error) { +func getConfigDetails(composefile string) (composetypes.ConfigDetails, error) { var details composetypes.ConfigDetails - var err error - details.WorkingDir, err = os.Getwd() + absPath, err := filepath.Abs(composefile) if err != nil { return details, err } + details.WorkingDir = filepath.Dir(absPath) - configFile, err := getConfigFile(opts.composefile) + configFile, err := getConfigFile(composefile) if err != nil { return details, err } diff --git a/cli/command/stack/deploy_composefile_test.go b/cli/command/stack/deploy_composefile_test.go new file mode 100644 index 0000000000..d5ef5463ff --- /dev/null +++ b/cli/command/stack/deploy_composefile_test.go @@ -0,0 +1,28 @@ +package stack + +import ( + "os" + "path/filepath" + "testing" + + "github.com/docker/docker/pkg/testutil/tempfile" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestGetConfigDetails(t *testing.T) { + content := ` +version: "3.0" +services: + foo: + image: alpine:3.5 +` + file := tempfile.NewTempFile(t, "test-get-config-details", content) + defer file.Remove() + + details, err := getConfigDetails(file.Name()) + require.NoError(t, err) + assert.Equal(t, filepath.Dir(file.Name()), details.WorkingDir) + assert.Len(t, details.ConfigFiles, 1) + assert.Len(t, details.Environment, len(os.Environ())) +} diff --git a/cli/compose/loader/loader.go b/cli/compose/loader/loader.go index 2394ff8e2f..0578cf34f3 100644 --- a/cli/compose/loader/loader.go +++ b/cli/compose/loader/loader.go @@ -4,7 +4,6 @@ import ( "fmt" "path" "reflect" - "regexp" "sort" "strings" @@ -23,10 +22,6 @@ import ( yaml "gopkg.in/yaml.v2" ) -var ( - fieldNameRegexp = regexp.MustCompile("[A-Z][a-z0-9]+") -) - // ParseYAML reads the bytes from a file, parses the bytes into a mapping // structure, and returns it. func ParseYAML(source []byte) (map[string]interface{}, error) {