osbuild-pipeline: read the blueprint from stdin

Make the bluprint parameter a bool, and if set, then read a
blueprint from stdin, otherwise an empty blueprint is used.

Signed-off-by: Tom Gundersen <teg@jklm.no>
This commit is contained in:
Tom Gundersen 2020-03-12 21:04:40 +01:00 committed by jkozol
parent 40040f2cc8
commit 333a1f3500

View file

@ -4,6 +4,7 @@ import (
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"path"
@ -28,12 +29,14 @@ func main() {
var distroArg string
var rpmmdArg bool
flag.StringVar(&imageType, "image-type", "", "image type, e.g. qcow2 or ami")
flag.StringVar(&blueprintArg, "blueprint", "", "path to a JSON file containing a blueprint to translate")
flag.StringVar(&archArg, "arch", "", "architecture to create image for, e.g. x86_64")
flag.StringVar(&distroArg, "distro", "", "distribution to create, e.g. fedora-30")
flag.BoolVar(&rpmmdArg, "rpmmd", false, "output rpmmd struct instead of pipeline manifest")
flag.Parse()
// Path to blueprint or '-' for stdin
blueprintArg = flag.Arg(0)
// Print help usage if one of the required arguments wasn't provided
if imageType == "" || archArg == "" || distroArg == "" {
flag.Usage()
@ -51,11 +54,21 @@ func main() {
blueprint := &blueprint.Blueprint{}
if blueprintArg != "" {
file, err := ioutil.ReadFile(blueprintArg)
if err != nil {
panic("Could not find blueprint: " + err.Error())
var reader io.Reader
if blueprintArg == "-" {
reader = os.Stdin
} else {
var err error
reader, err = os.Open(blueprintArg)
if err != nil {
panic("Could not open bluerpint: " + err.Error())
}
}
err = json.Unmarshal([]byte(file), &blueprint)
file, err := ioutil.ReadAll(reader)
if err != nil {
panic("Could not read blueprint: " + err.Error())
}
err = json.Unmarshal(file, &blueprint)
if err != nil {
panic("Could not parse blueprint: " + err.Error())
}