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
+9 -16
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 {
}
@@ -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)
+16
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)
}
+1 -2
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
}