From 389d7f7aed03e374c1b6b28cc94adbb341384ac8 Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Sun, 26 Feb 2017 20:43:50 -0300 Subject: [PATCH] First working version --- .gitignore | 2 ++ task.go | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index a1338d68..95945e38 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,5 @@ # Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736 .glide/ + +task diff --git a/task.go b/task.go index b028dfdf..8224d16f 100644 --- a/task.go +++ b/task.go @@ -1 +1,63 @@ -package task +package main + +import ( + "io/ioutil" + "log" + "os" + "os/exec" + "path/filepath" + + "github.com/kardianos/osext" + "gopkg.in/yaml.v2" +) + +var ( + CurrentDirectory, _ = osext.ExecutableFolder() + TaskFilePath = filepath.Join(CurrentDirectory, "Taskfile.yml") +) + +type Task struct { + Cmds []string + Deps []string + Source string + Generates string +} + +func main() { + log.SetFlags(0) + + args := os.Args[1:] + if len(args) == 0 { + log.Fatal("No argument given") + } + + file, err := ioutil.ReadFile(TaskFilePath) + if err != nil { + log.Fatal(err) + } + + tasks := make(map[string]*Task) + if err = yaml.Unmarshal(file, &tasks); err != nil { + log.Fatal(err) + } + + task, ok := tasks[args[0]] + if !ok { + log.Fatalf(`Task "%s" not found`, args[0]) + } + + if err = RunTask(task); err != nil { + log.Fatal(err) + } +} + +func RunTask(t *Task) error { + for _, c := range t.Cmds { + cmd := exec.Command("/bin/sh", "-c", c) + cmd.Stdout = os.Stdout + if err := cmd.Run(); err != nil { + return err + } + } + return nil +}