From 61b1aa8559dda4469dae5579b3bbc17f1c4683c7 Mon Sep 17 00:00:00 2001 From: Travis Date: Tue, 28 Feb 2017 08:13:48 -0800 Subject: [PATCH] added yml/json/toml support, --help flag, and example directory Signed-off-by: Andrey Nering --- cmd/task/task.go | 25 +++++++++++++++++++++++++ example/Taskfile.json | 11 +++++++++++ example/Taskfile.yml | 6 ++++++ task.go | 31 ++++++++++++++++++++++--------- 4 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 example/Taskfile.json create mode 100644 example/Taskfile.yml diff --git a/cmd/task/task.go b/cmd/task/task.go index 8f4cb493..eee52d81 100644 --- a/cmd/task/task.go +++ b/cmd/task/task.go @@ -1,9 +1,34 @@ package main import ( + "flag" + "fmt" + "github.com/go-task/task" ) func main() { + flag.CommandLine.Usage = func() { + fmt.Println(` +task [target1 target2 ...]: Runs commands under targets like make. + +Example: 'task hello' with the following 'Taskfile.json' file will generate +an 'output.txt' file. +''' +{ + "hello": { + "cmds": [ + "echo \"I am going to write a file named 'output.txt' now.\"", + "echo \"hello\" > output.txt" + ], + "generates": [ + "output.txt" + ] + } +} +''' +`) + } + flag.Parse() task.Run() } diff --git a/example/Taskfile.json b/example/Taskfile.json new file mode 100644 index 00000000..aaca303f --- /dev/null +++ b/example/Taskfile.json @@ -0,0 +1,11 @@ +{ + "hello": { + "cmds": [ + "echo \"I am going to write a file named 'output.txt' now.\"", + "echo \"hello\" > output.txt" + ], + "generates": [ + "output.txt" + ] + } +} \ No newline at end of file diff --git a/example/Taskfile.yml b/example/Taskfile.yml new file mode 100644 index 00000000..85b101f7 --- /dev/null +++ b/example/Taskfile.yml @@ -0,0 +1,6 @@ +hello: + cmds: + - echo "I am going to write a file named 'output.txt' now." + - echo "hello" > output.txt + generates: + - output.txt diff --git a/task.go b/task.go index 0dca8732..48afd080 100644 --- a/task.go +++ b/task.go @@ -1,18 +1,21 @@ package task import ( + "encoding/json" + "errors" "fmt" "io/ioutil" "log" "os" "os/exec" + "github.com/BurntSushi/toml" "gopkg.in/yaml.v2" ) var ( // TaskFilePath is the default Taskfile - TaskFilePath = "Taskfile.yml" + TaskFilePath = "Taskfile" // ShExists is true if Bash was found ShExists bool // ShPath constains the Bash path if found @@ -65,15 +68,9 @@ func Run() { log.Fatal("No argument given") } - file, err := ioutil.ReadFile(TaskFilePath) + var err error + Tasks, err = readTaskfile() if err != nil { - if os.IsNotExist(err) { - log.Fatal("Taskfile.yml not found") - } - log.Fatal(err) - } - - if err = yaml.Unmarshal(file, &Tasks); err != nil { log.Fatal(err) } @@ -142,3 +139,19 @@ func runCommand(c string) error { } return nil } + +func readTaskfile() (tasks map[string]*Task, err error) { + if b, err := ioutil.ReadFile(TaskFilePath + ".yml"); err == nil { + return tasks, yaml.Unmarshal(b, &tasks) + } + if b, err := ioutil.ReadFile(TaskFilePath + ".json"); err == nil { + return tasks, json.Unmarshal(b, &tasks) + } + if b, err := ioutil.ReadFile(TaskFilePath + ".toml"); err == nil { + return tasks, toml.Unmarshal(b, &tasks) + } + return nil, ErrNoTaskFile +} + +// ErrNoTaskFile is returns when the program can not find a proper TaskFile +var ErrNoTaskFile = errors.New("no task file found (is it named '" + TaskFilePath + "'?)")