From e4c9cb4c245e57f81ef2b79cb518d438c1015d53 Mon Sep 17 00:00:00 2001 From: Simon Bein Date: Sun, 7 Oct 2018 13:38:53 +0200 Subject: [PATCH] Upgrade handlers (#8) * Rewrite Open & Close App to use new handler * Rewrite Notification Center to use new handler * Make the old example-config the new default config --- .circleci/config.yml | 2 +- applescriptHandler.go | 39 ++++++----- example-config.json => default-config.json | 4 +- donotdisturbHandler.go | 77 ++++++++++++---------- main.go | 73 +++++++++----------- 5 files changed, 102 insertions(+), 93 deletions(-) rename example-config.json => default-config.json (59%) diff --git a/.circleci/config.yml b/.circleci/config.yml index b05eae2..9279f6e 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -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: diff --git a/applescriptHandler.go b/applescriptHandler.go index 7873ef4..6f35863 100644 --- a/applescriptHandler.go +++ b/applescriptHandler.go @@ -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() diff --git a/example-config.json b/default-config.json similarity index 59% rename from example-config.json rename to default-config.json index 152e2a2..7dcdff9 100644 --- a/example-config.json +++ b/default-config.json @@ -1,7 +1,7 @@ { "affectedApps": [ "Mail", - "Rocket.Chat+", "Calendar" - ] + ], + "notificationCenter": true } \ No newline at end of file diff --git a/donotdisturbHandler.go b/donotdisturbHandler.go index fdb4f54..28c7116 100644 --- a/donotdisturbHandler.go +++ b/donotdisturbHandler.go @@ -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 } diff --git a/main.go b/main.go index 8031fb9..709aa55 100644 --- a/main.go +++ b/main.go @@ -12,16 +12,19 @@ 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) @@ -29,7 +32,7 @@ func main() { 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) @@ -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) @@ -84,12 +64,24 @@ 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 @@ -97,6 +89,7 @@ func determineAction(desAction string) func(name string) error { fmt.Println("Usage: deepwork [on,off,version]") return nil } + return actions } func parseConfig(configLocation string) (config, error) { @@ -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)