add ability to sync journal only

This commit is contained in:
Anantha Kumaran 2022-08-20 15:48:03 +05:30
parent e9657e5acd
commit 6d5592f6c7
2 changed files with 18 additions and 2 deletions

View File

@ -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")
}

View File

@ -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 {