build: support reading yaml from stdin

Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
This commit is contained in:
Akihiro Suda
2017-04-25 10:26:34 +00:00
parent 75a784f74b
commit d98f23c2ba

View File

@@ -16,11 +16,13 @@ import (
"github.com/linuxkit/linuxkit/src/initrd" "github.com/linuxkit/linuxkit/src/initrd"
) )
const defaultNameForStdin = "moby"
// Process the build arguments and execute build // Process the build arguments and execute build
func build(args []string) { func build(args []string) {
buildCmd := flag.NewFlagSet("build", flag.ExitOnError) buildCmd := flag.NewFlagSet("build", flag.ExitOnError)
buildCmd.Usage = func() { buildCmd.Usage = func() {
fmt.Printf("USAGE: %s build [options] <file>[.yml]\n\n", os.Args[0]) fmt.Printf("USAGE: %s build [options] <file>[.yml] | -\n\n", os.Args[0])
fmt.Printf("Options:\n") fmt.Printf("Options:\n")
buildCmd.PrintDefaults() buildCmd.PrintDefaults()
} }
@@ -37,12 +39,32 @@ func build(args []string) {
buildCmd.Usage() buildCmd.Usage()
os.Exit(1) os.Exit(1)
} }
conf := remArgs[0] name := *buildName
if !(filepath.Ext(conf) == ".yml" || filepath.Ext(conf) == ".yaml") { var config []byte
conf = conf + ".yml" if conf := remArgs[0]; conf == "-" {
var err error
config, err = ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatalf("Cannot read stdin: %v", err)
}
if name == "" {
name = defaultNameForStdin
}
} else {
if !(filepath.Ext(conf) == ".yml" || filepath.Ext(conf) == ".yaml") {
conf = conf + ".yml"
}
var err error
config, err = ioutil.ReadFile(conf)
if err != nil {
log.Fatalf("Cannot open config file: %v", err)
}
if name == "" {
name = strings.TrimSuffix(filepath.Base(conf), filepath.Ext(conf))
}
} }
buildInternal(*buildName, *buildPull, conf) buildInternal(name, *buildPull, config)
} }
func initrdAppend(iw *initrd.Writer, r io.Reader) { func initrdAppend(iw *initrd.Writer, r io.Reader) {
@@ -78,20 +100,7 @@ func enforceContentTrust(fullImageName string, config *TrustConfig) bool {
} }
// Perform the actual build process // Perform the actual build process
func buildInternal(name string, pull bool, conf string) { func buildInternal(name string, pull bool, config []byte) {
if name == "" {
name = filepath.Base(conf)
ext := filepath.Ext(conf)
if ext != "" {
name = name[:len(name)-len(ext)]
}
}
config, err := ioutil.ReadFile(conf)
if err != nil {
log.Fatalf("Cannot open config file: %v", err)
}
m, err := NewConfig(config) m, err := NewConfig(config)
if err != nil { if err != nil {
log.Fatalf("Invalid config: %v", err) log.Fatalf("Invalid config: %v", err)