Skip to content

Commit

Permalink
fix: output file for kcl results
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <xpf6677@163.com>
  • Loading branch information
Peefy committed Nov 14, 2023
1 parent f6d5116 commit d37cfe9
Showing 1 changed file with 12 additions and 21 deletions.
33 changes: 12 additions & 21 deletions pkg/options/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package options

import (
"bufio"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -155,28 +154,10 @@ func (o *RunOptions) Validate() error {
return nil
}

// writer returns the writer to use for output.
func (o *RunOptions) writer() (io.Writer, error) {
if o.Output == "" {
return o.Writer, nil
} else {
file, err := os.OpenFile(o.Output, os.O_CREATE|os.O_RDWR, 0744)
if err != nil {
return nil, err
}
return bufio.NewWriter(file), nil
}
}

func (o *RunOptions) writeResult(result *kcl.KCLResultList) error {
if result == nil {
return nil
}
// Get the writer and output the kcl result.
writer, err := o.writer()
if err != nil {
return err
}
var output []byte
if strings.ToLower(o.Format) == Json {
// If we have multiple result, output the JSON array format, else output the single JSON object.
Expand All @@ -189,8 +170,18 @@ func (o *RunOptions) writeResult(result *kcl.KCLResultList) error {
// Both considering the raw YAML format and the YAML stream format that contains the `---` separator.
output = []byte(result.GetRawYamlResult() + "\n")
}
_, err = writer.Write(output)
return err

if o.Output == "" {
o.Writer.Write(output)
} else {
file, err := os.OpenFile(o.Output, os.O_CREATE|os.O_RDWR, 0744)
if err != nil {
return err
}
defer file.Close()
file.Write(output)
}
return nil
}

// CompileOptionFromCli will parse the kcl options from the cli options.
Expand Down

0 comments on commit d37cfe9

Please sign in to comment.