Skip to content

Instantly share code, notes, and snippets.

@include
Forked from adnaan/script.sh
Last active August 29, 2015 14:19
Show Gist options
  • Save include/4039bcab377e90ecf601 to your computer and use it in GitHub Desktop.
Save include/4039bcab377e90ecf601 to your computer and use it in GitHub Desktop.
ls -la
echo "hello"
tree
adb devices
adb wait-for-device #example of a long running task
package main
import (
"bufio"
"fmt"
"gopkg.in/pipe.v2"
"os"
"strings"
"time"
)
func readLines(path string) []string {
inFile, _ := os.Open(path)
defer inFile.Close()
scanner := bufio.NewScanner(inFile)
scanner.Split(bufio.ScanLines)
var lines []string
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines
}
func parseScript(path string) map[string][]string {
cmds := make(map[string][]string)
for _, line := range readLines(path) {
args := strings.Fields(line)
cmds[args[0]] = args[1:]
}
return cmds
}
func execScript(script map[string][]string, status chan string, quit chan bool) {
s := pipe.NewState(nil, nil)
go func() {
for name, args := range script {
p := pipe.Line(pipe.Exec(name, args...), pipe.WriteFile("console.txt", 0777))
p(s)
cmdline := name + " " + fmt.Sprintf("%s", args)
s.RunTasks()
status <- cmdline
}
}()
<-quit
s.Kill()
}
/*Working with the assumption that the script is simply a sequence of commands*/
func main() {
script := parseScript("script.sh")
status := make(chan string)
quit := make(chan bool)
go execScript(script, status, quit)
for i := 0; i < len(script); i++ {
select {
case s := <-status:
fmt.Printf("task done: %v\n", s)
case <-time.After(time.Second * 10):
fmt.Println("timed out")
quit <- true
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment