diff --git a/cmd/update.go b/cmd/update.go index 879b9ae..366e024 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -9,6 +9,9 @@ import ( "gorm.io/gorm" ) +var updateJournal bool +var updateCommodities bool + var updateCmd = &cobra.Command{ Use: "update", Short: "Sync journal data", @@ -17,10 +20,21 @@ var updateCmd = &cobra.Command{ if err != nil { log.Fatal(err) } - model.Sync(db) + + syncAll := !updateJournal && !updateCommodities + + if syncAll || updateJournal { + model.SyncJournal(db) + } + + if syncAll || updateCommodities { + model.SyncCommodities(db) + } }, } func init() { rootCmd.AddCommand(updateCmd) + updateCmd.Flags().BoolVarP(&updateJournal, "journal", "j", false, "update journal") + updateCmd.Flags().BoolVarP(&updateCommodities, "commodity", "c", false, "update commodities") } diff --git a/internal/model/model.go b/internal/model/model.go index d5fa59d..e8a997e 100644 --- a/internal/model/model.go +++ b/internal/model/model.go @@ -12,12 +12,14 @@ import ( "gorm.io/gorm" ) -func Sync(db *gorm.DB) { +func SyncJournal(db *gorm.DB) { db.AutoMigrate(&posting.Posting{}) log.Info("Syncing transactions from journal") postings, _ := ledger.Parse(viper.GetString("journal_path")) posting.UpsertAll(db, postings) +} +func SyncCommodities(db *gorm.DB) { db.AutoMigrate(&price.Price{}) log.Info("Fetching commodities price history") type Commodity struct {