Compare commits
No commits in common. "9247df089100b694daf701b3717e52f3ae4343e1" and "1dd42d3c14fd8ecc109aa55692c571778233a96e" have entirely different histories.
9247df0891
...
1dd42d3c14
8 changed files with 41 additions and 107 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,3 +1 @@
|
|||
ChronoTomato
|
||||
|
||||
dist/
|
||||
|
|
|
@ -1,40 +0,0 @@
|
|||
# vim: set ts=2 sw=2 tw=0 fo=cnqoj
|
||||
|
||||
version: 2
|
||||
|
||||
before:
|
||||
hooks:
|
||||
- rm -fr ./dist
|
||||
- go mod tidy
|
||||
|
||||
builds:
|
||||
- goos:
|
||||
- linux
|
||||
goarch:
|
||||
- amd64
|
||||
- arm64
|
||||
env:
|
||||
- CGO_ENABLED=0
|
||||
ldflags:
|
||||
- -s -w -X git.smsvc.net/pomodoro/ChronoTomato/cmd/client.version={{.Version}}
|
||||
|
||||
upx:
|
||||
- enabled: true
|
||||
compress: best
|
||||
lzma: true
|
||||
|
||||
changelog:
|
||||
use: gitea
|
||||
|
||||
archives:
|
||||
- format: binary
|
||||
|
||||
release:
|
||||
gitea:
|
||||
owner: pomodoro
|
||||
name: ChronoTomato
|
||||
mode: replace
|
||||
|
||||
gitea_urls:
|
||||
download: http://git.smsvc.net
|
||||
api: http://git.smsvc.net/api/v1
|
|
@ -2,8 +2,6 @@ package client
|
|||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"git.smsvc.net/pomodoro/ChronoTomato/internal/helper"
|
||||
"git.smsvc.net/pomodoro/ChronoTomato/internal/websocket"
|
||||
|
@ -15,9 +13,8 @@ import (
|
|||
)
|
||||
|
||||
var (
|
||||
version = "devel"
|
||||
config ChronoTomato.Config
|
||||
client websocket.Client
|
||||
config ChronoTomato.Config
|
||||
client websocket.Client
|
||||
)
|
||||
|
||||
type app struct {
|
||||
|
@ -52,14 +49,10 @@ func (a app) waitForChannelSignal() tea.Cmd {
|
|||
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,
|
||||
tea.ClearScreen,
|
||||
a.waitForChannelSignal(),
|
||||
)
|
||||
}
|
||||
|
@ -71,15 +64,9 @@ func Start() {
|
|||
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", version)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
// read passed config file or try to use default config
|
||||
if *parameter_configfile != defaultConfigFile {
|
||||
config = helper.ParseConfig(*parameter_configfile)
|
||||
|
|
|
@ -3,29 +3,22 @@ package client
|
|||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/alecthomas/colour"
|
||||
|
||||
"git.smsvc.net/pomodoro/ChronoTomato/cmd/client/helper"
|
||||
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||
)
|
||||
|
||||
func (a app) View() string {
|
||||
var body string
|
||||
var serverStatus string
|
||||
|
||||
body = "Waiting for server message..."
|
||||
if a.pomodoro != (GoTomato.ServerMessage{}) {
|
||||
body = helper.TerminalOutput(a.pomodoro)
|
||||
helper.DesktopNotifications(a.pomodoro)
|
||||
}
|
||||
|
||||
serverStatus = colour.Sprintf("^D^1disconnected: %v^R\n", client.LastErr)
|
||||
if client.Connected() {
|
||||
serverStatus = colour.Sprintf("^D^2connected to %s^R\n", client.Server)
|
||||
} else {
|
||||
body = "Waiting for first server message..."
|
||||
}
|
||||
|
||||
helpView := a.help.View(a.keys)
|
||||
height := 8 - strings.Count(body, "\n") - strings.Count(serverStatus, "\n") - strings.Count(helpView, "\n")
|
||||
height := 8 - strings.Count(body, "\n") - strings.Count(helpView, "\n")
|
||||
|
||||
return body + strings.Repeat("\n", height) + serverStatus + helpView
|
||||
return body + strings.Repeat("\n", height) + helpView
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"github.com/gorilla/websocket"
|
||||
"time"
|
||||
|
||||
"git.smsvc.net/pomodoro/ChronoTomato/internal/helper"
|
||||
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
|
||||
)
|
||||
|
||||
|
@ -12,24 +13,30 @@ type Client ChronoTomato.GoTomatoClient // New websocket client
|
|||
// Connects to websocket
|
||||
func Connect(url string) Client {
|
||||
conn, _, err := websocket.DefaultDialer.Dial(url, nil)
|
||||
if err != nil {
|
||||
helper.Logger.Fatal("Dial error!", "reason", err)
|
||||
}
|
||||
|
||||
return Client{Conn: conn, Server: url, LastErr: err}
|
||||
}
|
||||
helper.Logger.Info("Connected ó°–ź ", "host", url)
|
||||
time.Sleep(time.Second)
|
||||
|
||||
func (c Client) Connected() bool {
|
||||
return c.Conn != nil
|
||||
return Client{Conn: conn}
|
||||
}
|
||||
|
||||
// Disconnects from websocket
|
||||
func (c Client) Disconnect() {
|
||||
// Cleanly close the connection by sending a close message and then
|
||||
// waiting (with timeout) for the server to close the connection.
|
||||
if c.Connected() {
|
||||
select {
|
||||
case <-Done:
|
||||
// session closed by remote
|
||||
return
|
||||
default:
|
||||
// Cleanly close the connection by sending a close message and then
|
||||
// waiting (with timeout) for the server to close the connection.
|
||||
c.Conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
|
||||
select {
|
||||
case <-Done:
|
||||
case <-time.After(time.Second):
|
||||
}
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
|
@ -2,10 +2,9 @@ package websocket
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
|
||||
"git.smsvc.net/pomodoro/ChronoTomato/internal/helper"
|
||||
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||
)
|
||||
|
||||
|
@ -13,35 +12,30 @@ var Done = make(chan struct{})
|
|||
|
||||
// Receives websocket messages and writes them to a channel.
|
||||
// Closes the channel if websocket closes.
|
||||
func (c *Client) ProcessServerMessages(channel chan<- GoTomato.ServerMessage) {
|
||||
var serverMessage, prevMessage GoTomato.ServerMessage
|
||||
func (c Client) ProcessServerMessages(channel chan<- GoTomato.ServerMessage) {
|
||||
var serverMessage GoTomato.ServerMessage
|
||||
|
||||
defer close(Done)
|
||||
|
||||
for {
|
||||
_, message, err := c.Conn.ReadMessage()
|
||||
if err != nil {
|
||||
// On normal closure exit gracefully
|
||||
if websocket.IsCloseError(err, websocket.CloseNormalClosure) {
|
||||
// Ignore normal closure and exit gracefully
|
||||
return
|
||||
}
|
||||
|
||||
// Try to reconnect on unexpected disconnect
|
||||
for {
|
||||
channel <- prevMessage // send previous ServerMessage to update view
|
||||
time.Sleep(time.Second)
|
||||
*c = Connect(c.Server)
|
||||
if c.Connected() {
|
||||
break
|
||||
}
|
||||
}
|
||||
continue
|
||||
// Log any other errors
|
||||
helper.Logger.Error("Read error!", "reason", err)
|
||||
close(channel)
|
||||
return
|
||||
}
|
||||
|
||||
err = json.Unmarshal(message, &serverMessage)
|
||||
c.LastErr = err
|
||||
if err != nil {
|
||||
helper.Logger.Error("Error unmarshalling!", "reason", err)
|
||||
continue
|
||||
}
|
||||
|
||||
channel <- serverMessage
|
||||
prevMessage = serverMessage
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,30 +4,27 @@ import (
|
|||
"encoding/json"
|
||||
"github.com/gorilla/websocket"
|
||||
|
||||
"git.smsvc.net/pomodoro/ChronoTomato/internal/helper"
|
||||
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||
)
|
||||
|
||||
// Sends a message to the clients websocket
|
||||
func sendClientCommand(c *Client, msg GoTomato.ClientCommand) {
|
||||
if !c.Connected() {
|
||||
// noop if client is not connected
|
||||
return
|
||||
}
|
||||
func sendClientCommand(c Client, msg GoTomato.ClientCommand) {
|
||||
messageBytes, err := json.Marshal(msg)
|
||||
if err != nil {
|
||||
c.LastErr = err
|
||||
helper.Logger.Error("Error marshalling!", "reason", err)
|
||||
return
|
||||
}
|
||||
|
||||
err = c.Conn.WriteMessage(websocket.TextMessage, messageBytes)
|
||||
if err != nil {
|
||||
c.LastErr = err
|
||||
helper.Logger.Error("Write error!", "reason", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Sends the passed command to the server
|
||||
func (c *Client) SendCmd(cmd string) {
|
||||
func (c Client) SendCmd(cmd string) {
|
||||
message := GoTomato.ClientCommand{
|
||||
Command: cmd,
|
||||
Password: c.Password,
|
||||
|
@ -37,7 +34,7 @@ func (c *Client) SendCmd(cmd string) {
|
|||
}
|
||||
|
||||
// Sends the new PomodoroConfig to the server
|
||||
func (c *Client) SendSettingsUpdate(settings GoTomato.PomodoroConfig) {
|
||||
func (c Client) SendSettingsUpdate(settings GoTomato.PomodoroConfig) {
|
||||
message := GoTomato.ClientCommand{
|
||||
Command: "updateSettings",
|
||||
Password: c.Password,
|
||||
|
|
|
@ -4,8 +4,6 @@ import "github.com/gorilla/websocket"
|
|||
|
||||
// Represents a GoTomato client
|
||||
type GoTomatoClient struct {
|
||||
Server string // The GoTomato server
|
||||
Password string // Pomodoro password
|
||||
Conn *websocket.Conn // The websocket connection of the client
|
||||
LastErr error // Last client error
|
||||
Password string // Pomodoro password
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue