Skip to content

Commit

Permalink
feat(config): improve config saving and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
janyksteenbeek committed Nov 16, 2024
1 parent 4525534 commit ef3f6ba
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
20 changes: 15 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,18 @@ func LoadConfig() (*Config, error) {
}

func SaveConfig(config *Config) error {
viper.Set("moneybird.token", config.Moneybird.Token)
viper.Set("gmail.token", config.Gmail.Token)
viper.Set("app.last_update", config.App.LastUpdate)
return viper.WriteConfig()
}
viper.Set("moneybird.token", config.Moneybird.Token)
viper.Set("gmail.token", config.Gmail.Token)
viper.Set("app.last_update", config.App.LastUpdate)

if err := viper.WriteConfig(); err != nil {
return fmt.Errorf("writing config: %w", err)
}

// Force viper to read the config file again
if err := viper.ReadInConfig(); err != nil {
return fmt.Errorf("reloading config: %w", err)
}

return nil
}
12 changes: 9 additions & 3 deletions internal/processor/email_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@ func (p *EmailProcessor) ProcessEmails(ctx context.Context) ([]gmail.Email, erro
}

func (p *EmailProcessor) UpdateLastProcessed(email gmail.Email) error {
p.lastUpdate = email.Date.Add(time.Second)
p.cfg.App.LastUpdate = p.lastUpdate.Format(time.RFC3339)
return config.SaveConfig(p.cfg)
p.lastUpdate = email.Date.Add(time.Second)
p.cfg.App.LastUpdate = p.lastUpdate.Format(time.RFC3339)

if err := config.SaveConfig(p.cfg); err != nil {
return fmt.Errorf("failed to save config: %w", err)
}

log.Printf("Updated last processed time to: %v", p.lastUpdate.Format(time.RFC3339))
return nil
}

0 comments on commit ef3f6ba

Please sign in to comment.