-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
135 lines (112 loc) · 3.09 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path"
homedir "github.com/mitchellh/go-homedir"
)
type config struct {
AffectedApps []string `json:"affectedApps"`
}
var version = "dev-build"
var defaultConfig = []byte(`{"affectedApps":["Mail","Calendar"]}`)
var curConfig config
var reschan = make(chan string)
var errchan = make(chan error)
func main() {
// Get Users homedirectory to set the configLocation
configLocation, err := homedir.Dir()
if err != nil {
log.Fatalf("Could not determine users home directory: %v", err)
}
configLocation += "/.deepwork/config.json"
// Parse Configuration
curConfig, err = parseConfig(configLocation)
if err != nil {
log.Fatalf("Could not parse config file: %v", err)
}
// Parse Command Line Flags
flag.Parse()
desAction := flag.Arg(0)
// Determine desired actions
actions := determineActions(desAction)
if actions == nil {
os.Exit(0)
}
// Execute all actions in parallel
for _, action := range actions {
go action()
}
for i := 0; i < len(actions); i++ {
select {
case res := <-reschan:
fmt.Println(res)
case err := <-errchan:
fmt.Println(err)
}
}
}
func determineActions(desAction string) []func() {
var actions []func()
switch desAction {
case "on":
// Handle Apps
for _, app := range curConfig.AffectedApps {
actions = append(actions, CloseApp(app, reschan, errchan))
}
// Handle Notification Center
actions = append(actions, TurnDoNotDisturbOn(reschan, errchan))
case "off":
// Handle Apps
for _, app := range curConfig.AffectedApps {
actions = append(actions, OpenApp(app, reschan, errchan))
}
// Handle Notification Center
actions = append(actions, TurnDoNotDisturbOff(reschan, errchan))
case "version":
fmt.Println(version)
return nil
default:
fmt.Println("Usage: deepwork [on,off,version]")
return nil
}
return actions
}
func parseConfig(configLocation string) (config, error) {
var conf config
confDir := path.Dir(configLocation)
// Try to open config
jsonFile, err := os.Open(configLocation)
defer jsonFile.Close()
// Check if there is a config file at the specified location, if not create a default config
if os.IsNotExist(err) {
// Create required directories if necessary
if err = os.MkdirAll(confDir, 0744); err != nil {
return config{}, fmt.Errorf("Could not create required directories for config: %v", err)
}
// Write default config
if err = ioutil.WriteFile(configLocation, defaultConfig, 0644); err != nil {
return config{}, fmt.Errorf("Could not write default config: %v", err)
}
// Call itself again to parse newly created conf
return parseConfig(configLocation)
}
// Otherwise (e.g. no permissions on conf file or it's dir), return the error
if err != nil {
return config{}, fmt.Errorf("Could not access config file: %v", err)
}
// Read in the config
confByte, err := ioutil.ReadAll(jsonFile)
if err != nil {
return config{}, fmt.Errorf("Could not read config: %v", err)
}
err = json.Unmarshal(confByte, &conf)
if err != nil {
return config{}, fmt.Errorf("Could not parse config: %v", err)
}
return conf, nil
}