Support read data from stdin

This commit is contained in:
Hang Yan 2018-08-09 22:46:41 +08:00
parent 0252213efb
commit a012fba1c2
6 changed files with 79 additions and 20 deletions

View File

@ -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 {
}
@ -200,17 +199,11 @@ 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)
loadedFile, err := ReadFile(file)
if err != nil {
return "", err
}
}
err = yaml.Unmarshal(loadedFile, &version)
if err != nil {

View File

@ -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)
}

View File

@ -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
}

View File

@ -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

View File

@ -0,0 +1,4 @@
version: '3'
services:
backend:
image: helloworld

View File

@ -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": {}
}
]
}