Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ 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).

### Environment

### Environment
You can specify environment variables that are added when running a command:

```yml
Expand All @@ -54,6 +54,30 @@ build:
hallo: welt
```

### 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
Expand Down
9 changes: 7 additions & 2 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 58 additions & 0 deletions read_taskfile.go
Original file line number Diff line number Diff line change
@@ -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
}
17 changes: 0 additions & 17 deletions task.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package task

import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"strings"

"github.com/BurntSushi/toml"
"github.com/spf13/pflag"
"gopkg.in/yaml.v2"
)

var (
Expand Down Expand Up @@ -185,16 +181,3 @@ func (t *Task) runCommand(i int, envVariables map[string]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
}