From 91f7132d5eb808c8cdeef230be16199529b339bf Mon Sep 17 00:00:00 2001 From: Arsen Musayelyan Date: Tue, 24 Aug 2021 08:33:41 -0700 Subject: [PATCH] Create new config format --- itd.toml | 21 ++++++++++++++++----- main.go | 18 ++++++++++++------ music.go | 4 ++-- notifs.go | 6 +++--- socket.go | 11 +++++------ 5 files changed, 38 insertions(+), 22 deletions(-) diff --git a/itd.toml b/itd.toml index a970e3a..801a2e7 100644 --- a/itd.toml +++ b/itd.toml @@ -1,14 +1,25 @@ +# This is temporary, it is to show a notice +# to people still using the old config +cfg.version = 2 + +[socket] +path = "/tmp/itd/socket" + [conn] reconnect = true -[notify] - onConnect = true - onReconnect = true +[on.connect] + notify = true -[notifications.ignore] +[on.reconnect] + notify = true + setTime = true + +[notifs.ignore] sender = [] summary = ["InfiniTime"] body = [] [music] - volInterval = 5 \ No newline at end of file + vol.interval = 5 + diff --git a/main.go b/main.go index 21749ee..93f212c 100644 --- a/main.go +++ b/main.go @@ -50,6 +50,10 @@ func init() { } func main() { + if viper.GetInt("cfg.version") != 2 { + log.Fatal().Msg("Please update your config to the newest format, only v2 configs supported.") + } + // Cleanly exit after function defer infinitime.Exit() @@ -63,14 +67,16 @@ func main() { // When InfiniTime reconnects dev.OnReconnect(func() { - // Set time to current time - err = dev.SetTime(time.Now()) - if err != nil { - log.Error().Err(err).Msg("Error setting current time on connected InfiniTime") + if viper.GetBool("on.reconnect.setTime") { + // Set time to current time + err = dev.SetTime(time.Now()) + if err != nil { + log.Error().Err(err).Msg("Error setting current time on connected InfiniTime") + } } // If config specifies to notify on reconnect - if viper.GetBool("notify.onReconnect") { + if viper.GetBool("on.reconnect.notify") { // Send notification to InfiniTime err = dev.Notify("itd", "Successfully reconnected") if err != nil { @@ -89,7 +95,7 @@ func main() { log.Info().Str("version", ver).Msg("Connected to InfiniTime") // If config specifies to notify on connect - if viper.GetBool("notify.onConnect") { + if viper.GetBool("on.connect.notify") { // Send notification to InfiniTime err = dev.Notify("itd", "Successfully connected") if err != nil { diff --git a/music.go b/music.go index 496daba..be2dfde 100644 --- a/music.go +++ b/music.go @@ -85,9 +85,9 @@ func initMusicCtrl(dev *infinitime.Device) error { case infinitime.MusicEventPrev: player.Prev() case infinitime.MusicEventVolUp: - player.VolUp(viper.GetUint("music.volInterval")) + player.VolUp(viper.GetUint("music.vol.interval")) case infinitime.MusicEventVolDown: - player.VolDown(viper.GetUint("music.volInterval")) + player.VolDown(viper.GetUint("music.vol.interval")) } } }() diff --git a/notifs.go b/notifs.go index d829eee..0d1c7e9 100644 --- a/notifs.go +++ b/notifs.go @@ -90,9 +90,9 @@ func initNotifRelay(dev *infinitime.Device) error { // ignored checks whether any fields were ignored in the config func ignored(sender, summary, body string) bool { - ignoreSender := viper.GetStringSlice("notifications.ignore.sender") - ignoreSummary := viper.GetStringSlice("notifications.ignore.summary") - ignoreBody := viper.GetStringSlice("notifications.ignore.body") + ignoreSender := viper.GetStringSlice("notifs.ignore.sender") + ignoreSummary := viper.GetStringSlice("notifs.ignore.summary") + ignoreBody := viper.GetStringSlice("notifs.ignore.body") return strSlcContains(ignoreSender, sender) || strSlcContains(ignoreSummary, summary) || strSlcContains(ignoreBody, body) diff --git a/socket.go b/socket.go index 6480c86..58429a9 100644 --- a/socket.go +++ b/socket.go @@ -29,12 +29,11 @@ import ( "github.com/mitchellh/mapstructure" "github.com/rs/zerolog/log" + "github.com/spf13/viper" "go.arsenm.dev/infinitime" "go.arsenm.dev/itd/internal/types" ) -const SockPath = "/tmp/itd/socket" - const ( ReqTypeHeartRate = "hrt" ReqTypeBattLevel = "battlvl" @@ -52,19 +51,19 @@ const ( func startSocket(dev *infinitime.Device) error { // Make socket directory if non-existent - err := os.MkdirAll(filepath.Dir(SockPath), 0755) + err := os.MkdirAll(filepath.Dir(viper.GetString("socket.path")), 0755) if err != nil { return err } // Remove old socket if it exists - err = os.RemoveAll(SockPath) + err = os.RemoveAll(viper.GetString("socket.path")) if err != nil { return err } // Listen on socket path - ln, err := net.Listen("unix", SockPath) + ln, err := net.Listen("unix", viper.GetString("socket.path")) if err != nil { return err } @@ -83,7 +82,7 @@ func startSocket(dev *infinitime.Device) error { }() // Log socket start - log.Info().Str("path", SockPath).Msg("Started control socket") + log.Info().Str("path", viper.GetString("socket.path")).Msg("Started control socket") return nil }