DEV 1916: Fix output issues in CLI (#114)

* adding testing of output file and write line for /end

* fix regression in latency rounding for board
This commit is contained in:
Rob O'Dwyer 2022-10-20 13:16:52 -07:00 committed by GitHub
parent 5f60ccbba8
commit 639362ef46
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 396 additions and 49 deletions

View file

@ -3,9 +3,7 @@ package commands
import (
"encoding/json"
"fmt"
"os"
log "github.com/spf13/jwalterweatherman"
"io"
"github.com/BattlesnakeOfficial/rules/client"
)
@ -23,37 +21,20 @@ type result struct {
IsDraw bool `json:"isDraw"`
}
func (ge *GameExporter) FlushToFile(filepath string, format string) error {
var formattedOutput []string
var formattingErr error
// TODO: Support more formats
if format == "JSONL" {
formattedOutput, formattingErr = ge.ConvertToJSON()
} else {
log.ERROR.Fatalf("Invalid output format passed: %s", format)
}
if formattingErr != nil {
return formattingErr
}
f, err := os.OpenFile(filepath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
func (ge *GameExporter) FlushToFile(outputFile io.Writer) (int, error) {
formattedOutput, err := ge.ConvertToJSON()
if err != nil {
return err
return 0, err
}
defer f.Close()
for _, line := range formattedOutput {
_, err := f.WriteString(fmt.Sprintf("%s\n", line))
_, err := io.WriteString(outputFile, fmt.Sprintf("%s\n", line))
if err != nil {
return err
return 0, err
}
}
log.DEBUG.Printf("Written %d lines of output to file: %s\n", len(formattedOutput), filepath)
return nil
return len(formattedOutput), nil
}
func (ge *GameExporter) ConvertToJSON() ([]string, error) {