forked from goliatone/dnd-macos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
92 lines (74 loc) · 2.08 KB
/
main.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"flag"
"fmt"
"os"
"os/exec"
)
const (
enableFlag = "enable"
disableFlag = "disable"
killall = "killall NotificationCenter"
setDoNotDisturb = "defaults -currentHost write ~/Library/Preferences/ByHost/com.apple.notificationcenterui doNotDisturb -boolean true"
setDoNotDisturbDate = "defaults -currentHost write ~/Library/Preferences/ByHost/com.apple.notificationcenterui doNotDisturbDate -date \"`date -u +\"%Y-%m-%d %H:%M:%S +000\"`\""
unsetDoNotDisturb = "defaults -currentHost write ~/Library/Preferences/ByHost/com.apple.notificationcenterui doNotDisturb -boolean false"
)
var usage func()
func init() {
usage = func() {
fmt.Println("Utility to toggle macOS do not disturb using notification center")
fmt.Fprintf(os.Stderr, "Usage: %s [subcommand] {enable|disable}\n", os.Args[0])
flag.PrintDefaults()
os.Exit(0)
}
}
func main() {
flag.Parse()
if len(flag.Args()) == 0 {
usage()
}
switch os.Args[1] {
case enableFlag:
enable()
break
case disableFlag:
disable()
break
default:
usage()
}
}
func enable() {
cmd := exec.Command("sh", "-c", setDoNotDisturb)
_, err := cmd.CombinedOutput()
if err != nil {
fmt.Fprintf(os.Stderr, "error: could not set doNotDisturb to true: %v", err)
os.Exit(1)
}
cmd = exec.Command("sh", "-c", setDoNotDisturbDate)
_, err = cmd.CombinedOutput()
if err != nil {
fmt.Fprintf(os.Stderr, "error: could not write time to ~/Library/Preferences/ByHost/com.apple.notificationcenterui: %v", err)
os.Exit(1)
}
cmd = exec.Command("sh", "-c", killall)
_, err = cmd.CombinedOutput()
if err != nil {
fmt.Fprintf(os.Stderr, "error: could not reset NotificationCenter: %v", err)
os.Exit(1)
}
}
func disable() {
cmd := exec.Command("sh", "-c", unsetDoNotDisturb)
_, err := cmd.CombinedOutput()
if err != nil {
fmt.Fprintf(os.Stderr, "error: could not set doNotDisturb to false: %v", err)
os.Exit(1)
}
cmd = exec.Command("sh", "-c", killall)
_, err = cmd.CombinedOutput()
if err != nil {
fmt.Fprintf(os.Stderr, "error: could not reset NotificationCenter: %v", err)
os.Exit(1)
}
}