mirror of
https://github.com/go-task/task.git
synced 2024-12-04 10:24:45 +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:
parent
a15cf26842
commit
61b1aa8559
@ -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()
|
||||
}
|
||||
|
11
example/Taskfile.json
Normal file
11
example/Taskfile.json
Normal 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
6
example/Taskfile.yml
Normal 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
31
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 + "'?)")
|
||||
|
Loading…
Reference in New Issue
Block a user