From 8e4bb5b1be2af326d82c083b9e1758836e34514a Mon Sep 17 00:00:00 2001 From: Simon Bein Date: Mon, 20 Aug 2018 22:33:22 +0200 Subject: [PATCH] Close apps in parallel (#4) * Also show output when an app was opened/closed successfully * Use goroutines to close apps in parallel --- .circleci/config.yml | 2 +- applescriptHandler.go | 10 ++++++++-- main.go | 23 +++++++++++++++++++---- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 2167568..1360e49 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.1.1 + APP_VERSION: v0.2.0 steps: - checkout - run: diff --git a/applescriptHandler.go b/applescriptHandler.go index f154e85..7873ef4 100644 --- a/applescriptHandler.go +++ b/applescriptHandler.go @@ -16,7 +16,10 @@ end tell err := ExecuteAppleScript(script) - return fmt.Errorf("Could not open app '%s': '%v'", appName, err) + if err != nil { + return fmt.Errorf("Could not open app '%s': '%v'", appName, err) + } + return nil } // CloseApp closes an application using AppleScript @@ -29,7 +32,10 @@ end tell err := ExecuteAppleScript(script) - return fmt.Errorf("Could not close app '%s': '%v'", appName, err) + if err != nil { + return fmt.Errorf("Could not close app '%s': '%v'", appName, err) + } + return nil } // ExecuteAppleScript takes in a fully parsed Apple-Script executes the command using osascript diff --git a/main.go b/main.go index ddbaf3c..17520f4 100644 --- a/main.go +++ b/main.go @@ -40,7 +40,7 @@ func main() { desAction := flag.Arg(0) // Determine desired action - var action func(name string) error + var action (func(name string) error) action = determineAction(desAction) if action == nil { @@ -48,10 +48,25 @@ func main() { } // Execute action + reschan, errchan := make(chan string), make(chan error) + for _, app := range config.AffectedApps { - err := action(app) - if err != nil { - log.Printf("%v", err) + 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++ { + select { + case res := <-reschan: + fmt.Println(res) + case err := <-errchan: + fmt.Println(err) } } }