Compare commits
5 commits
aaad07bc77
...
36dca57830
Author | SHA1 | Date | |
---|---|---|---|
36dca57830 | |||
10021eb65e | |||
09dbf35a6a | |||
58d658be66 | |||
05e8bf5854 |
10 changed files with 168 additions and 39 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1 +1,3 @@
|
||||||
ChronoTomato
|
ChronoTomato
|
||||||
|
|
||||||
|
dist/
|
||||||
|
|
40
.goreleaser.yaml
Normal file
40
.goreleaser.yaml
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
# vim: set ts=2 sw=2 tw=0 fo=cnqoj
|
||||||
|
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
|
||||||
|
|
||||||
|
version: 2
|
||||||
|
|
||||||
|
before:
|
||||||
|
hooks:
|
||||||
|
- go mod tidy
|
||||||
|
|
||||||
|
builds:
|
||||||
|
- goos:
|
||||||
|
- linux
|
||||||
|
goarch:
|
||||||
|
- amd64
|
||||||
|
- arm64
|
||||||
|
env:
|
||||||
|
- CGO_ENABLED=0
|
||||||
|
ldflags:
|
||||||
|
- -s -w -X {{.ModulePath}}/internal/metadata.ChronoTomatoVersion={{.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
|
34
Taskfile.yml
Normal file
34
Taskfile.yml
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
# yaml-language-server: $schema=https://taskfile.dev/schema.json
|
||||||
|
#
|
||||||
|
# github.com/go-task/task/v3/cmd/task@latest
|
||||||
|
# Requirements:
|
||||||
|
# github.com/caarlos0/svu@latest
|
||||||
|
# github.com/goreleaser/goreleaser/v2@latest
|
||||||
|
|
||||||
|
version: '3'
|
||||||
|
|
||||||
|
tasks:
|
||||||
|
|
||||||
|
release:
|
||||||
|
desc: Create and publish an new release
|
||||||
|
vars:
|
||||||
|
RELEASE:
|
||||||
|
sh: svu next
|
||||||
|
BRANCH:
|
||||||
|
sh: git branch --show-current
|
||||||
|
COMMIT:
|
||||||
|
sh: git rev-parse --short --verify {{.BRANCH}}
|
||||||
|
preconditions:
|
||||||
|
- sh: test "{{.BRANCH}}" == "main"
|
||||||
|
msg: "You must be on the main branch to release"
|
||||||
|
prompt: Create new release {{.RELEASE}} from {{.COMMIT}}@{{.BRANCH}}?
|
||||||
|
cmds:
|
||||||
|
- git tag {{.RELEASE}}
|
||||||
|
- git push
|
||||||
|
- git push origin tag {{.RELEASE}}
|
||||||
|
- goreleaser release --clean
|
||||||
|
|
||||||
|
snapshot:
|
||||||
|
desc: Create a local snapshot release
|
||||||
|
cmds:
|
||||||
|
- goreleaser release --clean --snapshot
|
|
@ -2,8 +2,11 @@ package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
"git.smsvc.net/pomodoro/ChronoTomato/internal/helper"
|
"git.smsvc.net/pomodoro/ChronoTomato/internal/helper"
|
||||||
|
"git.smsvc.net/pomodoro/ChronoTomato/internal/metadata"
|
||||||
"git.smsvc.net/pomodoro/ChronoTomato/internal/websocket"
|
"git.smsvc.net/pomodoro/ChronoTomato/internal/websocket"
|
||||||
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
|
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
|
||||||
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||||
|
@ -49,10 +52,14 @@ func (a app) waitForChannelSignal() tea.Cmd {
|
||||||
func (a app) Init() tea.Cmd {
|
func (a app) Init() tea.Cmd {
|
||||||
client = websocket.Connect(config.URL)
|
client = websocket.Connect(config.URL)
|
||||||
client.Password = config.Password
|
client.Password = config.Password
|
||||||
|
if client.LastErr != nil {
|
||||||
|
helper.Logger.Fatal(client.LastErr)
|
||||||
|
}
|
||||||
|
|
||||||
go client.ProcessServerMessages(a.channel)
|
go client.ProcessServerMessages(a.channel)
|
||||||
|
|
||||||
return tea.Batch(
|
return tea.Batch(
|
||||||
tea.ClearScreen,
|
tea.EnterAltScreen,
|
||||||
a.waitForChannelSignal(),
|
a.waitForChannelSignal(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -64,9 +71,15 @@ func Start() {
|
||||||
parameter_url = flag.String("url", "", "GoTomato Server URL (eg ws://localhost:8080/ws)")
|
parameter_url = flag.String("url", "", "GoTomato Server URL (eg ws://localhost:8080/ws)")
|
||||||
parameter_password = flag.String("password", "", "Control password for pomodoro session")
|
parameter_password = flag.String("password", "", "Control password for pomodoro session")
|
||||||
parameter_configfile = flag.String("config", defaultConfigFile, "Path to config file")
|
parameter_configfile = flag.String("config", defaultConfigFile, "Path to config file")
|
||||||
|
showVersion = flag.Bool("version", false, "Show Version")
|
||||||
)
|
)
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
if *showVersion {
|
||||||
|
fmt.Println("ChronoTomato", metadata.ChronoTomatoVersion)
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
// read passed config file or try to use default config
|
// read passed config file or try to use default config
|
||||||
if *parameter_configfile != defaultConfigFile {
|
if *parameter_configfile != defaultConfigFile {
|
||||||
config = helper.ParseConfig(*parameter_configfile)
|
config = helper.ParseConfig(*parameter_configfile)
|
||||||
|
|
|
@ -3,22 +3,29 @@ package client
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/alecthomas/colour"
|
||||||
|
|
||||||
"git.smsvc.net/pomodoro/ChronoTomato/cmd/client/helper"
|
"git.smsvc.net/pomodoro/ChronoTomato/cmd/client/helper"
|
||||||
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (a app) View() string {
|
func (a app) View() string {
|
||||||
var body string
|
var body string
|
||||||
|
var serverStatus string
|
||||||
|
|
||||||
|
body = "Waiting for server message..."
|
||||||
if a.pomodoro != (GoTomato.ServerMessage{}) {
|
if a.pomodoro != (GoTomato.ServerMessage{}) {
|
||||||
body = helper.TerminalOutput(a.pomodoro)
|
body = helper.TerminalOutput(a.pomodoro)
|
||||||
helper.DesktopNotifications(a.pomodoro)
|
helper.DesktopNotifications(a.pomodoro)
|
||||||
} else {
|
}
|
||||||
body = "Waiting for first server message..."
|
|
||||||
|
serverStatus = colour.Sprintf("^D^1disconnected: %v^R\n", client.LastErr)
|
||||||
|
if client.Connected() {
|
||||||
|
serverStatus = colour.Sprintf("^D^2connected to %s^R\n", client.Server)
|
||||||
}
|
}
|
||||||
|
|
||||||
helpView := a.help.View(a.keys)
|
helpView := a.help.View(a.keys)
|
||||||
height := 8 - strings.Count(body, "\n") - strings.Count(helpView, "\n")
|
height := 8 - strings.Count(body, "\n") - strings.Count(serverStatus, "\n") - strings.Count(helpView, "\n")
|
||||||
|
|
||||||
return body + strings.Repeat("\n", height) + helpView
|
return body + strings.Repeat("\n", height) + serverStatus + helpView
|
||||||
}
|
}
|
||||||
|
|
29
internal/metadata/version.go
Normal file
29
internal/metadata/version.go
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
package metadata
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os/exec"
|
||||||
|
"runtime/debug"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var ChronoTomatoVersion = ""
|
||||||
|
|
||||||
|
func getLatestTag() string {
|
||||||
|
bytes, _ := exec.Command("git", "describe", "--tags").Output()
|
||||||
|
output := strings.TrimSpace(string(bytes))
|
||||||
|
return output
|
||||||
|
}
|
||||||
|
|
||||||
|
// set GoTomatoVersion from runtime/debug.Main.Version
|
||||||
|
// use latest git tag as fallback
|
||||||
|
// can be overwritten via ldflags (e,g. by goreleaser)
|
||||||
|
func init() {
|
||||||
|
if ChronoTomatoVersion == "" {
|
||||||
|
info, _ := debug.ReadBuildInfo()
|
||||||
|
if info.Main.Version != "(devel)" {
|
||||||
|
ChronoTomatoVersion = info.Main.Version
|
||||||
|
} else {
|
||||||
|
ChronoTomatoVersion = getLatestTag()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.smsvc.net/pomodoro/ChronoTomato/internal/helper"
|
|
||||||
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
|
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -13,30 +12,24 @@ type Client ChronoTomato.GoTomatoClient // New websocket client
|
||||||
// Connects to websocket
|
// Connects to websocket
|
||||||
func Connect(url string) Client {
|
func Connect(url string) Client {
|
||||||
conn, _, err := websocket.DefaultDialer.Dial(url, nil)
|
conn, _, err := websocket.DefaultDialer.Dial(url, nil)
|
||||||
if err != nil {
|
|
||||||
helper.Logger.Fatal("Dial error!", "reason", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
helper.Logger.Info("Connected ó°–ź ", "host", url)
|
return Client{Conn: conn, Server: url, LastErr: err}
|
||||||
time.Sleep(time.Second)
|
}
|
||||||
|
|
||||||
return Client{Conn: conn}
|
func (c Client) Connected() bool {
|
||||||
|
return c.Conn != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Disconnects from websocket
|
// Disconnects from websocket
|
||||||
func (c Client) Disconnect() {
|
func (c Client) Disconnect() {
|
||||||
select {
|
// Cleanly close the connection by sending a close message and then
|
||||||
case <-Done:
|
// waiting (with timeout) for the server to close the connection.
|
||||||
// session closed by remote
|
if c.Connected() {
|
||||||
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, ""))
|
c.Conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
|
||||||
select {
|
select {
|
||||||
case <-Done:
|
case <-Done:
|
||||||
case <-time.After(time.Second):
|
case <-time.After(time.Second):
|
||||||
}
|
}
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,10 @@ package websocket
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
|
|
||||||
"git.smsvc.net/pomodoro/ChronoTomato/internal/helper"
|
|
||||||
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -12,30 +13,35 @@ var Done = make(chan struct{})
|
||||||
|
|
||||||
// Receives websocket messages and writes them to a channel.
|
// Receives websocket messages and writes them to a channel.
|
||||||
// Closes the channel if websocket closes.
|
// Closes the channel if websocket closes.
|
||||||
func (c Client) ProcessServerMessages(channel chan<- GoTomato.ServerMessage) {
|
func (c *Client) ProcessServerMessages(channel chan<- GoTomato.ServerMessage) {
|
||||||
var serverMessage GoTomato.ServerMessage
|
var serverMessage, prevMessage GoTomato.ServerMessage
|
||||||
|
|
||||||
defer close(Done)
|
defer close(Done)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
_, message, err := c.Conn.ReadMessage()
|
_, message, err := c.Conn.ReadMessage()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
// On normal closure exit gracefully
|
||||||
if websocket.IsCloseError(err, websocket.CloseNormalClosure) {
|
if websocket.IsCloseError(err, websocket.CloseNormalClosure) {
|
||||||
// Ignore normal closure and exit gracefully
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Log any other errors
|
|
||||||
helper.Logger.Error("Read error!", "reason", err)
|
|
||||||
close(channel)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err = json.Unmarshal(message, &serverMessage)
|
// Try to reconnect on unexpected disconnect
|
||||||
if err != nil {
|
for {
|
||||||
helper.Logger.Error("Error unmarshalling!", "reason", err)
|
channel <- prevMessage // send previous ServerMessage to update view
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
*c = Connect(c.Server)
|
||||||
|
if c.Connected() {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = json.Unmarshal(message, &serverMessage)
|
||||||
|
c.LastErr = err
|
||||||
|
|
||||||
channel <- serverMessage
|
channel <- serverMessage
|
||||||
|
prevMessage = serverMessage
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,27 +4,30 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
|
|
||||||
"git.smsvc.net/pomodoro/ChronoTomato/internal/helper"
|
|
||||||
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Sends a message to the clients websocket
|
// Sends a message to the clients websocket
|
||||||
func sendClientCommand(c Client, msg GoTomato.ClientCommand) {
|
func sendClientCommand(c *Client, msg GoTomato.ClientCommand) {
|
||||||
|
if !c.Connected() {
|
||||||
|
// noop if client is not connected
|
||||||
|
return
|
||||||
|
}
|
||||||
messageBytes, err := json.Marshal(msg)
|
messageBytes, err := json.Marshal(msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
helper.Logger.Error("Error marshalling!", "reason", err)
|
c.LastErr = err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = c.Conn.WriteMessage(websocket.TextMessage, messageBytes)
|
err = c.Conn.WriteMessage(websocket.TextMessage, messageBytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
helper.Logger.Error("Write error!", "reason", err)
|
c.LastErr = err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sends the passed command to the server
|
// Sends the passed command to the server
|
||||||
func (c Client) SendCmd(cmd string) {
|
func (c *Client) SendCmd(cmd string) {
|
||||||
message := GoTomato.ClientCommand{
|
message := GoTomato.ClientCommand{
|
||||||
Command: cmd,
|
Command: cmd,
|
||||||
Password: c.Password,
|
Password: c.Password,
|
||||||
|
@ -34,7 +37,7 @@ func (c Client) SendCmd(cmd string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sends the new PomodoroConfig to the server
|
// Sends the new PomodoroConfig to the server
|
||||||
func (c Client) SendSettingsUpdate(settings GoTomato.PomodoroConfig) {
|
func (c *Client) SendSettingsUpdate(settings GoTomato.PomodoroConfig) {
|
||||||
message := GoTomato.ClientCommand{
|
message := GoTomato.ClientCommand{
|
||||||
Command: "updateSettings",
|
Command: "updateSettings",
|
||||||
Password: c.Password,
|
Password: c.Password,
|
||||||
|
|
|
@ -4,6 +4,8 @@ import "github.com/gorilla/websocket"
|
||||||
|
|
||||||
// Represents a GoTomato client
|
// Represents a GoTomato client
|
||||||
type GoTomatoClient struct {
|
type GoTomatoClient struct {
|
||||||
Conn *websocket.Conn // The websocket connection of the client
|
Server string // The GoTomato server
|
||||||
Password string // Pomodoro password
|
Password string // Pomodoro password
|
||||||
|
Conn *websocket.Conn // The websocket connection of the client
|
||||||
|
LastErr error // Last client error
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue