summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Hurst <julian.hurst92@gmail.com>2020-08-03 14:52:37 +0200
committerJulian Hurst <julian.hurst92@gmail.com>2020-08-03 14:52:37 +0200
commit9b836710ad52ea06a84e919774e612170bf03292 (patch)
treec8305bd2bf9f58a9854fbc88b8b086f48f1ceabb
parent4b575266ca443b1cca945b14f5ebf10de6f87c66 (diff)
downloadstatusbar-9b836710ad52ea06a84e919774e612170bf03292.tar.gz
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.
-rw-r--r--statusbar.go28
1 files 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 {