From bb790459c18593bf7e41192b192aefe07ba909d2 Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Wed, 23 Oct 2024 17:36:30 +0200 Subject: [PATCH] feat: add configuration file handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - add helper function to parse configuration from a YAML file - update command-line parameters to include a config file option - remove hardcoded Pomodoro settings and use parsed config instead - delete obsolete PomodoroSettings.go file 🤖 --- cmd/client/main.go | 36 +++++++++++++++++++++++++----------- config/PomodoroSettings.go | 12 ------------ go.mod | 1 + go.sum | 4 ++++ internal/helper/config.go | 26 ++++++++++++++++++++++++++ internal/websocket/send.go | 5 ++--- pkg/models/configfile.go | 11 +++++++++++ 7 files changed, 69 insertions(+), 26 deletions(-) delete mode 100644 config/PomodoroSettings.go create mode 100644 internal/helper/config.go create mode 100644 pkg/models/configfile.go diff --git a/cmd/client/main.go b/cmd/client/main.go index c4d9d91..9184598 100644 --- a/cmd/client/main.go +++ b/cmd/client/main.go @@ -5,10 +5,13 @@ import ( "os" "os/signal" + "ChronoTomato/internal/helper" "ChronoTomato/internal/shared" "ChronoTomato/internal/websocket" + "atomicgo.dev/keyboard" "atomicgo.dev/keyboard/keys" + "git.smsvc.net/pomodoro/GoTomato/pkg/models" ) var interrupt = make(chan os.Signal, 1) @@ -16,16 +19,27 @@ var interrupt = make(chan os.Signal, 1) func Start() { signal.Notify(interrupt, os.Interrupt) - GoTomatoUrl := flag.String("url", "ws://localhost:8080/ws", "GoTomato Server URL") - password := flag.String("password", "", "Control password for pomodoro session (optional)") - autoStart := flag.Bool("start", false, "Immediately start a Pomodoro") + parameter_url := flag.String("url", "", "GoTomato Server URL (eg ws://localhost:8080/ws)") + parameter_password := flag.String("password", "", "Control password for pomodoro session (optional)") + configfile := flag.String("config", "~/.config/ChronoTomato.yml", "path to config file (optional)") flag.Parse() - conn := websocket.Connect(*GoTomatoUrl) + config := helper.ParseConfig(*configfile) - websocket.Send_updateSettings(conn, *password) - if *autoStart { - websocket.SendCmd(conn, *password, "start") + url := *parameter_url + if url == "" { + url = config.URL + } + + password := *parameter_password + if password == "" { + password = config.Password + } + + conn := websocket.Connect(url) + + if config.PomodoroConfig != (models.GoTomatoPomodoroConfig{}) { + websocket.Send_updateSettings(conn, config.Password, config.PomodoroConfig) } go websocket.ProcessServerMessages(conn) @@ -35,18 +49,18 @@ func Start() { switch key.String() { case "space": if !pomodoro.Ongoing { - websocket.SendCmd(conn, *password, "start") + websocket.SendCmd(conn, password, "start") return false, nil } if pomodoro.Paused { - websocket.SendCmd(conn, *password, "resume") + websocket.SendCmd(conn, password, "resume") return false, nil } else { - websocket.SendCmd(conn, *password, "pause") + websocket.SendCmd(conn, password, "pause") return false, nil } case "r": - websocket.SendCmd(conn, *password, "stop") + websocket.SendCmd(conn, password, "stop") return false, nil case "q": interrupt <- os.Interrupt diff --git a/config/PomodoroSettings.go b/config/PomodoroSettings.go deleted file mode 100644 index 36c64c5..0000000 --- a/config/PomodoroSettings.go +++ /dev/null @@ -1,12 +0,0 @@ -package config - -import ( - "git.smsvc.net/pomodoro/GoTomato/pkg/models" -) - -var PomodoroSettings = models.GoTomatoPomodoroConfig{ - Work: 25 * 60, - ShortBreak: 5 * 60, - LongBreak: 10 * 60, - Sessions: 4, -} diff --git a/go.mod b/go.mod index 4192fff..2a0cb6e 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/fatih/color v1.18.0 github.com/gen2brain/beeep v0.0.0-20240516210008-9c006672e7f4 github.com/gorilla/websocket v1.5.3 + gopkg.in/yaml.v2 v2.2.4 ) require ( diff --git a/go.sum b/go.sum index fe9b589..ed5f5ae 100644 --- a/go.sum +++ b/go.sum @@ -44,8 +44,10 @@ github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa02 github.com/klauspost/cpuid/v2 v2.0.10/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c= github.com/klauspost/cpuid/v2 v2.0.12 h1:p9dKCg8i4gmOxtv35DvrYoWqYzQrvEVdjQ762Y0OqZE= github.com/klauspost/cpuid/v2 v2.0.12/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= @@ -107,8 +109,10 @@ golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b/go.mod h1:jbD1KX2456YbFQfuX golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/helper/config.go b/internal/helper/config.go new file mode 100644 index 0000000..257e1d4 --- /dev/null +++ b/internal/helper/config.go @@ -0,0 +1,26 @@ +package helper + +import ( + "ChronoTomato/pkg/models" + "github.com/charmbracelet/log" + "gopkg.in/yaml.v2" + "os" +) + +func ParseConfig(filename string) models.ConfigFile { + var config models.ConfigFile + yamlFile, err := os.ReadFile(filename) + if err != nil { + log.Error("Error opening config file!", "reason", err) + log.Info("Using defaults") + return models.ConfigFile{ + URL: "ws://localhost:8080/ws", + } + } + err = yaml.Unmarshal(yamlFile, &config) + if err != nil { + log.Fatalf("Unmarshal: %v", err) + } + return config + +} diff --git a/internal/websocket/send.go b/internal/websocket/send.go index df11a4b..dec081c 100644 --- a/internal/websocket/send.go +++ b/internal/websocket/send.go @@ -1,7 +1,6 @@ package websocket import ( - "ChronoTomato/config" "encoding/json" "git.smsvc.net/pomodoro/GoTomato/pkg/models" "github.com/charmbracelet/log" @@ -32,11 +31,11 @@ func SendCmd(conn *websocket.Conn, pwd string, cmd string) { sendClientCommand(conn, message) } -func Send_updateSettings(conn *websocket.Conn, pwd string) { +func Send_updateSettings(conn *websocket.Conn, pwd string, settings models.GoTomatoPomodoroConfig) { message := models.ClientCommand{ Command: "updateSettings", Password: pwd, - PomodoroSettings: config.PomodoroSettings, + PomodoroSettings: settings, } sendClientCommand(conn, message) diff --git a/pkg/models/configfile.go b/pkg/models/configfile.go new file mode 100644 index 0000000..28c3fa9 --- /dev/null +++ b/pkg/models/configfile.go @@ -0,0 +1,11 @@ +package models + +import ( + "git.smsvc.net/pomodoro/GoTomato/pkg/models" +) + +type ConfigFile struct { + URL string `yaml:"url"` + Password string `yaml:"password"` + PomodoroConfig models.GoTomatoPomodoroConfig `yaml:"config"` +}