feat: add configuration file handling
- 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
🤖
This commit is contained in:
parent
25e939f523
commit
bb790459c1
7 changed files with 69 additions and 26 deletions
|
@ -5,10 +5,13 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
|
||||||
|
"ChronoTomato/internal/helper"
|
||||||
"ChronoTomato/internal/shared"
|
"ChronoTomato/internal/shared"
|
||||||
"ChronoTomato/internal/websocket"
|
"ChronoTomato/internal/websocket"
|
||||||
|
|
||||||
"atomicgo.dev/keyboard"
|
"atomicgo.dev/keyboard"
|
||||||
"atomicgo.dev/keyboard/keys"
|
"atomicgo.dev/keyboard/keys"
|
||||||
|
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
var interrupt = make(chan os.Signal, 1)
|
var interrupt = make(chan os.Signal, 1)
|
||||||
|
@ -16,16 +19,27 @@ var interrupt = make(chan os.Signal, 1)
|
||||||
func Start() {
|
func Start() {
|
||||||
signal.Notify(interrupt, os.Interrupt)
|
signal.Notify(interrupt, os.Interrupt)
|
||||||
|
|
||||||
GoTomatoUrl := flag.String("url", "ws://localhost:8080/ws", "GoTomato Server URL")
|
parameter_url := flag.String("url", "", "GoTomato Server URL (eg ws://localhost:8080/ws)")
|
||||||
password := flag.String("password", "", "Control password for pomodoro session (optional)")
|
parameter_password := flag.String("password", "", "Control password for pomodoro session (optional)")
|
||||||
autoStart := flag.Bool("start", false, "Immediately start a Pomodoro")
|
configfile := flag.String("config", "~/.config/ChronoTomato.yml", "path to config file (optional)")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
conn := websocket.Connect(*GoTomatoUrl)
|
config := helper.ParseConfig(*configfile)
|
||||||
|
|
||||||
websocket.Send_updateSettings(conn, *password)
|
url := *parameter_url
|
||||||
if *autoStart {
|
if url == "" {
|
||||||
websocket.SendCmd(conn, *password, "start")
|
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)
|
go websocket.ProcessServerMessages(conn)
|
||||||
|
@ -35,18 +49,18 @@ func Start() {
|
||||||
switch key.String() {
|
switch key.String() {
|
||||||
case "space":
|
case "space":
|
||||||
if !pomodoro.Ongoing {
|
if !pomodoro.Ongoing {
|
||||||
websocket.SendCmd(conn, *password, "start")
|
websocket.SendCmd(conn, password, "start")
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
if pomodoro.Paused {
|
if pomodoro.Paused {
|
||||||
websocket.SendCmd(conn, *password, "resume")
|
websocket.SendCmd(conn, password, "resume")
|
||||||
return false, nil
|
return false, nil
|
||||||
} else {
|
} else {
|
||||||
websocket.SendCmd(conn, *password, "pause")
|
websocket.SendCmd(conn, password, "pause")
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
case "r":
|
case "r":
|
||||||
websocket.SendCmd(conn, *password, "stop")
|
websocket.SendCmd(conn, password, "stop")
|
||||||
return false, nil
|
return false, nil
|
||||||
case "q":
|
case "q":
|
||||||
interrupt <- os.Interrupt
|
interrupt <- os.Interrupt
|
||||||
|
|
|
@ -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,
|
|
||||||
}
|
|
1
go.mod
1
go.mod
|
@ -10,6 +10,7 @@ require (
|
||||||
github.com/fatih/color v1.18.0
|
github.com/fatih/color v1.18.0
|
||||||
github.com/gen2brain/beeep v0.0.0-20240516210008-9c006672e7f4
|
github.com/gen2brain/beeep v0.0.0-20240516210008-9c006672e7f4
|
||||||
github.com/gorilla/websocket v1.5.3
|
github.com/gorilla/websocket v1.5.3
|
||||||
|
gopkg.in/yaml.v2 v2.2.4
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
|
4
go.sum
4
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.10/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c=
|
||||||
github.com/klauspost/cpuid/v2 v2.0.12 h1:p9dKCg8i4gmOxtv35DvrYoWqYzQrvEVdjQ762Y0OqZE=
|
github.com/klauspost/cpuid/v2 v2.0.12 h1:p9dKCg8i4gmOxtv35DvrYoWqYzQrvEVdjQ762Y0OqZE=
|
||||||
github.com/klauspost/cpuid/v2 v2.0.12/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c=
|
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/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
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/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 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
|
||||||
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
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 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
|
||||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
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 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/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.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.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-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|
26
internal/helper/config.go
Normal file
26
internal/helper/config.go
Normal file
|
@ -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
|
||||||
|
|
||||||
|
}
|
|
@ -1,7 +1,6 @@
|
||||||
package websocket
|
package websocket
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"ChronoTomato/config"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||||
"github.com/charmbracelet/log"
|
"github.com/charmbracelet/log"
|
||||||
|
@ -32,11 +31,11 @@ func SendCmd(conn *websocket.Conn, pwd string, cmd string) {
|
||||||
sendClientCommand(conn, message)
|
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{
|
message := models.ClientCommand{
|
||||||
Command: "updateSettings",
|
Command: "updateSettings",
|
||||||
Password: pwd,
|
Password: pwd,
|
||||||
PomodoroSettings: config.PomodoroSettings,
|
PomodoroSettings: settings,
|
||||||
}
|
}
|
||||||
|
|
||||||
sendClientCommand(conn, message)
|
sendClientCommand(conn, message)
|
||||||
|
|
11
pkg/models/configfile.go
Normal file
11
pkg/models/configfile.go
Normal file
|
@ -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"`
|
||||||
|
}
|
Loading…
Reference in a new issue