-
-
Notifications
You must be signed in to change notification settings - Fork 69
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ __debug_bin | |
cmd/backrest/backrest | ||
*.exe | ||
.DS_Store | ||
.idea/ |
Large diffs are not rendered by default.
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{}) | ||
} |
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) | ||
} | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -229,6 +230,14 @@ message Hook { | |
string webhook_url = 1 [json_name="webhookUrl"]; | ||
string template = 2 [json_name="template"]; | ||
} | ||
|
||
message Beeep { | ||
string template = 1 [json_name="template"]; | ||
string title_template = 2 [json_name="titleTemplate"]; | ||
double frequency = 3 [json_name="frequency"]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
. WDYT? are you finding the range of frequency control useful? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree, I will update.