Skip to content

Commit

Permalink
ref: config reloader
Browse files Browse the repository at this point in the history
  • Loading branch information
leukipp committed Oct 28, 2024
1 parent 7ab7e74 commit 5e00681
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func InitConfig() {
}

// Read config file into memory
readConfig(Args.Config)
readConfig(Args.Config, true)

// Config file system watcher
watchConfig(Args.Config)
Expand All @@ -72,32 +72,38 @@ func ConfigFolderPath(name string) string {
return filepath.Join(userConfigDir, name)
}

func readConfig(configFilePath string) {
func readConfig(configFilePath string, initial bool) {

// Print build infos
fmt.Print("BUILD")
if HasReleaseInfos() {
fmt.Printf(" [>>> %s v%s is available <<<]", Build.Name, Source.Releases[0].Name)
// Print runtime infos
if initial {
fmt.Print("BUILD")
if HasReleaseInfos() {
fmt.Printf(" [>>> %s v%s is available <<<]", Build.Name, Source.Releases[0].Name)
}
fmt.Printf(": \n name: %s\n target: %s\n version: v%s-%s\n date: %s\n\n", Build.Name, Build.Target, Build.Version, Build.Commit, Build.Date)
fmt.Printf("FILES: \n log: %s\n lock: %s\n cache: %s\n config: %s\n\n", Args.Log, Args.Lock, Args.Cache, configFilePath)
}
fmt.Printf(": \n name: %s\n target: %s\n version: v%s-%s\n date: %s\n\n", Build.Name, Build.Target, Build.Version, Build.Commit, Build.Date)

// Print file infos
fmt.Printf("FILES: \n log: %s\n lock: %s\n cache: %s\n config: %s\n\n", Args.Log, Args.Lock, Args.Cache, configFilePath)

// Decode config file into struct
_, err := toml.DecodeFile(configFilePath, &Config)
if err != nil {
log.Fatal("Error reading config file ", err)
if initial {
log.Fatal("Error reading config file ", err)
} else {
log.Warn("Error updating config file ", err)
}
}

// Print shortcut infos
keys, _ := json.MarshalIndent(Config.Keys, "", " ")
corners, _ := json.MarshalIndent(Config.Corners, "", " ")
systray, _ := json.MarshalIndent(Config.Systray, "", " ")

fmt.Printf("KEYS: %s\n", RemoveChars(string(keys), []string{"{", "}", "\"", ","}))
fmt.Printf("CORNERS: %s\n", RemoveChars(string(corners), []string{"{", "}", "\"", ","}))
fmt.Printf("SYSTRAY: %s\n", RemoveChars(string(systray), []string{"{", "}", "\"", ","}))
if initial {
keys, _ := json.MarshalIndent(Config.Keys, "", " ")
corners, _ := json.MarshalIndent(Config.Corners, "", " ")
systray, _ := json.MarshalIndent(Config.Systray, "", " ")

fmt.Printf("KEYS: %s\n", RemoveChars(string(keys), []string{"{", "}", "\"", ","}))
fmt.Printf("CORNERS: %s\n", RemoveChars(string(corners), []string{"{", "}", "\"", ","}))
fmt.Printf("SYSTRAY: %s\n", RemoveChars(string(systray), []string{"{", "}", "\"", ","}))
}
}

func watchConfig(configFilePath string) {
Expand All @@ -119,7 +125,7 @@ func watchConfig(configFilePath string) {
return
}
if event.Has(fsnotify.Write) {
readConfig(configFilePath)
readConfig(configFilePath, false)
}
case err, ok := <-watcher.Errors:
if !ok {
Expand Down

0 comments on commit 5e00681

Please sign in to comment.