Use env vars

This commit is contained in:
Marcus Noble 2020-10-17 20:55:05 +01:00
parent 9b56b3a5f6
commit 38e0d61a5b
4 changed files with 6 additions and 18 deletions

View File

@ -20,7 +20,7 @@ var (
func init() { func init() {
refreshCmd.Flags().IntVar(&interval, "interval", 15, "How long in minutes to wait before refreshing feeds") refreshCmd.Flags().IntVar(&interval, "interval", 15, "How long in minutes to wait before refreshing feeds")
viper.BindPFlag("refreshTimeoutMinutes", refreshCmd.Flags().Lookup("interval")) viper.BindPFlag("REFRESH_TIMEOUT", refreshCmd.Flags().Lookup("interval"))
rootCmd.AddCommand(refreshCmd) rootCmd.AddCommand(refreshCmd)
} }

View File

@ -14,7 +14,7 @@ var fp = gofeed.NewParser()
var feedStore = &FeedStore{} var feedStore = &FeedStore{}
func Refresh() error { func Refresh() error {
interval := viper.GetInt("refreshTimeoutMinutes") interval := viper.GetInt("REFRESH_TIMEOUT")
for { for {
fmt.Println("Refreshing feeds...") fmt.Println("Refreshing feeds...")

View File

@ -10,7 +10,7 @@ import (
func (fs *FeedStore) getDB() *gorm.DB { func (fs *FeedStore) getDB() *gorm.DB {
if fs.db == nil { if fs.db == nil {
db, err := gorm.Open(sqlite.Open(viper.GetString("dbPath")), &gorm.Config{ db, err := gorm.Open(sqlite.Open(viper.GetString("DB_PATH")), &gorm.Config{
Logger: logger.Default.LogMode(logger.Error), Logger: logger.Default.LogMode(logger.Error),
}) })
if err != nil { if err != nil {

18
main.go
View File

@ -10,24 +10,12 @@ import (
func main() { func main() {
// Load config // Load config
setDefaultConfig() setDefaultConfig()
viper.SetConfigName("config") viper.AutomaticEnv()
viper.SetConfigType("yaml")
viper.AddConfigPath("$HOME/.gopherss")
viper.AddConfigPath(".")
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// Ignore
} else {
panic(err)
}
} else {
viper.WatchConfig()
}
cmd.Execute() cmd.Execute()
} }
func setDefaultConfig() { func setDefaultConfig() {
viper.SetDefault("refreshTimeoutMinutes", 15) viper.SetDefault("REFRESH_TIMEOUT", 15)
viper.SetDefault("dbPath", "./feeds.db") viper.SetDefault("DB_PATH", "./feeds.db")
} }