diff --git a/Dofile.example b/Dofile.example index 25d4715..288361f 100644 --- a/Dofile.example +++ b/Dofile.example @@ -1,14 +1,14 @@ description = "Dofile example" -[task] +[tasks] - [task.build] + [tasks.build] commands = [ 'echo "Ja hallo!', 'ls -l' ] - [task.clean] + [tasks.clean] commands = [ 'echo "Ui ui ui ui ui!"', ] \ No newline at end of file diff --git a/Gopkg.lock b/Gopkg.lock index 77f9cba..4b8127d 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -19,6 +19,12 @@ revision = "6638fbbc28f9ad0136ef5fd84a15c995e08e161f" version = "v1.0.0" +[[projects]] + branch = "master" + name = "github.com/flynn/go-shlex" + packages = ["."] + revision = "3f9db97f856818214da2e1057f8ad84803971cff" + [[projects]] name = "github.com/logrusorgru/aurora" packages = ["."] @@ -28,6 +34,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "f9c491a4f9e9a67215f89654601df99beeea8e607e60c8e3830d67936998c3ea" + inputs-digest = "adc7005966d8144acb43ef3b508f709a8fe83d222fbf40dcf6765fa5d0013ca9" solver-name = "gps-cdcl" solver-version = 1 diff --git a/main.go b/main.go index ae94604..27424e7 100644 --- a/main.go +++ b/main.go @@ -4,9 +4,13 @@ import ( "fmt" "github.com/BurntSushi/toml" "github.com/alexflint/go-arg" + "github.com/flynn/go-shlex" . "github.com/logrusorgru/aurora" "io/ioutil" "log" + "os" + "os/exec" + "strings" ) var args struct { @@ -15,15 +19,46 @@ var args struct { type Dofile struct { Description string - Tasks map[string]Task + Tasks map[string]task } -type Task struct { - commands []string +type task struct { + Commands []string } -func executeTask() { +func remove(slice []string, s int) []string { + return append(slice[:s], slice[s+1:]...) +} + +func parseCommand(command string) []string { + parts, err := shlex.Split(strings.TrimSpace(command)) + if err != nil { + log.Fatal(err) + } + + return parts +} +func executeTask(doFile Dofile, taskName string) { + if _, found := doFile.Tasks[taskName]; found { + fmt.Println(Bold(Green("Executing task")), Bold(Cyan(taskName))) + + for _, command := range doFile.Tasks[taskName].Commands { + fmt.Println(Bold(Yellow(taskName)), " ", command) + + tokens := parseCommand(command) + cmdName := tokens[0] + tokens = remove(tokens, 0) + + if err := exec.Command(cmdName, tokens...).Run(); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + } + } else { + fmt.Println(Bold(Red("Could not find task")), Bold(Yellow(taskName)), Bold(Red("aborting!"))) + os.Exit(-1); + } } func main() { @@ -40,6 +75,8 @@ func main() { } for _, taskName := range args.TaskName { - fmt.Println(Bold(Green("Executing task")), Bold(Cyan(taskName))) + executeTask(doFile, taskName) } + + fmt.Println(Bold(Green("Done executing all tasks for")), doFile.Description) }