writers in writing package
This commit is contained in:
1 parent
4604175e8d
commit
bb00676452
15 files changed
+215
-721
No files matched your search
@@ -1,57 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/nmoniz/any2anexoj/internal"
|
||||
)
|
||||
|
||||
type CSVWriter struct {
|
||||
w *csv.Writer
|
||||
}
|
||||
|
||||
func NewCSVWriter(w io.Writer) *CSVWriter {
|
||||
return &CSVWriter{w: csv.NewWriter(w)}
|
||||
}
|
||||
|
||||
func (cw *CSVWriter) Render(aw *internal.AggregatorWriter) error {
|
||||
err := cw.w.Write([]string{
|
||||
"source_country", "code",
|
||||
"realization_year", "realization_month", "realization_day", "realization_value",
|
||||
"acquisition_year", "acquisition_month", "acquisition_day", "acquisition_value",
|
||||
"expenses", "foreign_tax_paid", "counter_country",
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("write csv header: %w", err)
|
||||
}
|
||||
|
||||
for ri := range aw.Iter() {
|
||||
err := cw.w.Write(reportItemToRow(ri))
|
||||
if err != nil {
|
||||
return fmt.Errorf("write csv row: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
cw.w.Flush()
|
||||
return cw.w.Error()
|
||||
}
|
||||
|
||||
func reportItemToRow(ri internal.ReportItem) []string {
|
||||
return []string{
|
||||
fmt.Sprintf("%d", ri.AssetCountry),
|
||||
string(ri.Nature),
|
||||
fmt.Sprintf("%d", ri.SellTimestamp.Year()),
|
||||
fmt.Sprintf("%d", int(ri.SellTimestamp.Month())),
|
||||
fmt.Sprintf("%d", ri.SellTimestamp.Day()),
|
||||
ri.SellValue.StringFixed(2),
|
||||
fmt.Sprintf("%d", ri.BuyTimestamp.Year()),
|
||||
fmt.Sprintf("%d", int(ri.BuyTimestamp.Month())),
|
||||
fmt.Sprintf("%d", ri.BuyTimestamp.Day()),
|
||||
ri.BuyValue.StringFixed(2),
|
||||
ri.Fees.StringFixed(2),
|
||||
ri.Taxes.StringFixed(2),
|
||||
fmt.Sprintf("%d", ri.BrokerCountry),
|
||||
}
|
||||
}
|
||||
@@ -12,8 +12,10 @@ import (
|
||||
|
||||
"github.com/nmoniz/any2anexoj/internal"
|
||||
"github.com/nmoniz/any2anexoj/internal/ofigi"
|
||||
"github.com/nmoniz/any2anexoj/internal/prettyprinter"
|
||||
"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/pretty"
|
||||
"github.com/spf13/pflag"
|
||||
"golang.org/x/text/language"
|
||||
)
|
||||
@@ -99,13 +101,13 @@ func runWithIO(ctx context.Context, stdin io.Reader, stdout io.Writer) error {
|
||||
|
||||
switch *format {
|
||||
case "csv":
|
||||
return NewCSVWriter(stdout).Render(writer)
|
||||
return csv.NewCSVWriter(stdout).Render(writer)
|
||||
case "table":
|
||||
loc, err := prettyprinter.NewLocalizer(*lang)
|
||||
loc, err := writing.NewLocalizer(*lang)
|
||||
if err != nil {
|
||||
return fmt.Errorf("create localizer: %w", err)
|
||||
}
|
||||
prettyprinter.NewPrettyPrinter(stdout, loc).Render(writer)
|
||||
pretty.NewPrettyPrinter(stdout, loc).Render(writer)
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("unsupported format %q: must be table or csv", *format)
|
||||
|
||||
Reference in new issue
Block a user