浏览代码

Merge pull request #226 from docker/compose_stdin

Allow compose up from stdin with `-f -`.
Djordje Lukic 5 年之前
父节点
当前提交
228097b1be
共有 2 个文件被更改,包括 47 次插入6 次删除
  1. 3 6
      compose/project.go
  2. 44 0
      compose/project_test.go

+ 3 - 6
compose/project.go

@@ -1,7 +1,6 @@
 package compose
 
 import (
-	"errors"
 	"fmt"
 	"io/ioutil"
 	"os"
@@ -130,12 +129,10 @@ 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 {
+			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
 		}

+ 44 - 0
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))
+}