Skip to content

Commit d970e93

Browse files
committed
Add --taskfile flag (alias -t) to allow running another Taskfile
Closes #221
1 parent 762714d commit d970e93

File tree

4 files changed

+43
-22
lines changed

4 files changed

+43
-22
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
([#205](https://github.com/go-task/task/pull/205)).
88
- Create directory informed on `dir:` if it doesn't exist
99
([#209](https://github.com/go-task/task/issues/209), [#211](https://github.com/go-task/task/pull/211)).
10+
- We now have a `--taskfile` flag (alias `-t`), which can be used to run
11+
another Taskfile (other than the default `Taskfile.yml`)
12+
([#221](https://github.com/go-task/task/pull/221)).
1013

1114
## v2.5.2 - 2019-05-11
1215

cmd/task/task.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"log"
66
"os"
77
"os/signal"
8+
"path/filepath"
89
"syscall"
910

1011
"github.com/go-task/task/v2"
@@ -17,7 +18,7 @@ var (
1718
version = "master"
1819
)
1920

20-
const usage = `Usage: task [-ilfwvsd] [--init] [--list] [--force] [--watch] [--verbose] [--silent] [--dir] [--dry] [--summary] [task...]
21+
const usage = `Usage: task [-ilfwvsd] [--init] [--list] [--force] [--watch] [--verbose] [--silent] [--dir] [--taskfile] [--dry] [--summary] [task...]
2122
2223
Runs the specified task(s). Falls back to the "default" task if no task name
2324
was specified, or lists all tasks if an unknown task name was specified.
@@ -58,6 +59,7 @@ func main() {
5859
dry bool
5960
summary bool
6061
dir string
62+
entrypoint string
6163
output string
6264
)
6365

@@ -72,6 +74,7 @@ func main() {
7274
pflag.BoolVar(&dry, "dry", false, "compiles and prints tasks in the order that they would be run, without executing them")
7375
pflag.BoolVar(&summary, "summary", false, "show summary about a task")
7476
pflag.StringVarP(&dir, "dir", "d", "", "sets directory of execution")
77+
pflag.StringVarP(&entrypoint, "taskfile", "t", "", `choose which Taskfile to run. Defaults to "Taskfile.yml"`)
7578
pflag.StringVarP(&output, "output", "o", "", "sets output style: [interleaved|group|prefixed]")
7679
pflag.Parse()
7780

@@ -91,14 +94,26 @@ func main() {
9194
return
9295
}
9396

97+
if dir != "" && entrypoint != "" {
98+
log.Fatal("task: You can't set both --dir and --taskfile")
99+
return
100+
}
101+
if entrypoint != "" {
102+
dir = filepath.Dir(entrypoint)
103+
entrypoint = filepath.Base(entrypoint)
104+
} else {
105+
entrypoint = "Taskfile.yml"
106+
}
107+
94108
e := task.Executor{
95-
Force: force,
96-
Watch: watch,
97-
Verbose: verbose,
98-
Silent: silent,
99-
Dir: dir,
100-
Dry: dry,
101-
Summary: summary,
109+
Force: force,
110+
Watch: watch,
111+
Verbose: verbose,
112+
Silent: silent,
113+
Dir: dir,
114+
Dry: dry,
115+
Entrypoint: entrypoint,
116+
Summary: summary,
102117

103118
Stdin: os.Stdin,
104119
Stdout: os.Stdout,

internal/taskfile/read/taskfile.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,13 @@ import (
1515
var (
1616
// ErrIncludedTaskfilesCantHaveIncludes is returned when a included Taskfile contains includes
1717
ErrIncludedTaskfilesCantHaveIncludes = errors.New("task: Included Taskfiles can't have includes. Please, move the include to the main Taskfile")
18-
19-
// ErrNoTaskfileFound is returned when Taskfile.yml is not found
20-
ErrNoTaskfileFound = errors.New(`task: No Taskfile.yml found. Use "task --init" to create a new one`)
2118
)
2219

2320
// Taskfile reads a Taskfile for a given directory
24-
func Taskfile(dir string) (*taskfile.Taskfile, error) {
25-
path := filepath.Join(dir, "Taskfile.yml")
21+
func Taskfile(dir string, entrypoint string) (*taskfile.Taskfile, error) {
22+
path := filepath.Join(dir, entrypoint)
2623
if _, err := os.Stat(path); err != nil {
27-
return nil, ErrNoTaskfileFound
24+
return nil, fmt.Errorf(`task: No Taskfile found on "%s". Use "task --init" to create a new one`, path)
2825
}
2926
t, err := readTaskfile(path)
3027
if err != nil {

task.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@ const (
3232
// Executor executes a Taskfile
3333
type Executor struct {
3434
Taskfile *taskfile.Taskfile
35-
Dir string
36-
Force bool
37-
Watch bool
38-
Verbose bool
39-
Silent bool
40-
Dry bool
41-
Summary bool
35+
36+
Dir string
37+
Entrypoint string
38+
Force bool
39+
Watch bool
40+
Verbose bool
41+
Silent bool
42+
Dry bool
43+
Summary bool
4244

4345
Stdin io.Reader
4446
Stdout io.Writer
@@ -85,8 +87,12 @@ func (e *Executor) Run(ctx context.Context, calls ...taskfile.Call) error {
8587

8688
// Setup setups Executor's internal state
8789
func (e *Executor) Setup() error {
90+
if e.Entrypoint == "" {
91+
e.Entrypoint = "Taskfile.yml"
92+
}
93+
8894
var err error
89-
e.Taskfile, err = read.Taskfile(e.Dir)
95+
e.Taskfile, err = read.Taskfile(e.Dir, e.Entrypoint)
9096
if err != nil {
9197
return err
9298
}

0 commit comments

Comments
 (0)