forked from LaconicNetwork/kompose
Support read data from stdin
This commit is contained in:
parent
0252213efb
commit
a012fba1c2
@ -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 {
|
||||
|
||||
@ -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)
|
||||
|
||||
}
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
4
script/test/fixtures/stdin/docker-compose.yaml
vendored
Normal file
4
script/test/fixtures/stdin/docker-compose.yaml
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
version: '3'
|
||||
services:
|
||||
backend:
|
||||
image: helloworld
|
||||
45
script/test/fixtures/stdin/output-k8s.json
vendored
Normal file
45
script/test/fixtures/stdin/output-k8s.json
vendored
Normal 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": {}
|
||||
}
|
||||
]
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user