package client import ( "flag" "fmt" "os" "git.smsvc.net/pomodoro/ChronoTomato/internal/helper" "git.smsvc.net/pomodoro/ChronoTomato/internal/metadata" "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 if client.LastErr != nil { helper.Logger.Fatal(client.LastErr) } go client.ProcessServerMessages(a.channel) return tea.Batch( tea.EnterAltScreen, 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") showVersion = flag.Bool("version", false, "Show Version") ) flag.Parse() if *showVersion { fmt.Println("ChronoTomato", metadata.ChronoTomatoVersion) os.Exit(0) } // 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) } }