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 ( import (
"fmt" "fmt"
"io/ioutil"
"reflect" "reflect"
"strings" "strings"
yaml "gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"bufio"
"os"
"github.com/docker/libcompose/project" "github.com/docker/libcompose/project"
"github.com/fatih/structs" "github.com/fatih/structs"
@ -34,6 +30,9 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
//
var StdinData []byte
// Compose is docker compose file loader, implements Loader interface // Compose is docker compose file loader, implements Loader interface
type Compose struct { type Compose struct {
} }
@ -182,7 +181,7 @@ func (c *Compose) LoadFile(files []string) (kobject.KomposeObject, error) {
return kobject.KomposeObject{}, err return kobject.KomposeObject{}, err
} }
return komposeObject, nil return komposeObject, nil
// Use docker/cli for 3 // Use docker/cli for 3
case "3", "3.0", "3.1", "3.2", "3.3": case "3", "3.0", "3.1", "3.2", "3.3":
komposeObject, err := parseV3(files) komposeObject, err := parseV3(files)
if err != nil { if err != nil {
@ -200,16 +199,10 @@ func getVersionFromFile(file string) (string, error) {
Version string `json:"version"` // This affects YAML as well Version string `json:"version"` // This affects YAML as well
} }
var version ComposeVersion var version ComposeVersion
var loadedFile []byte loadedFile, err := ReadFile(file)
var err error
if file == "-" { if err != nil {
data := bufio.NewScanner(os.Stdin) return "", err
loadedFile = data.Bytes()
} else {
loadedFile, err = ioutil.ReadFile(file)
if err != nil {
return "", err
}
} }
err = yaml.Unmarshal(loadedFile, &version) err = yaml.Unmarshal(loadedFile, &version)

View File

@ -17,6 +17,7 @@ limitations under the License.
package compose package compose
import ( import (
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -115,3 +116,18 @@ func handleServiceType(ServiceType string) (string, error) {
func normalizeServiceNames(svcName string) string { func normalizeServiceNames(svcName string) string {
return strings.Replace(svcName, "_", "-", -1) 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 package compose
import ( import (
"io/ioutil"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -77,7 +76,7 @@ func parseV3(files []string) (kobject.KomposeObject, error) {
var config *types.Config var config *types.Config
for _, file := range files { for _, file := range files {
// Load and then parse the YAML first! // Load and then parse the YAML first!
loadedFile, err := ioutil.ReadFile(file) loadedFile, err := ReadFile(file)
if err != nil { if err != nil {
return kobject.KomposeObject{}, err 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" convert::expect_success "$cmd" "/tmp/output-os.json"
# Testing stdin feature # Testing stdin feature
cmd="$KOMPOSE_ROOT/kompose convert --stdout -j -f -" cmd="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 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" echo -e "\n"
go test -v github.com/kubernetes/kompose/script/test/cmd 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": {}
}
]
}