-
Notifications
You must be signed in to change notification settings - Fork 0
/
appwrapper.go
77 lines (64 loc) · 2.07 KB
/
appwrapper.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// appwrapper executes applications and presents errors to the GUI as notifications
package main
import (
"bytes"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"github.com/godbus/dbus"
)
func main() {
if len(os.Args) < 2 {
log.Println("Argument missing")
os.Exit(1)
}
cmd := exec.Command(os.Args[1], os.Args[2:]...)
var out bytes.Buffer
cmd.Stderr = &out
if err := cmd.Start(); err != nil {
log.Fatalf("cmd.Start: %v", err)
}
if err := cmd.Wait(); err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
// The program has exited with an exit code != 0
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
log.Printf("Exit Status: %d", status.ExitStatus())
log.Println(out.String())
summary := "Error"
body := strings.TrimSpace(out.String())
if strings.Contains(out.String(), "cannot open shared object file: No such file or directory") == true {
parts := strings.Split(out.String(), ":")
summary = "Error: Missing library " + strings.TrimSpace(parts[2])
body = filepath.Base(os.Args[1]) + " could not be started because " + strings.TrimSpace(parts[2]) + " is missing"
}
sendDesktopNotification(summary, body)
}
} else {
log.Fatalf("cmd.Wait: %v", err)
}
}
}
// Send desktop notification. See
// https://developer.gnome.org/notification-spec/
func sendDesktopNotification(title string, body string) {
conn, err := dbus.SessionBus()
defer conn.Close()
if err != nil {
log.Println(os.Stderr, "Failed to connect to session bus:", err)
return
}
obj := conn.Object("org.freedesktop.Notifications", "/org/freedesktop/Notifications")
call := obj.Call("org.freedesktop.Notifications.Notify", 0, "", uint32(0),
"", title, body, []string{},
map[string]dbus.Variant{},
int32(0)) // The timeout time in milliseconds at which the notification should automatically close.
// If -1, the notification's expiration time is dependent on the notification server's settings,
// and may vary for the type of notification.
// If 0, the notification never expires.
if call.Err != nil {
panic(call.Err)
}
}