Skip to content

Commit

Permalink
Add template for multiperiod flags
Browse files Browse the repository at this point in the history
  • Loading branch information
Silvio Böhler committed Aug 25, 2023
1 parent e3ffed8 commit ed84b54
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 35 deletions.
14 changes: 4 additions & 10 deletions cmd/commands/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

"github.com/sboehler/knut/cmd/flags"
"github.com/sboehler/knut/lib/amounts"
"github.com/sboehler/knut/lib/common/date"
"github.com/sboehler/knut/lib/common/mapper"
"github.com/sboehler/knut/lib/common/predicate"
"github.com/sboehler/knut/lib/common/table"
Expand Down Expand Up @@ -55,18 +54,15 @@ func CreateBalanceCommand() *cobra.Command {
}

type balanceRunner struct {
flags.Multiperiod

// internal
cpuprofile string

// journal structure
close bool
valuation flags.CommodityFlag

// alignment
period flags.PeriodFlag
last int
interval flags.IntervalFlags

// mapping
mapping flags.MappingFlag
remap flags.RegexFlag
Expand Down Expand Up @@ -103,14 +99,12 @@ func (r *balanceRunner) run(cmd *cobra.Command, args []string) {
}

func (r *balanceRunner) setupFlags(c *cobra.Command) {
r.Multiperiod.Setup(c)
c.Flags().StringVar(&r.cpuprofile, "cpuprofile", "", "file to write profile")
r.period.Setup(c, date.Period{End: date.Today()})
c.Flags().IntVar(&r.last, "last", 0, "last n periods")
c.Flags().BoolVarP(&r.diff, "diff", "d", false, "diff")
c.Flags().BoolVar(&r.close, "close", true, "close")
c.Flags().BoolVarP(&r.sortAlphabetically, "sort", "a", false, "Sort accounts alphabetically")
c.Flags().VarP(&r.showCommodities, "show-commodities", "s", "<regex>")
r.interval.Setup(c, date.Once)
c.Flags().VarP(&r.valuation, "val", "v", "valuate in the given commodity")
c.Flags().VarP(&r.mapping, "map", "m", "<level>,<regex>")
c.Flags().VarP(&r.remap, "remap", "r", "<regex>")
Expand All @@ -131,7 +125,7 @@ func (r balanceRunner) execute(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
partition := date.NewPartition(r.period.Value().Clip(j.Period()), r.interval.Value(), r.last)
partition := r.Multiperiod.Partition(j.Period())
rep := balance.NewReport(reg, partition)
_, err = j.Process(
journal.ComputePrices(valuation),
Expand Down
10 changes: 3 additions & 7 deletions cmd/commands/portfolio/returns.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/spf13/cobra"

"github.com/sboehler/knut/cmd/flags"
"github.com/sboehler/knut/lib/common/date"
"github.com/sboehler/knut/lib/common/predicate"
"github.com/sboehler/knut/lib/journal"
"github.com/sboehler/knut/lib/journal/performance"
Expand All @@ -49,21 +48,18 @@ func CreateReturnsCommand() *cobra.Command {
}

type returnsRunner struct {
flags.Multiperiod
cpuprofile string
valuation flags.CommodityFlag
accounts, commodities flags.RegexFlag
period flags.PeriodFlag
interval flags.IntervalFlags
}

func (r *returnsRunner) setupFlags(cmd *cobra.Command) {
r.Multiperiod.Setup(cmd)
cmd.Flags().StringVar(&r.cpuprofile, "cpuprofile", "", "file to write profile")
cmd.Flags().VarP(&r.valuation, "val", "v", "valuate in the given commodity")
cmd.Flags().Var(&r.accounts, "account", "filter accounts with a regex")
cmd.Flags().Var(&r.commodities, "commodity", "filter commodities with a regex")
r.period.Setup(cmd, date.Period{End: date.Today()})
r.interval.Setup(cmd, date.Once)

}

func (r *returnsRunner) run(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -92,7 +88,7 @@ func (r *returnsRunner) execute(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
partition := date.NewPartition(r.period.Value().Clip(j.Period()), r.interval.Value(), 0)
partition := r.Multiperiod.Partition(j.Period())
calculator := &performance.Calculator{
Context: reg,
Valuation: valuation,
Expand Down
15 changes: 5 additions & 10 deletions cmd/commands/portfolio/weights.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/spf13/cobra"

"github.com/sboehler/knut/cmd/flags"
"github.com/sboehler/knut/lib/common/date"
"github.com/sboehler/knut/lib/common/predicate"
"github.com/sboehler/knut/lib/common/table"
"github.com/sboehler/knut/lib/journal"
Expand Down Expand Up @@ -51,14 +50,11 @@ func CreateWeightsCommand() *cobra.Command {
}

type weightsRunner struct {
flags.Multiperiod

valuation flags.CommodityFlag
accounts, commodities flags.RegexFlag

// alignment
period flags.PeriodFlag
last int
interval flags.IntervalFlags

// formatting
thousands bool
color bool
Expand All @@ -74,14 +70,13 @@ type weightsRunner struct {
}

func (r *weightsRunner) setupFlags(cmd *cobra.Command) {
r.Multiperiod.Setup(cmd)
cmd.Flags().StringVarP(&r.universe, "universe", "", "", "universe file")
cmd.Flags().VarP(&r.valuation, "val", "v", "valuate in the given commodity")
cmd.Flags().Var(&r.accounts, "account", "filter accounts with a regex")
cmd.Flags().Var(&r.commodities, "commodity", "filter commodities with a regex")
r.period.Setup(cmd, date.Period{End: date.Today()})
r.interval.Setup(cmd, date.Once)

cmd.Flags().BoolVarP(&r.sortAlphabetically, "sort", "a", false, "Sort accounts alphabetically")
cmd.Flags().IntVar(&r.last, "last", 0, "last n periods")
cmd.Flags().BoolVar(&r.csv, "csv", false, "render csv")
cmd.Flags().BoolVar(&r.omitCommodities, "omit-commodities", false, "don't render commodities")
cmd.Flags().VarP(&r.mapping, "map", "m", "<level>,<regex>")
Expand Down Expand Up @@ -117,7 +112,7 @@ func (r *weightsRunner) execute(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
partition := date.NewPartition(r.period.Value().Clip(j.Period()), r.interval.Value(), r.last)
partition := r.Multiperiod.Partition(j.Period())
calculator := &performance.Calculator{
Context: reg,
Valuation: valuation,
Expand Down
12 changes: 4 additions & 8 deletions cmd/commands/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

"github.com/sboehler/knut/cmd/flags"
"github.com/sboehler/knut/lib/amounts"
"github.com/sboehler/knut/lib/common/date"
"github.com/sboehler/knut/lib/common/mapper"
"github.com/sboehler/knut/lib/common/predicate"
"github.com/sboehler/knut/lib/common/table"
Expand Down Expand Up @@ -56,13 +55,12 @@ func CreateRegisterCmd() *cobra.Command {
}

type registerRunner struct {
flags.Multiperiod

// internal
cpuprofile string

// transformations
period flags.PeriodFlag
last int
interval flags.IntervalFlags
showCommodities bool
showSource bool
showDescriptions bool
Expand Down Expand Up @@ -94,10 +92,8 @@ func (r *registerRunner) run(cmd *cobra.Command, args []string) {
}

func (r *registerRunner) setupFlags(c *cobra.Command) {
r.Multiperiod.Setup(c)
c.Flags().StringVar(&r.cpuprofile, "cpuprofile", "", "file to write profile")
r.period.Setup(c, date.Period{End: date.Today()})
c.Flags().IntVar(&r.last, "last", 0, "last n periods")
r.interval.Setup(c, date.Daily)
c.Flags().BoolVarP(&r.sortAlphabetically, "sort", "s", false, "Sort accounts alphabetically")
c.Flags().BoolVarP(&r.showCommodities, "show-commodities", "c", false, "Show commodities")
c.Flags().BoolVarP(&r.showDescriptions, "show-descriptions", "d", false, "Show descriptions")
Expand Down Expand Up @@ -129,7 +125,7 @@ func (r registerRunner) execute(cmd *cobra.Command, args []string) error {
if r.showSource {
am = account.Remap(reg.Accounts(), r.remap.Regex())
}
partition := date.NewPartition(r.period.Value().Clip(j.Period()), r.interval.Value(), r.last)
partition := r.Multiperiod.Partition(j.Period())
rep := register.NewReport(reg)
_, err = j.Process(
journal.Sort(),
Expand Down
22 changes: 22 additions & 0 deletions cmd/flags/templates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package flags

import (
"github.com/sboehler/knut/lib/common/date"
"github.com/spf13/cobra"
)

type Multiperiod struct {
period PeriodFlag
last int
interval IntervalFlags
}

func (mp *Multiperiod) Setup(cmd *cobra.Command) {
mp.period.Setup(cmd, date.Period{End: date.Today()})
cmd.Flags().IntVar(&mp.last, "last", 0, "last n periods")
mp.interval.Setup(cmd, date.Once)
}

func (mp *Multiperiod) Partition(clip date.Period) date.Partition {
return date.NewPartition(mp.period.Value().Clip(clip), mp.interval.Value(), mp.last)
}

0 comments on commit ed84b54

Please sign in to comment.