From 6636cd38c094000410ec7b40fefcda9df6dcc109 Mon Sep 17 00:00:00 2001 From: Sascha Andres Date: Tue, 7 Mar 2017 09:45:14 +0100 Subject: [PATCH 1/2] Implemented os specific task file support --- errors.go | 9 ++++++-- read_taskfile.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ task.go | 17 -------------- 3 files changed, 65 insertions(+), 19 deletions(-) create mode 100644 read_taskfile.go diff --git a/errors.go b/errors.go index 4b94e194..0c6a1dcf 100644 --- a/errors.go +++ b/errors.go @@ -4,8 +4,13 @@ import ( "fmt" ) -// ErrNoTaskFile is returned when the program can not find a proper TaskFile -var ErrNoTaskFile = fmt.Errorf(`task: No task file found (is it named "%s"?)`, TaskFilePath) +type taskFileNotFound struct { + taskFile string +} + +func (err taskFileNotFound) Error() string { + return fmt.Sprintf(`task: No task file found (is it named "%s"?)`, err.taskFile) +} type taskNotFoundError struct { taskName string diff --git a/read_taskfile.go b/read_taskfile.go new file mode 100644 index 00000000..e76df86f --- /dev/null +++ b/read_taskfile.go @@ -0,0 +1,58 @@ +package task + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "os" + + "runtime" + + "github.com/BurntSushi/toml" + "github.com/imdario/mergo" + "gopkg.in/yaml.v2" +) + +func readTaskfile() (map[string]*Task, error) { + initialTasks, err := readTaskfileData(TaskFilePath) + if err != nil { + return nil, err + } + mergeTasks, err := readTaskfileData(fmt.Sprintf("%s_%s", TaskFilePath, runtime.GOOS)) + if err != nil { + switch err.(type) { + default: + return nil, err + case taskFileNotFound: + return initialTasks, nil + } + } + if err := mergo.MapWithOverwrite(&initialTasks, mergeTasks); err != nil { + return nil, err + } + return initialTasks, nil +} + +func readTaskfileData(path string) (tasks map[string]*Task, err error) { + if b, err := ioutil.ReadFile(path + ".yml"); err == nil { + return tasks, yaml.Unmarshal(b, &tasks) + } + if b, err := ioutil.ReadFile(path + ".json"); err == nil { + return tasks, json.Unmarshal(b, &tasks) + } + if b, err := ioutil.ReadFile(path + ".toml"); err == nil { + return tasks, toml.Unmarshal(b, &tasks) + } + return nil, taskFileNotFound{path} +} + +func exists(path string) (bool, error) { + _, err := os.Stat(path) + if err == nil { + return true, nil + } + if os.IsNotExist(err) { + return false, nil + } + return true, err +} diff --git a/task.go b/task.go index 26aa9809..ec07617f 100644 --- a/task.go +++ b/task.go @@ -1,16 +1,12 @@ package task import ( - "encoding/json" - "io/ioutil" "log" "os" "os/exec" "strings" - "github.com/BurntSushi/toml" "github.com/spf13/pflag" - "gopkg.in/yaml.v2" ) var ( @@ -168,16 +164,3 @@ func (t *Task) runCommand(i int) 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 -} From 78bf1aeeac3e0035da3fbcef23c980668d193fe6 Mon Sep 17 00:00:00 2001 From: Sascha Andres Date: Tue, 7 Mar 2017 09:47:41 +0100 Subject: [PATCH 2/2] Updated README Closes #4 --- README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/README.md b/README.md index e49afd49..300ee1f9 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,30 @@ task assets build If Bash is available (Linux and Windows while on Git Bash), the commands will run in Bash, otherwise will fallback to `cmd` (on Windows). +### OS specific task support + +If you add a `Taskfile_{{GOOS}}` you can override or amend your taskfile based on the op;erating system. + +Example: + +Taskfile.yml: + +```yml +build: + cmds: + - echo "default" +``` + +Taskfile_linux.yml: + +```yml +build: + cmds: + - echo "linux" +``` + +Will print out `linux` and not default + ### Running task in another dir By default, tasks will be executed in the directory where the Taskfile is