|
| 1 | +// Copyright 2026 Peter Edge |
| 2 | +// |
| 3 | +// All rights reserved. |
| 4 | + |
| 5 | +// Package categorylist implements the "holding category list" command. |
| 6 | +package categorylist |
| 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 | +// downloadFlagName is the flag name for downloading fresh data before displaying. |
| 28 | +const downloadFlagName = "download" |
| 29 | + |
| 30 | +// NewCommand returns a new category list command. |
| 31 | +func NewCommand(name string, builder appext.SubCommandBuilder) *appcmd.Command { |
| 32 | + flags := newFlags() |
| 33 | + return &appcmd.Command{ |
| 34 | + Use: name, |
| 35 | + Short: "List holdings aggregated by category", |
| 36 | + Args: appcmd.NoArgs, |
| 37 | + Run: builder.NewRunFunc( |
| 38 | + func(ctx context.Context, container appext.Container) error { |
| 39 | + return run(ctx, container, flags) |
| 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 | + // Download fetches fresh data before displaying. |
| 52 | + Download 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.Download, downloadFlagName, false, "Download fresh data before displaying") |
| 64 | +} |
| 65 | + |
| 66 | +func run(ctx context.Context, container appext.Container, flags *flags) 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 if --download is set. |
| 77 | + if flags.Download { |
| 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 | + // Compute holdings via FIFO from all trade data. |
| 100 | + result, err := ibctlholdings.GetHoldingsOverview(mergedData.Trades, mergedData.Positions, mergedData.CashPositions, config, fxStore) |
| 101 | + if err != nil { |
| 102 | + return err |
| 103 | + } |
| 104 | + // Aggregate holdings by category. |
| 105 | + categories := ibctlholdings.GetCategoryList(result.Holdings) |
| 106 | + // Write output in the requested format. |
| 107 | + writer := os.Stdout |
| 108 | + switch format { |
| 109 | + case cliio.FormatTable: |
| 110 | + headers := ibctlholdings.CategoryListHeaders() |
| 111 | + rows := make([][]string, 0, len(categories)) |
| 112 | + for _, c := range categories { |
| 113 | + rows = append(rows, ibctlholdings.CategoryOverviewToTableRow(c)) |
| 114 | + } |
| 115 | + return cliio.WriteTable(writer, headers, rows) |
| 116 | + case cliio.FormatCSV: |
| 117 | + headers := ibctlholdings.CategoryListHeaders() |
| 118 | + records := make([][]string, 0, len(categories)+1) |
| 119 | + records = append(records, headers) |
| 120 | + for _, c := range categories { |
| 121 | + records = append(records, ibctlholdings.CategoryOverviewToRow(c)) |
| 122 | + } |
| 123 | + return cliio.WriteCSVRecords(writer, records) |
| 124 | + case cliio.FormatJSON: |
| 125 | + return cliio.WriteJSON(writer, categories...) |
| 126 | + default: |
| 127 | + return appcmd.NewInvalidArgumentErrorf("unsupported format: %s", format) |
| 128 | + } |
| 129 | +} |
0 commit comments