Skip to content

Commit

Permalink
Close apps in parallel (#4)
Browse files Browse the repository at this point in the history
* Also show output when an app was opened/closed successfully

* Use goroutines to close apps in parallel
  • Loading branch information
SimonTheLeg authored Aug 20, 2018
1 parent 249924c commit 8e4bb5b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 8 additions & 2 deletions applescriptHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
23 changes: 19 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,33 @@ 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 {
os.Exit(0)
}

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

0 comments on commit 8e4bb5b

Please sign in to comment.