1
0
mirror of https://github.com/drone/drone-cli.git synced 2024-11-26 06:07:05 +01:00

(fix) fix multi-docs/lists in starlark

This commit is contained in:
TP Honey 2021-09-07 13:04:04 +01:00
parent b8b8a24077
commit bf16f9f9a8

@ -39,7 +39,7 @@ var Command = cli.Command{
Usage: "target file", Usage: "target file",
Value: ".drone.yml", Value: ".drone.yml",
}, },
cli.BoolTFlag{ cli.BoolFlag{
Name: "format", Name: "format",
Usage: "Write output as formatted YAML", Usage: "Write output as formatted YAML",
}, },
@ -193,13 +193,24 @@ func generate(c *cli.Context) error {
switch v := mainVal.(type) { switch v := mainVal.(type) {
case *starlark.List: case *starlark.List:
for i := 0; i < v.Len(); i++ { for i := 0; i < v.Len(); i++ {
tmpBuf := new(bytes.Buffer)
item := v.Index(i) item := v.Index(i)
buf.WriteString("---\n") tmpBuf.WriteString("---\n")
err = writeJSON(buf, item) err = writeJSON(tmpBuf, item)
if err != nil { if err != nil {
return err return err
} }
buf.WriteString("\n") tmpBuf.WriteString("\n")
if c.Bool("format") {
yml, yErr := yaml.JSONToYAML(tmpBuf.Bytes())
if yErr != nil {
return fmt.Errorf("failed to convert to YAML: %v", yErr)
}
tmpBuf.Reset()
tmpBuf.WriteString("---\n")
tmpBuf.Write(yml)
}
buf.Write(tmpBuf.Bytes())
} }
case *starlark.Dict: case *starlark.Dict:
buf.WriteString("---\n") buf.WriteString("---\n")
@ -207,27 +218,18 @@ func generate(c *cli.Context) error {
if err != nil { if err != nil {
return err return err
} }
if c.BoolT("format") {
yml, yErr := yaml.JSONToYAML(buf.Bytes())
if yErr != nil {
return fmt.Errorf("failed to convert to YAML: %v", yErr)
}
buf.Reset()
buf.Write(yml)
}
default: default:
return fmt.Errorf("invalid return type (got a %s)", mainVal.Type()) return fmt.Errorf("invalid return type (got a %s)", mainVal.Type())
} }
// the user can optionally write the to stdout. This is useful for debugging purposes without mutating an existing file.
// if the user disables pretty printing, the yaml is printed to the console or written to the file in json format.
if c.BoolT("format") == false {
if c.Bool("stdout") {
io.Copy(os.Stdout, buf)
return nil
}
return ioutil.WriteFile(target, buf.Bytes(), 0644)
}
yml, yErr := yaml.JSONToYAML(buf.Bytes())
if yErr != nil {
return fmt.Errorf("failed to convert to YAML: %v", yErr)
}
buf.Reset()
buf.Write(yml)
// the user can optionally write the yaml to stdout. This is useful for debugging purposes without mutating an existing file.
if c.Bool("stdout") { if c.Bool("stdout") {
io.Copy(os.Stdout, buf) io.Copy(os.Stdout, buf)
return nil return nil