Skip to content

Commit

Permalink
Upgrade handlers (#8)
Browse files Browse the repository at this point in the history
* Rewrite Open & Close App to use new handler

* Rewrite Notification Center to use new handler

* Make the old example-config the new default config
  • Loading branch information
SimonTheLeg committed Oct 7, 2018
1 parent e2a4ed6 commit e4c9cb4
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 93 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Expand Up @@ -22,7 +22,7 @@ jobs:
environment:
TEST_RES_DIR: /tmp/test-results
ARTIFACTS_DIR: /tmp/artifacts
APP_VERSION: v0.3.0
APP_VERSION: v0.4.0
steps:
- checkout
- run:
Expand Down
39 changes: 24 additions & 15 deletions applescriptHandler.go
Expand Up @@ -7,39 +7,48 @@ import (
)

// OpenApp opens an application using AppleScript
func OpenApp(appName string) error {
script := fmt.Sprintf(`
func OpenApp(appName string, reschan chan string, errchan chan error) func() {
return func() {
script := fmt.Sprintf(`
tell application "%s"
activate
end tell
`, appName)

err := ExecuteAppleScript(script)
err := executeAppleScript(script)

if err != nil {
return fmt.Errorf("Could not open app '%s': '%v'", appName, err)
if err != nil {
errchan <- fmt.Errorf("Could not open app '%s': '%v'", appName, err)
return
}

reschan <- fmt.Sprintf("Successfully opened app: '%s'", appName)
return
}
return nil
}

// CloseApp closes an application using AppleScript
func CloseApp(appName string) error {
script := fmt.Sprintf(`
func CloseApp(appName string, reschan chan string, errchan chan error) func() {
return func() {
script := fmt.Sprintf(`
tell application "%s"
quit
end tell
`, appName)
`, appName)

err := ExecuteAppleScript(script)
err := executeAppleScript(script)

if err != nil {
return fmt.Errorf("Could not close app '%s': '%v'", appName, err)
if err != nil {
errchan <- fmt.Errorf("Could not close app '%s': '%v'", appName, err)
return
}
reschan <- fmt.Sprintf("Successfully closed app: '%s'", appName)
return
}
return nil
}

// ExecuteAppleScript takes in a fully parsed Apple-Script executes the command using osascript
func ExecuteAppleScript(command string) error {
// executeAppleScript takes in a fully parsed Apple-Script and executes the command using osascript
func executeAppleScript(command string) error {
cmd := exec.Command("osascript", "-e", command)

output, err := cmd.CombinedOutput()
Expand Down
4 changes: 2 additions & 2 deletions example-config.json → default-config.json
@@ -1,7 +1,7 @@
{
"affectedApps": [
"Mail",
"Rocket.Chat+",
"Calendar"
]
],
"notificationCenter": true
}
77 changes: 43 additions & 34 deletions donotdisturbHandler.go
Expand Up @@ -19,57 +19,66 @@ func restartNotificationCenter() error {
}

// TurnDoNotDisturbOn turns on Do Not Disturb on Mac OS X
func TurnDoNotDisturbOn() error {
func TurnDoNotDisturbOn(reschan chan string, errchan chan error) func() {
return func() {
// Enable Do Not Disturb Mode
cmd := exec.Command("bash", "-c", "defaults -currentHost write ~/Library/Preferences/ByHost/com.apple.notificationcenterui doNotDisturb -boolean true")

// Enable Do Not Disturb Mode
cmd := exec.Command("bash", "-c", "defaults -currentHost write ~/Library/Preferences/ByHost/com.apple.notificationcenterui doNotDisturb -boolean true")
_, err := cmd.CombinedOutput()

_, err := cmd.CombinedOutput()
if err != nil {
errchan <- fmt.Errorf("Could not turn Do Not Disturb on: %v", err)
return
}

if err != nil {
return err
}
// Set Time forDo Not Disturb Mode
t := time.Now()
tfmt := fmt.Sprintf("%d-%02d-%02d %02d:%02d:%02d +0000", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())

// Set Time forDo Not Disturb Mode
t := time.Now()
tfmt := fmt.Sprintf("%d-%02d-%02d %02d:%02d:%02d +0000", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
cmd = exec.Command("bash", "-c", fmt.Sprintf("defaults -currentHost write ~/Library/Preferences/ByHost/com.apple.notificationcenterui doNotDisturbDate -date '%s'", tfmt))

cmd = exec.Command("bash", "-c", fmt.Sprintf("defaults -currentHost write ~/Library/Preferences/ByHost/com.apple.notificationcenterui doNotDisturbDate -date '%s'", tfmt))
_, err = cmd.CombinedOutput()

_, err = cmd.CombinedOutput()
if err != nil {
errchan <- fmt.Errorf("Could not set time for turning Do Not Disturb on: %v", err)
return
}

if err != nil {
return err
}
// Restart Notification Center
err = restartNotificationCenter()

// Restart Notification Center
err = restartNotificationCenter()
if err != nil {
errchan <- fmt.Errorf("Could not restart Notification Center: %v", err)
return
}

if err != nil {
return err
reschan <- "Successfully turned Do Not Disturb On"
return
}

return nil
}

// TurnDoNotDisturbOff turns off Do Not Disturb on Mac OS X
func TurnDoNotDisturbOff() error {
func TurnDoNotDisturbOff(reschan chan string, errchan chan error) func() {
return func() {
// Disable Do Not Disturb Mode
cmd := exec.Command("bash", "-c", "defaults -currentHost write ~/Library/Preferences/ByHost/com.apple.notificationcenterui doNotDisturb -boolean false")

// Disable Do Not Disturb Mode
cmd := exec.Command("bash", "-c", "defaults -currentHost write ~/Library/Preferences/ByHost/com.apple.notificationcenterui doNotDisturb -boolean false")
_, err := cmd.CombinedOutput()

_, err := cmd.CombinedOutput()
if err != nil {
errchan <- fmt.Errorf("Could not turn Do Not Disturb off: %v", err)
return
}

if err != nil {
return err
}
// Restart Notification Center
err = restartNotificationCenter()

// Restart Notification Center
err = restartNotificationCenter()
if err != nil {
errchan <- fmt.Errorf("Could not restart Notification Center: %v", err)
return
}

if err != nil {
return err
reschan <- "Successfully turned Do Not Disturb Off"
return
}

return nil
}
73 changes: 32 additions & 41 deletions main.go
Expand Up @@ -12,24 +12,27 @@ import (
homedir "github.com/mitchellh/go-homedir"
)

var configLocation string

var version = "dev-build"

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
// 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
config, err := parseConfig(configLocation)
curConfig, err = parseConfig(configLocation)

if err != nil {
log.Fatalf("Could not parse config file: %v", err)
Expand All @@ -39,42 +42,19 @@ func main() {
flag.Parse()
desAction := flag.Arg(0)

// Determine desired action
var action (func(name string) error)
action = determineAction(desAction)
// Determine desired actions
actions := determineActions(desAction)

if action == nil {
if actions == nil {
os.Exit(0)
}

// Handle Notification Center
switch desAction {
case "on":
err = TurnDoNotDisturbOn()
case "off":
err = TurnDoNotDisturbOff()
// Execute all actions in parallel
for _, action := range actions {
go action()
}

if err != nil {
fmt.Printf("Could not change Do Not Disturb State: %v\n", err)
} else {
fmt.Println("Successfully changed Do Not Disturb State")
}
// Execute action
reschan, errchan := make(chan string), make(chan error)

for _, app := range config.AffectedApps {
go func(app string) {
err := action(app)
if err != nil {
errchan <- err
return
}
reschan <- fmt.Sprintf("Successfully opened/closed: '%s'", app)
}(app)
}

for i := 0; i < len(config.AffectedApps); i++ {
for i := 0; i < len(actions); i++ {
select {
case res := <-reschan:
fmt.Println(res)
Expand All @@ -84,19 +64,32 @@ func main() {
}
}

func determineAction(desAction string) func(name string) error {
func determineActions(desAction string) []func() {
var actions []func()

switch desAction {
case "on":
return CloseApp
// 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":
return OpenApp
// 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) {
Expand All @@ -109,8 +102,6 @@ func parseConfig(configLocation string) (config, error) {

// Check if there is a config file at the specified location, if not create a default config
if os.IsNotExist(err) {
defaultConfig := []byte(`{"affectedApps":["Mail","Calendar"]}`)

// 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)
Expand Down

0 comments on commit e4c9cb4

Please sign in to comment.