1
0
mirror of https://github.com/go-task/task.git synced 2025-06-23 00:38:19 +02:00

added yml/json/toml support, --help flag, and example directory

Signed-off-by: Andrey Nering <andrey.nering@gmail.com>
This commit is contained in:
Travis
2017-02-28 08:13:48 -08:00
committed by Andrey Nering
parent a15cf26842
commit 61b1aa8559
4 changed files with 64 additions and 9 deletions

View File

@ -1,9 +1,34 @@
package main package main
import ( import (
"flag"
"fmt"
"github.com/go-task/task" "github.com/go-task/task"
) )
func main() { 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() task.Run()
} }

11
example/Taskfile.json Normal file
View File

@ -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"
]
}
}

6
example/Taskfile.yml Normal file
View File

@ -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

31
task.go
View File

@ -1,18 +1,21 @@
package task package task
import ( import (
"encoding/json"
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
"os/exec" "os/exec"
"github.com/BurntSushi/toml"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
) )
var ( var (
// TaskFilePath is the default Taskfile // TaskFilePath is the default Taskfile
TaskFilePath = "Taskfile.yml" TaskFilePath = "Taskfile"
// ShExists is true if Bash was found // ShExists is true if Bash was found
ShExists bool ShExists bool
// ShPath constains the Bash path if found // ShPath constains the Bash path if found
@ -65,15 +68,9 @@ func Run() {
log.Fatal("No argument given") log.Fatal("No argument given")
} }
file, err := ioutil.ReadFile(TaskFilePath) var err error
Tasks, err = readTaskfile()
if err != nil { 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) log.Fatal(err)
} }
@ -142,3 +139,19 @@ func runCommand(c string) error {
} }
return nil 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 + "'?)")