Skip to content

Commit

Permalink
Refactor Reduce to also output extra text
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Apr 22, 2022
1 parent e05e917 commit 36af650
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions pkg/reducer/reduce.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,31 +47,31 @@ func Reduce(object interface{}, args []string, theme Theme) {
panic("unknown lang")
}

var stdout, stderr bytes.Buffer
cmd.Stdin = strings.NewReader(Stringify(object))
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
output, err := cmd.CombinedOutput()
if err == nil {
dec := json.NewDecoder(&stdout)
dec := json.NewDecoder(bytes.NewReader(output))
dec.UseNumber()
jsonObject, err := Parse(dec)
if err == nil {
if str, ok := jsonObject.(string); ok {
fmt.Println(str)
} else {
fmt.Println(PrettyPrint(jsonObject, 1, theme))
}
if err != nil {
fmt.Print(string(output))
return
}
if str, ok := jsonObject.(string); ok {
fmt.Println(str)
} else {
_, _ = fmt.Fprint(os.Stderr, stderr.String())
fmt.Println(PrettyPrint(jsonObject, 1, theme))
}
if dec.InputOffset() < int64(len(output)) {
fmt.Print(string(output[dec.InputOffset():]))
}
} else {
exitCode := 1
status, ok := err.(*exec.ExitError)
if ok {
exitCode = status.ExitCode()
}
_, _ = fmt.Fprint(os.Stderr, stderr.String())
fmt.Print(string(output))
os.Exit(exitCode)
}
}
Expand Down

0 comments on commit 36af650

Please sign in to comment.