print as html

This commit is contained in:
natercio committed 2026-10-05 10:33:02 +01:00
1 parent bb00676452
commit 3b971cda61
9 files changed
+339 -7

No files matched your search

+13 -2
View File
@@ -15,6 +15,7 @@ import (
"github.com/nmoniz/any2anexoj/internal/trading212"
"github.com/nmoniz/any2anexoj/internal/writing"
"github.com/nmoniz/any2anexoj/internal/writing/csv"
"github.com/nmoniz/any2anexoj/internal/writing/html"
"github.com/nmoniz/any2anexoj/internal/writing/pretty"
"github.com/spf13/pflag"
"golang.org/x/text/language"
@@ -26,7 +27,7 @@ var (
platform = pflag.StringP("platform", "p", "trading212", "One of the supported platforms")
lang = pflag.StringP("language", "l", language.Portuguese.String(), "The 2 letter language code")
debug = pflag.BoolP("debug", "d", false, "Activate to log debug messages")
format = pflag.StringP("format", "f", "table", "Output format: table or csv")
format = pflag.StringP("format", "f", "table", "Output format: table, csv or html")
ofAPIKey = pflag.String("open-figi-api-key", "", "An OpenFIGI API key for faster report generation (better rate api rate limits)")
selectors = pflag.StringSlice("selectors", nil, "Only process entries that conform to all the selectors: code, assetCountry")
stateFile = pflag.String("state-file", "", "Path to a state file for incremental processing")
@@ -109,8 +110,18 @@ func runWithIO(ctx context.Context, stdin io.Reader, stdout io.Writer) error {
}
pretty.NewPrettyPrinter(stdout, loc).Render(writer)
return nil
case "html":
loc, err := writing.NewLocalizer(*lang)
if err != nil {
return fmt.Errorf("create localizer: %w", err)
}
// RenderHTML never writes to the printer's output writer, so
// io.Discard is safe here.
printer := pretty.NewPrettyPrinter(io.Discard, loc)
title := loc.Translate("report_title", 1, nil)
return html.NewHTMLWriter(stdout, printer, title, *lang).Render(writer)
default:
return fmt.Errorf("unsupported format %q: must be table or csv", *format)
return fmt.Errorf("unsupported format %q: must be table, csv or html", *format)
}
}
+31
View File
@@ -139,6 +139,37 @@ func TestRunWithIO_NoStateFileByDefault(t *testing.T) {
}
}
// TestRunWithIO_HTMLFormat verifies that --format html produces a complete
// HTML document on stdout starting with the DOCTYPE declaration.
func TestRunWithIO_HTMLFormat(t *testing.T) {
resetFlags(t)
t.Cleanup(func() { resetFlags(t) })
if err := pflag.Set("format", "html"); err != nil {
t.Fatalf("set format flag: %v", err)
}
var stdout bytes.Buffer
if err := runWithStdin(t, trading212SampleCSV, &stdout); err != nil {
t.Fatalf("runWithIO returned an error: %v\nstdout: %s", err, stdout.String())
}
got := stdout.String()
if !strings.HasPrefix(got, "<!DOCTYPE html>") {
t.Errorf("expected HTML output to start with <!DOCTYPE html>; got prefix: %q",
got[:min(len(got), 64)])
}
if !strings.Contains(got, "table-responsive") {
t.Errorf("expected output to contain Bootstrap table-responsive wrapper")
}
if !strings.Contains(got, "table-striped") {
t.Errorf("expected output to contain Bootstrap table-striped class")
}
if !strings.Contains(got, "</html>") {
t.Errorf("expected output to end with </html>")
}
}
// TestRunWithIO_UnsupportedPlatformForPersistence verifies that using
// --state-file with an unknown platform surfaces a clear error rather than
// silently falling back to EphemeralStore.