Add convert sub-command

This commit is contained in:
Thomas Boerger 2019-01-28 16:32:24 +01:00
parent b6add0c2d9
commit 6a1167dbb3
No known key found for this signature in database
GPG Key ID: 09745AFF9D63C79B

27
main.go
View File

@ -15,6 +15,7 @@ import (
"github.com/drone/drone-yaml/yaml"
"github.com/drone/drone-yaml/yaml/compiler"
"github.com/drone/drone-yaml/yaml/compiler/transform"
"github.com/drone/drone-yaml/yaml/converter"
"github.com/drone/drone-yaml/yaml/linter"
"github.com/drone/drone-yaml/yaml/pretty"
"github.com/drone/drone-yaml/yaml/signer"
@ -28,6 +29,10 @@ var (
formatSave = format.Flag("save", "save result to source").Short('s').Bool()
formatFile = format.Arg("source", "source file location").Default(".drone.yml").File()
convert = kingpin.Command("convert", "convert the yaml file")
convertSave = convert.Flag("save", "save result to source").Short('s').Bool()
convertFile = convert.Arg("source", "source file location").Default(".drone.yml").File()
lint = kingpin.Command("lint", "lint the yaml file")
lintPriv = lint.Flag("privileged", "privileged mode").Short('p').Bool()
lintFile = lint.Arg("source", "source file location").Default(".drone.yml").File()
@ -50,6 +55,8 @@ func main() {
switch kingpin.Parse() {
case format.FullCommand():
kingpin.FatalIfError(runFormat(), "")
case convert.FullCommand():
kingpin.FatalIfError(runConvert(), "")
case lint.FullCommand():
kingpin.FatalIfError(runLint(), "")
case sign.FullCommand():
@ -78,6 +85,26 @@ func runFormat() error {
return err
}
func runConvert() error {
f := *convertFile
d, err := ioutil.ReadAll(f)
if err != nil {
return err
}
m := converter.Metadata{
Filename: f.Name(),
}
b, err := converter.Convert(d, m)
if err != nil {
return err
}
if *formatSave {
return ioutil.WriteFile(f.Name(), b, 0644)
}
_, err = io.Copy(os.Stderr, bytes.NewReader(b))
return err
}
func runLint() error {
f := *lintFile
m, err := yaml.Parse(f)