-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotificator.go
61 lines (52 loc) · 1.43 KB
/
notificator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"context"
"fmt"
"os/exec"
"runtime"
"strconv"
)
var _ Task = (*DesktopNotificator)(nil)
// DesktopNotificator implements Task
type DesktopNotificator struct {
transient bool
expireTime string
}
// NewDesktopNotificator constructs task
func NewDesktopNotificator(transient bool, expireInMillisecond int) *DesktopNotificator {
notifier := &DesktopNotificator{}
notifier.transient = transient
notifier.expireTime = strconv.Itoa(expireInMillisecond)
return notifier
}
// ID of the task
func (n *DesktopNotificator) ID() string {
return "DesktopNotificator"
}
// Run implements Task interface
func (n *DesktopNotificator) Run(ctx context.Context) (string, error) {
in := ctx.Value(prevTaskOutputKey).(string)
return in, n.Send(ctx, in)
}
// Send desktop notification
func (n *DesktopNotificator) Send(ctx context.Context, msg string) error {
var cmd *exec.Cmd
switch runtime.GOOS {
case "darwin":
cmd = exec.CommandContext(ctx, "osascript", "-e",
fmt.Sprintf("display notification \"%s\" with title \"%s\"", msg, msg))
case "linux":
cmd = exec.CommandContext(ctx, "notify-send", "-t", n.expireTime)
if n.transient {
cmd.Args = append(cmd.Args, []string{"--hint", "int:transient:1"}...)
}
cmd.Args = append(cmd.Args, msg)
default:
return fmt.Errorf("unsupported os %s", runtime.GOOS)
}
err := cmd.Run()
if err != nil {
return fmt.Errorf("Desktop notification error %v", err)
}
return nil
}