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") configfile = flag.String("config", defaultConfigFile, "Path to config file") ) flag.Parse() // read passed config file or try to use default config if *configfile != defaultConfigFile { config = helper.ParseConfig(*configfile) } else { if helper.FileExists(*configfile) { config = helper.ParseConfig(*configfile) } } // 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) } }