From 9b836710ad52ea06a84e919774e612170bf03292 Mon Sep 17 00:00:00 2001 From: Julian Hurst Date: Mon, 3 Aug 2020 14:52:37 +0200 Subject: Add support for reloading the config through SIGHUP The config can now be reloaded at runtime by starting statusbar in daemon mode (-d) and sending it a SIGHUP. --- statusbar.go | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/statusbar.go b/statusbar.go index 73f3cb2..47b04f9 100644 --- a/statusbar.go +++ b/statusbar.go @@ -8,6 +8,7 @@ import ( "strconv" "os" "os/exec" + "os/signal" "io/ioutil" "time" "flag" @@ -17,6 +18,8 @@ import ( "github.com/google/shlex" ) +var config Config + type MusicPlayer interface { nowPlaying() string status() int @@ -396,8 +399,26 @@ func readConfig(configPath *string) ([]byte, error) { return content, err } +func awaitSighup(configPath *string) { + for { + c := make(chan os.Signal, 1) + signal.Notify(c, syscall.SIGHUP) + <-c + fmt.Fprintf(os.Stderr, "Reloading config...") + content, err := readConfig(configPath) + if err != nil { + panic(err) + } + _, err = toml.Decode(string(content), &config) + if err != nil { + panic(err) + } + } +} + func main() { - configPath := flag.String("c", getDefaultConfigPath(), "The config path") + configPath := flag.String("c", getDefaultConfigPath(), "The config path.") + daemon := flag.Bool("d", false, "Whether to launch statusbar as a daemon process or not.") flag.Parse() if *configPath == "-" { configPath = nil @@ -406,11 +427,14 @@ func main() { if err != nil { panic(err) } - var config Config + //var config Config _, err = toml.Decode(string(content), &config) if err != nil { panic(err) } + if *daemon { + go awaitSighup(configPath) + } for true { var b strings.Builder for i, section := range config.Sections { -- cgit v1.2.3