From 90acf5eddc0fbb07c362423287e5d652a19f496b Mon Sep 17 00:00:00 2001 From: Guillaume Tardif Date: Tue, 16 Jun 2020 17:35:21 +0200 Subject: [PATCH] Allow compose up from stdin with `-f -`. Cf https://github.com/docker/desktop-microsoft/issues/15 --- compose/project.go | 12 +++++------ compose/project_test.go | 44 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 compose/project_test.go diff --git a/compose/project.go b/compose/project.go index d3222a08a..c178270a0 100644 --- a/compose/project.go +++ b/compose/project.go @@ -1,7 +1,6 @@ package compose import ( - "errors" "fmt" "io/ioutil" "os" @@ -130,12 +129,13 @@ func parseConfigs(configPaths []string) ([]types.ConfigFile, error) { var b []byte var err error if f == "-" { - return []types.ConfigFile{}, errors.New("reading compose file from stdin is not supported") + b, err = ioutil.ReadAll(os.Stdin) + } else { + if _, err := os.Stat(f); err != nil { + return nil, err + } + b, err = ioutil.ReadFile(f) } - if _, err := os.Stat(f); err != nil { - return nil, err - } - b, err = ioutil.ReadFile(f) if err != nil { return nil, err } diff --git a/compose/project_test.go b/compose/project_test.go new file mode 100644 index 000000000..f56974b8b --- /dev/null +++ b/compose/project_test.go @@ -0,0 +1,44 @@ +package compose + +import ( + "os" + "testing" + + . "github.com/onsi/gomega" + "github.com/stretchr/testify/suite" +) + +type ComposeTest struct { + suite.Suite +} + +func (suite *ComposeTest) TestParseComposeFile() { + files := []string{"../tests/composefiles/aci-demo/aci_demo_port.yaml"} + config, err := parseConfigs(files) + Expect(err).To(BeNil()) + services := config[0].Config["services"].(map[string]interface{}) + Expect(len(services)).To(Equal(3)) +} + +func (suite *ComposeTest) TestParseComposeStdin() { + files := []string{"-"} + f, err := os.Open("../tests/composefiles/aci-demo/aci_demo_port.yaml") + Expect(err).To(BeNil()) + defer func() { + err := f.Close() + Expect(err).To(BeNil()) + }() + oldStdin := os.Stdin + defer func() { os.Stdin = oldStdin }() // Restore original Stdin + + os.Stdin = f + config, err := parseConfigs(files) + Expect(err).To(BeNil()) + services := config[0].Config["services"].(map[string]interface{}) + Expect(len(services)).To(Equal(3)) +} + +func TestComposeProject(t *testing.T) { + RegisterTestingT(t) + suite.Run(t, new(ComposeTest)) +}