|
| 1 | +// Copyright 2026 Peter Edge |
| 2 | +// |
| 3 | +// All rights reserved. |
| 4 | + |
| 5 | +// Package lotlist implements the "holding lot list" command. |
| 6 | +package lotlist |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "os" |
| 11 | + |
| 12 | + "buf.build/go/app/appcmd" |
| 13 | + "buf.build/go/app/appext" |
| 14 | + "github.com/bufdev/ibctl/cmd/ibctl/internal/ibctlcmd" |
| 15 | + "github.com/bufdev/ibctl/internal/ibctl/ibctlconfig" |
| 16 | + "github.com/bufdev/ibctl/internal/ibctl/ibctlfxrates" |
| 17 | + "github.com/bufdev/ibctl/internal/ibctl/ibctlholdings" |
| 18 | + "github.com/bufdev/ibctl/internal/ibctl/ibctlmerge" |
| 19 | + "github.com/bufdev/ibctl/internal/ibctl/ibctlpath" |
| 20 | + "github.com/bufdev/ibctl/internal/pkg/cliio" |
| 21 | + "github.com/spf13/pflag" |
| 22 | +) |
| 23 | + |
| 24 | +// formatFlagName is the flag name for the output format. |
| 25 | +const formatFlagName = "format" |
| 26 | + |
| 27 | +// cachedFlagName is the flag name for skipping download and using cached data only. |
| 28 | +const cachedFlagName = "cached" |
| 29 | + |
| 30 | +// NewCommand returns a new lot list command. |
| 31 | +func NewCommand(name string, builder appext.SubCommandBuilder) *appcmd.Command { |
| 32 | + flags := newFlags() |
| 33 | + return &appcmd.Command{ |
| 34 | + Use: name + " <symbol>", |
| 35 | + Short: "List individual tax lots for a symbol", |
| 36 | + Args: appcmd.ExactArgs(1), |
| 37 | + Run: builder.NewRunFunc( |
| 38 | + func(ctx context.Context, container appext.Container) error { |
| 39 | + return run(ctx, container, flags, container.Arg(0)) |
| 40 | + }, |
| 41 | + ), |
| 42 | + BindFlags: flags.Bind, |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +type flags struct { |
| 47 | + // Dir is the base directory containing ibctl.yaml and data subdirectories. |
| 48 | + Dir string |
| 49 | + // Format is the output format (table, csv, json). |
| 50 | + Format string |
| 51 | + // Cached skips downloading and uses only cached data. |
| 52 | + Cached bool |
| 53 | +} |
| 54 | + |
| 55 | +func newFlags() *flags { |
| 56 | + return &flags{} |
| 57 | +} |
| 58 | + |
| 59 | +// Bind registers the flag definitions with the given flag set. |
| 60 | +func (f *flags) Bind(flagSet *pflag.FlagSet) { |
| 61 | + flagSet.StringVar(&f.Dir, ibctlcmd.DirFlagName, ".", "The ibctl directory containing ibctl.yaml") |
| 62 | + flagSet.StringVar(&f.Format, formatFlagName, "table", "Output format (table, csv, json)") |
| 63 | + flagSet.BoolVar(&f.Cached, cachedFlagName, false, "Skip downloading and use only cached data") |
| 64 | +} |
| 65 | + |
| 66 | +func run(ctx context.Context, container appext.Container, flags *flags, symbol string) error { |
| 67 | + format, err := cliio.ParseFormat(flags.Format) |
| 68 | + if err != nil { |
| 69 | + return appcmd.NewInvalidArgumentError(err.Error()) |
| 70 | + } |
| 71 | + // Read and validate the configuration file from the base directory. |
| 72 | + config, err := ibctlconfig.ReadConfig(flags.Dir) |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + // Download fresh data unless --cached is set. |
| 77 | + if !flags.Cached { |
| 78 | + downloader, err := ibctlcmd.NewDownloader(container, flags.Dir) |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + if err := downloader.Download(ctx); err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + } |
| 86 | + // Merge trade data from all sources. |
| 87 | + mergedData, err := ibctlmerge.Merge( |
| 88 | + ibctlpath.DataAccountsDirPath(config.DirPath), |
| 89 | + ibctlpath.CacheAccountsDirPath(config.DirPath), |
| 90 | + ibctlpath.ActivityStatementsDirPath(config.DirPath), |
| 91 | + ibctlpath.SeedDirPath(config.DirPath), |
| 92 | + config.AccountAliases, |
| 93 | + ) |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + // Load FX rates for USD conversion. |
| 98 | + fxStore := ibctlfxrates.NewStore(ibctlpath.CacheFXDirPath(config.DirPath)) |
| 99 | + // Get the lot list for the requested symbol. |
| 100 | + result, err := ibctlholdings.GetLotList(symbol, mergedData.Trades, mergedData.Positions, fxStore) |
| 101 | + if err != nil { |
| 102 | + return err |
| 103 | + } |
| 104 | + // Write output in the requested format. |
| 105 | + writer := os.Stdout |
| 106 | + switch format { |
| 107 | + case cliio.FormatTable: |
| 108 | + headers := ibctlholdings.LotListHeaders() |
| 109 | + rows := make([][]string, 0, len(result.Lots)) |
| 110 | + for _, l := range result.Lots { |
| 111 | + rows = append(rows, ibctlholdings.LotOverviewToTableRow(l)) |
| 112 | + } |
| 113 | + // Build totals row for P&L USD and VALUE USD columns. |
| 114 | + totalPnL, totalValue := ibctlholdings.ComputeLotTotals(result.Lots) |
| 115 | + totalsRow := make([]string, len(headers)) |
| 116 | + totalsRow[0] = "TOTAL" |
| 117 | + totalsRow[8] = totalPnL |
| 118 | + totalsRow[9] = totalValue |
| 119 | + return cliio.WriteTableWithTotals(writer, headers, rows, totalsRow) |
| 120 | + case cliio.FormatCSV: |
| 121 | + headers := ibctlholdings.LotListHeaders() |
| 122 | + records := make([][]string, 0, len(result.Lots)+1) |
| 123 | + records = append(records, headers) |
| 124 | + for _, l := range result.Lots { |
| 125 | + records = append(records, ibctlholdings.LotOverviewToRow(l)) |
| 126 | + } |
| 127 | + return cliio.WriteCSVRecords(writer, records) |
| 128 | + case cliio.FormatJSON: |
| 129 | + return cliio.WriteJSON(writer, result.Lots...) |
| 130 | + default: |
| 131 | + return appcmd.NewInvalidArgumentErrorf("unsupported format: %s", format) |
| 132 | + } |
| 133 | +} |
0 commit comments