diff --git a/config.go b/config.go index 51036cb..e01e6e6 100644 --- a/config.go +++ b/config.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "log" + "net/url" "os" "slices" @@ -12,7 +13,8 @@ import ( ) type Config struct { - Data Data + Data Data + Output url.URL } type Data struct { @@ -35,6 +37,9 @@ func newConfig() (Config, error) { } result.Data.Weight.Floor = 12 result.Data.Weight.Range = 20 + result.Output = url.URL{Scheme: "file", Path: "/tmp/f"} + result.Data.Title = os.Getenv("TITLE") + result.Data.Subtitle = os.Getenv("SUBTITLE") if js := os.Getenv("POINTS_JSON"); js == "" { } else if err := json.Unmarshal([]byte(js), &result.Data.Points); err != nil { @@ -74,8 +79,12 @@ func newConfig() (Config, error) { return result, fmt.Errorf("found negative weight floor %d", result.Data.Weight.Floor) } - result.Data.Title = os.Getenv("TITLE") - result.Data.Subtitle = os.Getenv("SUBTITLE") + if s := os.Getenv("OUTPUT"); s == "" { + } else if u, err := url.Parse(s); err != nil { + return result, fmt.Errorf("failed to parse $OUTPUT (%s): %w", s, err) + } else { + result.Output = *u + } return result, nil } diff --git a/main.go b/main.go index 11e5b55..8c4e097 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,9 @@ package main import ( + "bytes" "context" + "fmt" "log" "os" "os/signal" @@ -50,12 +52,15 @@ func run(ctx context.Context) error { })) scatter.SetXAxis(xs). AddSeries(y, ys) - f, _ := os.Create("/tmp/f") - defer f.Close() - if err := scatter.Render(f); err != nil { + + buff := bytes.NewBuffer(nil) + if err := scatter.Render(buff); err != nil { return err } - log.Println("firefox /tmp/f") - return nil + switch config.Output.Scheme { + case "file": + return os.WriteFile(config.Output.Path, buff.Bytes(), os.ModePerm) + } + return fmt.Errorf("not impl output scheme: %s", config.Output.Scheme) }