Sebastian Mark
1dd42d3c14
- rename configfile parameter to parameter_configfile
- update references to the configfile parameter
🤖
91 lines
2.2 KiB
Go
91 lines
2.2 KiB
Go
package client
|
|
|
|
import (
|
|
"flag"
|
|
|
|
"git.smsvc.net/pomodoro/ChronoTomato/internal/helper"
|
|
"git.smsvc.net/pomodoro/ChronoTomato/internal/websocket"
|
|
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
|
|
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
|
|
|
"github.com/charmbracelet/bubbles/help"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
)
|
|
|
|
var (
|
|
config ChronoTomato.Config
|
|
client websocket.Client
|
|
)
|
|
|
|
type app struct {
|
|
keys keyMap
|
|
help help.Model
|
|
channel chan GoTomato.ServerMessage
|
|
pomodoro GoTomato.ServerMessage
|
|
}
|
|
|
|
func myApp() app {
|
|
return app{
|
|
keys: keys,
|
|
help: help.New(),
|
|
channel: make(chan GoTomato.ServerMessage),
|
|
pomodoro: GoTomato.ServerMessage{},
|
|
}
|
|
}
|
|
|
|
// Wait for channel signal and return the ServerMessage as a tea.Msg.
|
|
// Return tea.Quit() if the channel has been closed.
|
|
// Excapsulate all this in a tea.Cmd() because bubbletea demands it.
|
|
func (a app) waitForChannelSignal() tea.Cmd {
|
|
return func() tea.Msg {
|
|
content, open := <-a.channel
|
|
if !open {
|
|
return tea.Quit()
|
|
}
|
|
return content
|
|
}
|
|
}
|
|
|
|
func (a app) Init() tea.Cmd {
|
|
client = websocket.Connect(config.URL)
|
|
client.Password = config.Password
|
|
go client.ProcessServerMessages(a.channel)
|
|
|
|
return tea.Batch(
|
|
tea.ClearScreen,
|
|
a.waitForChannelSignal(),
|
|
)
|
|
}
|
|
|
|
func Start() {
|
|
var (
|
|
defaultConfigFile = "~/.config/ChronoTomato.yml"
|
|
|
|
parameter_url = flag.String("url", "", "GoTomato Server URL (eg ws://localhost:8080/ws)")
|
|
parameter_password = flag.String("password", "", "Control password for pomodoro session")
|
|
parameter_configfile = flag.String("config", defaultConfigFile, "Path to config file")
|
|
)
|
|
flag.Parse()
|
|
|
|
// read passed config file or try to use default config
|
|
if *parameter_configfile != defaultConfigFile {
|
|
config = helper.ParseConfig(*parameter_configfile)
|
|
} else {
|
|
if helper.FileExists(defaultConfigFile) {
|
|
config = helper.ParseConfig(defaultConfigFile)
|
|
}
|
|
}
|
|
|
|
// cli parameters always supersede config file
|
|
if *parameter_url != "" {
|
|
config.URL = *parameter_url
|
|
}
|
|
if *parameter_password != "" {
|
|
config.Password = *parameter_password
|
|
}
|
|
|
|
_, err := tea.NewProgram(myApp()).Run()
|
|
if err != nil {
|
|
helper.Logger.Fatal("Could not start program:", "msg", err)
|
|
}
|
|
}
|