Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add Beeep action to protobuf definitions and UI components #679

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ __debug_bin
cmd/backrest/backrest
*.exe
.DS_Store
.idea/
211 changes: 159 additions & 52 deletions gen/go/v1/config.pb.go

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/containrrr/shoutrrr v0.8.0
github.com/djherbis/buffer v1.2.0
github.com/djherbis/nio/v3 v3.0.1
github.com/gen2brain/beeep v0.0.0-20240516210008-9c006672e7f4
github.com/getlantern/systray v1.2.2
github.com/gitploy-io/cronexpr v0.2.2
github.com/gofrs/flock v0.12.1
Expand All @@ -24,12 +25,14 @@ require (
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/google/uuid v1.6.0
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/hectane/go-acl v0.0.0-20230122075934-ca0b05cb1adb
github.com/mattn/go-colorable v0.1.13
github.com/natefinch/atomic v1.0.1
github.com/ncruces/zenity v0.10.14
github.com/prometheus/client_golang v1.20.5
go.etcd.io/bbolt v1.3.11
go.uber.org/multierr v1.11.0
go.uber.org/zap v1.27.0
golang.org/x/crypto v0.29.0
golang.org/x/net v0.31.0
Expand Down Expand Up @@ -57,26 +60,28 @@ require (
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/go-toast/toast v0.0.0-20190211030409-01e6764cf0a4 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/josephspurrier/goversioninfo v1.4.1 // indirect
github.com/klauspost/compress v1.17.11 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d // indirect
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.60.1 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/randall77/makefat v0.0.0-20210315173500-7ddd0e42c844 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af // indirect
go.opentelemetry.io/otel v1.32.0 // indirect
go.opentelemetry.io/otel/metric v1.32.0 // indirect
go.opentelemetry.io/otel/trace v1.32.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/image v0.22.0 // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/sys v0.30.0 // indirect
golang.org/x/text v0.20.0 // indirect
golang.org/x/tools v0.26.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241113202542-65e8d215514f // indirect
Expand Down
14 changes: 12 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkp
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
github.com/gen2brain/beeep v0.0.0-20240516210008-9c006672e7f4 h1:ygs9POGDQpQGLJPlq4+0LBUmMBNox1N4JSpw+OETcvI=
github.com/gen2brain/beeep v0.0.0-20240516210008-9c006672e7f4/go.mod h1:0W7dI87PvXJ1Sjs0QPvWXKcQmNERY77e8l7GFhZB/s4=
github.com/getlantern/context v0.0.0-20190109183933-c447772a6520/go.mod h1:L+mq6/vvYHKjCX2oez0CgEAJmbq1fbb/oNJIWQkBybY=
github.com/getlantern/context v0.0.0-20220418194847-3d5e7a086201 h1:oEZYEpZo28Wdx+5FZo4aU7JFXu0WG/4wJWese5reQSA=
github.com/getlantern/context v0.0.0-20220418194847-3d5e7a086201/go.mod h1:Y9WZUHEb+mpra02CbQ/QczLUe6f0Dezxaw5DCJlJQGo=
Expand Down Expand Up @@ -60,6 +62,10 @@ github.com/go-stack/stack v1.8.1 h1:ntEHSVwIt7PNXNpgPmVfMrNhLtgjlmnZha2kOpuRiDw=
github.com/go-stack/stack v1.8.1/go.mod h1:dcoOX6HbPZSZptuspn9bctJ+N/CnF5gGygcUP3XYfe4=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
github.com/go-toast/toast v0.0.0-20190211030409-01e6764cf0a4 h1:qZNfIGkIANxGv/OqtnntR4DfOY2+BgwR60cAcu/i3SE=
github.com/go-toast/toast v0.0.0-20190211030409-01e6764cf0a4/go.mod h1:kW3HQ4UdaAyrUCSSDR4xUzBKW6O2iA4uHhk7AtyYp10=
github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=
github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gofrs/flock v0.12.1 h1:MTLVXXHf8ekldpJk3AKicLij9MdwOWkZ+a/jHHZby9E=
github.com/gofrs/flock v0.12.1/go.mod h1:9zxTsyu5xtJ9DK+1tFZyibEV7y3uwDxPPfbxeeHCoD0=
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
Expand Down Expand Up @@ -110,6 +116,8 @@ github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdh
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
github.com/ncruces/zenity v0.10.14 h1:OBFl7qfXcvsdo1NUEGxTlZvAakgWMqz9nG38TuiaGLI=
github.com/ncruces/zenity v0.10.14/go.mod h1:ZBW7uVe/Di3IcRYH0Br8X59pi+O6EPnNIOU66YHpOO4=
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d h1:VhgPp6v9qf9Agr/56bj7Y/xa04UccTW04VP0Qed4vnQ=
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d/go.mod h1:YUTz3bUH2ZwIWBy3CJBeOBEugqcmXREj14T+iG/4k4U=
github.com/onsi/ginkgo/v2 v2.9.2 h1:BA2GMJOtfGAfagzYtrAlufIP0lq6QERkFmHLMLPwFSU=
github.com/onsi/ginkgo/v2 v2.9.2/go.mod h1:WHcJJG2dIlcCqVfBAwUCrJxSPFb6v4azBwgxeMeDuts=
github.com/onsi/gomega v1.27.6 h1:ENqfyGeS5AX/rlXDd/ETokDz93u0YufY1Pgxuy/PvWE=
Expand Down Expand Up @@ -141,6 +149,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af h1:6yITBqGTE2lEeTPG04SN9W+iWHCRyHqlVYILiSXziwk=
github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af/go.mod h1:4F09kP5F+am0jAwlQLddpoMDM+iewkxxt6nxUQ5nq5o=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.etcd.io/bbolt v1.3.11 h1:yGEzV1wPz2yVCLsD8ZAiGHhHVlczyC9d1rP43/VCRJ0=
go.etcd.io/bbolt v1.3.11/go.mod h1:dksAq7YMXoljX0xu6VF5DMZGbhYYoLUalEiSySYAS4I=
Expand Down Expand Up @@ -192,8 +202,8 @@ golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down
67 changes: 67 additions & 0 deletions internal/hook/types/beeep.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package types

import (
"context"
"fmt"
v1 "github.com/garethgeorge/backrest/gen/go/v1"
"github.com/garethgeorge/backrest/internal/hook/hookutil"
"github.com/garethgeorge/backrest/internal/orchestrator/tasks"
"github.com/gen2brain/beeep"
"log"
"reflect"
)

type beeepHandler struct{}

func (beeepHandler) Execute(ctx context.Context, h *v1.Hook, vars interface{}, runner tasks.TaskRunner, event v1.Hook_Condition) error {
b := h.GetActionBeeep()

message, err := hookutil.RenderTemplateOrDefault(b.GetTemplate(), hookutil.DefaultTemplate, vars)
if err != nil {
return fmt.Errorf("template rendering: %w", err)
}

title, err := hookutil.RenderTemplateOrDefault(b.GetTitleTemplate(), "Backrest Event", vars)
if err != nil {
return fmt.Errorf("title template rendering: %w", err)
}

frequency := b.GetFrequency()
if frequency == 0 {
frequency = beeep.DefaultFreq
}

duration := b.GetDuration()
if duration == 0 {
duration = int32(beeep.DefaultDuration)
}

icon := b.GetIcon()
if icon == "" {
icon = "assets/information.png"
}

log.Printf("Sending beeep notification icon %s", icon)
err = beeep.Alert(title, message, icon)
if err != nil {
return fmt.Errorf("beeep: %w", err)
}

err = beeep.Beep(b.GetFrequency(), int(b.GetDuration()))
if err != nil {
return fmt.Errorf("beeep: %w", err)
}
return nil
}

func (beeepHandler) Name() string {
return "beeep"
}

func (beeepHandler) ActionType() reflect.Type {
return reflect.TypeOf(&v1.Hook_ActionBeeep{})
}

func init() {
DefaultRegistry().RegisterHandler(&beeepHandler{})
}
81 changes: 81 additions & 0 deletions internal/hook/types/beeep_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package types

import (
"context"
"testing"

v1 "github.com/garethgeorge/backrest/gen/go/v1"
"github.com/garethgeorge/backrest/internal/orchestrator/tasks"
"github.com/gen2brain/beeep"
)

type mockTaskRunner struct {
tasks.TaskRunner
}

func TestBeeepHandler_Execute(t *testing.T) {
tests := []struct {
name string
hook *v1.Hook
vars interface{}
wantErr bool
}{
{
name: "basic notification",
hook: &v1.Hook{
Action: &v1.Hook_ActionBeeep{
ActionBeeep: &v1.Hook_Beeep{
Template: "test message",
TitleTemplate: "test title",
Frequency: beeep.DefaultFreq,
Duration: int32(beeep.DefaultDuration),
Icon: "",
},
},
},
vars: map[string]interface{}{"test": "value"},
wantErr: false,
},
{
name: "custom frequency and duration",
hook: &v1.Hook{
Action: &v1.Hook_ActionBeeep{
ActionBeeep: &v1.Hook_Beeep{
Template: "test message",
TitleTemplate: "test title",
Frequency: 1000,
Duration: 2000,
Icon: "assets/information.png",
},
},
},
vars: map[string]interface{}{"test": "value"},
wantErr: false,
},
{
name: "with template variables",
hook: &v1.Hook{
Action: &v1.Hook_ActionBeeep{
ActionBeeep: &v1.Hook_Beeep{
Template: "{{.test}} message",
TitleTemplate: "{{.test}} title",
},
},
},
vars: map[string]interface{}{"test": "value"},
wantErr: false,
},
}

handler := &beeepHandler{}
runner := &mockTaskRunner{}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := handler.Execute(context.Background(), tt.hook, tt.vars, runner, v1.Hook_CONDITION_SNAPSHOT_SUCCESS)
if (err != nil) != tt.wantErr {
t.Errorf("Execute() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
9 changes: 9 additions & 0 deletions proto/v1/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ message Hook {
Slack action_slack = 104 [json_name="actionSlack"];
Shoutrrr action_shoutrrr = 105 [json_name="actionShoutrrr"];
Healthchecks action_healthchecks = 106 [json_name="actionHealthchecks"];
Beeep action_beeep = 107 [json_name="actionBeeep"];
}

message Command {
Expand Down Expand Up @@ -229,6 +230,14 @@ message Hook {
string webhook_url = 1 [json_name="webhookUrl"];
string template = 2 [json_name="template"];
}

message Beeep {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming note: it may make more sense to name this after the capability e.g. OsNotification or such rather than the library beeep on the off chance that we need to migrate in the future.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, I will update.

string template = 1 [json_name="template"];
string title_template = 2 [json_name="titleTemplate"];
double frequency = 3 [json_name="frequency"];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im somewhat ambivalent about accepting a frequency and duration , I wonder if most users mind or would rather have some opinionated default e.g. simply an enum.

I could see something like

enum DeliveryMode {
   SILENT,
   LOW_PITCH_CHIME,
   HIGH_PITCH_CHIME,
}

.

WDYT? are you finding the range of frequency control useful?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like setting the frequency and duration does not work. At leat not on macOS. Maybe we should reduce this to just SILENT / CHIME ?

int32 duration = 4 [json_name="duration"];
string icon = 5 [json_name="icon"];
}
}

message Auth {
Expand Down
Binary file added smart-commits
Binary file not shown.
Loading