diff --git a/pkg/loader/compose/compose.go b/pkg/loader/compose/compose.go index 2dabee42..9186f376 100644 --- a/pkg/loader/compose/compose.go +++ b/pkg/loader/compose/compose.go @@ -18,14 +18,10 @@ package compose import ( "fmt" - "io/ioutil" "reflect" "strings" - yaml "gopkg.in/yaml.v2" - - "bufio" - "os" + "gopkg.in/yaml.v2" "github.com/docker/libcompose/project" "github.com/fatih/structs" @@ -34,6 +30,9 @@ import ( log "github.com/sirupsen/logrus" ) +// +var StdinData []byte + // Compose is docker compose file loader, implements Loader interface type Compose struct { } @@ -182,7 +181,7 @@ func (c *Compose) LoadFile(files []string) (kobject.KomposeObject, error) { return kobject.KomposeObject{}, err } return komposeObject, nil - // Use docker/cli for 3 + // Use docker/cli for 3 case "3", "3.0", "3.1", "3.2", "3.3": komposeObject, err := parseV3(files) if err != nil { @@ -200,16 +199,10 @@ func getVersionFromFile(file string) (string, error) { Version string `json:"version"` // This affects YAML as well } var version ComposeVersion - var loadedFile []byte - var err error - if file == "-" { - data := bufio.NewScanner(os.Stdin) - loadedFile = data.Bytes() - } else { - loadedFile, err = ioutil.ReadFile(file) - if err != nil { - return "", err - } + loadedFile, err := ReadFile(file) + + if err != nil { + return "", err } err = yaml.Unmarshal(loadedFile, &version) diff --git a/pkg/loader/compose/utils.go b/pkg/loader/compose/utils.go index d67732e1..e3ead747 100644 --- a/pkg/loader/compose/utils.go +++ b/pkg/loader/compose/utils.go @@ -17,6 +17,7 @@ limitations under the License. package compose import ( + "io/ioutil" "os" "path/filepath" "strings" @@ -115,3 +116,18 @@ func handleServiceType(ServiceType string) (string, error) { func normalizeServiceNames(svcName string) string { return strings.Replace(svcName, "_", "-", -1) } + +// ReadFile read data from file or stdin +func ReadFile(fileName string) ([]byte, error) { + if fileName == "-" { + if StdinData == nil { + data, err := ioutil.ReadAll(os.Stdin) + StdinData = data + return data, err + } else { + return StdinData, nil + } + } + return ioutil.ReadFile(fileName) + +} diff --git a/pkg/loader/compose/v3.go b/pkg/loader/compose/v3.go index 75030c20..27c7a22f 100644 --- a/pkg/loader/compose/v3.go +++ b/pkg/loader/compose/v3.go @@ -17,7 +17,6 @@ limitations under the License. package compose import ( - "io/ioutil" "strconv" "strings" "time" @@ -77,7 +76,7 @@ func parseV3(files []string) (kobject.KomposeObject, error) { var config *types.Config for _, file := range files { // Load and then parse the YAML first! - loadedFile, err := ioutil.ReadFile(file) + loadedFile, err := ReadFile(file) if err != nil { return kobject.KomposeObject{}, err } diff --git a/script/test/cmd/tests.sh b/script/test/cmd/tests.sh index 8081268d..193cda37 100755 --- a/script/test/cmd/tests.sh +++ b/script/test/cmd/tests.sh @@ -805,8 +805,10 @@ sed -e "s;%VERSION%;$version;g" -e "s;%CMD%;$cmd;g" $KOMPOSE_ROOT/script/test/f convert::expect_success "$cmd" "/tmp/output-os.json" # Testing stdin feature -cmd="$KOMPOSE_ROOT/kompose convert --stdout -j -f -" -sed -e "s;%VERSION%;$version;g" -e "s;%CMD%;$cmd;g" $KOMPOSE_ROOT/script/test/fixtures/stdin/output.json > /tmp/output-k8s.json +cmd="kompose convert --stdout -j -f -" +sed -e "s;%VERSION%;$version;g" -e "s;%CMD%;$cmd;g" $KOMPOSE_ROOT/script/test/fixtures/stdin/output-k8s.json > /tmp/output-k8s.json +cat $KOMPOSE_ROOT/script/test/fixtures/stdin/docker-compose.yaml | $cmd | diff /tmp/output-k8s.json - +EXIT_STATUS=$? echo -e "\n" go test -v github.com/kubernetes/kompose/script/test/cmd diff --git a/script/test/fixtures/stdin/docker-compose.yaml b/script/test/fixtures/stdin/docker-compose.yaml new file mode 100644 index 00000000..8ba27bda --- /dev/null +++ b/script/test/fixtures/stdin/docker-compose.yaml @@ -0,0 +1,4 @@ +version: '3' +services: + backend: + image: helloworld diff --git a/script/test/fixtures/stdin/output-k8s.json b/script/test/fixtures/stdin/output-k8s.json new file mode 100644 index 00000000..e67cf971 --- /dev/null +++ b/script/test/fixtures/stdin/output-k8s.json @@ -0,0 +1,45 @@ +{ + "kind": "List", + "apiVersion": "v1", + "metadata": {}, + "items": [ + { + "kind": "Deployment", + "apiVersion": "extensions/v1beta1", + "metadata": { + "name": "backend", + "creationTimestamp": null, + "labels": { + "io.kompose.service": "backend" + }, + "annotations": { + "kompose.cmd": "%CMD%", + "kompose.version": "%VERSION%" + } + }, + "spec": { + "replicas": 1, + "template": { + "metadata": { + "creationTimestamp": null, + "labels": { + "io.kompose.service": "backend" + } + }, + "spec": { + "containers": [ + { + "name": "backend", + "image": "helloworld", + "resources": {} + } + ], + "restartPolicy": "Always" + } + }, + "strategy": {} + }, + "status": {} + } + ] +}