diff options
Diffstat (limited to 'cmd/jp/main.go')
-rw-r--r-- | cmd/jp/main.go | 35 |
1 files changed, 30 insertions, 5 deletions
diff --git a/cmd/jp/main.go b/cmd/jp/main.go index 07de252..62f3db0 100644 --- a/cmd/jp/main.go +++ b/cmd/jp/main.go @@ -1,6 +1,7 @@ package main import ( + "encoding/csv" "encoding/json" "flag" "fmt" @@ -21,6 +22,7 @@ type configuration struct { XY string PlotType enumVar CanvasType enumVar + InputType enumVar } const ( @@ -37,6 +39,11 @@ const ( canvasTypeAuto = "auto" ) +const ( + inputTypeCSV = "csv" + inputTypeJSON = "json" +) + var config = configuration{ PlotType: enumVar{ Value: plotTypeLine, @@ -55,6 +62,13 @@ var config = configuration{ canvasTypeAuto, }, }, + InputType: enumVar{ + Value: inputTypeJSON, + Choices: []string{ + inputTypeJSON, + inputTypeCSV, + }, + }, } var ( @@ -66,6 +80,7 @@ var ( func init() { flag.Var(&config.PlotType, "type", fmt.Sprintf("Plot type. One of %v", config.PlotType.Choices)) flag.Var(&config.CanvasType, "canvas", fmt.Sprintf("Canvas type. One of %v", config.CanvasType.Choices)) + flag.Var(&config.InputType, "input", fmt.Sprintf("Input type. One of %v", config.InputType.Choices)) flag.StringVar(&config.X, "x", "", "x values (JSONPath expression)") flag.StringVar(&config.Y, "y", "", "y values (JSONPath expression)") flag.StringVar(&config.XY, "xy", "", "x,y value pairs (JSONPath expression). Overrides -x and -y if given.") @@ -113,12 +128,21 @@ func match(in interface{}, p *jsonpath.JSONPath) [][]reflect.Value { func main() { var in interface{} - dec := json.NewDecoder(os.Stdin) - err := dec.Decode(&in) - if err != nil { - log.Println(err) + switch config.InputType.Value { + case inputTypeJSON: + dec := json.NewDecoder(os.Stdin) + err := dec.Decode(&in) + if err != nil { + log.Println(err) + } + case inputTypeCSV: + r := csv.NewReader(os.Stdin) + rows, err := r.ReadAll() + if err != nil { + log.Println(err) + } + in = parseRows(rows) } - fmt.Println() var x, y [][]reflect.Value if xyPattern != nil { x, y = split(match(in, xyPattern)) @@ -138,6 +162,7 @@ func main() { } p.Clear() c := draw.Canvas{Pixels: p} + fmt.Println() switch config.PlotType.Value { case plotTypeLine: fmt.Println(linePlot(x, y, c)) |