Skip to content

Commit 6ae93dc

Browse files
committed
feat: a couple of fixes and improvements on task --init
* Fixed check for an existing Taskfile: look for all possibilities, and not only `Taskfile.yml` specifically. * Added a description (`desc`) to the `default` task. Important to at least `task --list` work by default (a core feature).
1 parent 67a0225 commit 6ae93dc

File tree

6 files changed

+27
-14
lines changed

6 files changed

+27
-14
lines changed

init.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import (
66

77
"github.com/go-task/task/v3/errors"
88
"github.com/go-task/task/v3/internal/filepathext"
9+
"github.com/go-task/task/v3/taskfile"
910
)
1011

11-
const defaultTaskFilename = "Taskfile.yml"
12+
const defaultFilename = "Taskfile.yml"
1213

1314
//go:embed taskfile/templates/default.yml
1415
var DefaultTaskfile string
@@ -20,22 +21,30 @@ var DefaultTaskfile string
2021
//
2122
// The final file path is always returned and may be different from the input path.
2223
func InitTaskfile(path string) (string, error) {
23-
fi, err := os.Stat(path)
24-
if err == nil && !fi.IsDir() {
24+
info, err := os.Stat(path)
25+
if err == nil && !info.IsDir() {
2526
return path, errors.TaskfileAlreadyExistsError{}
2627
}
2728

28-
if fi != nil && fi.IsDir() {
29-
path = filepathext.SmartJoin(path, defaultTaskFilename)
30-
// path was a directory, so check if Taskfile.yml exists in it
31-
if _, err := os.Stat(path); err == nil {
29+
if info != nil && info.IsDir() {
30+
// path was a directory, check if there is a Taskfile already
31+
if hasDefaultTaskfile(path) {
3232
return path, errors.TaskfileAlreadyExistsError{}
3333
}
34+
path = filepathext.SmartJoin(path, defaultFilename)
3435
}
3536

3637
if err := os.WriteFile(path, []byte(DefaultTaskfile), 0o644); err != nil {
3738
return path, err
3839
}
39-
4040
return path, nil
4141
}
42+
43+
func hasDefaultTaskfile(dir string) bool {
44+
for _, name := range taskfile.DefaultTaskfiles {
45+
if _, err := os.Stat(filepathext.SmartJoin(dir, name)); err == nil {
46+
return true
47+
}
48+
}
49+
return false
50+
}

internal/fsext/fs_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ func TestSearch(t *testing.T) {
114114
name: "find ../../Taskfile.yml using no entrypoint or dir by walking",
115115
entrypoint: "",
116116
dir: "",
117-
possibleFilenames: []string{"Taskfile.yml"},
118-
expectedEntrypoint: filepath.Join(wd, "..", "..", "Taskfile.yml"),
117+
possibleFilenames: []string{"Taskfile.yaml"},
118+
expectedEntrypoint: filepath.Join(wd, "..", "..", "Taskfile.yaml"),
119119
},
120120
{
121121
name: "find foo.txt first if listed first in possible filenames",

taskfile/node_file.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type FileNode struct {
1919

2020
func NewFileNode(entrypoint, dir string, opts ...NodeOption) (*FileNode, error) {
2121
// Find the entrypoint file
22-
resolvedEntrypoint, err := fsext.Search(entrypoint, dir, defaultTaskfiles)
22+
resolvedEntrypoint, err := fsext.Search(entrypoint, dir, DefaultTaskfiles)
2323
if err != nil {
2424
return nil, err
2525
}

taskfile/taskfile.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import (
1212
)
1313

1414
var (
15-
defaultTaskfiles = []string{
15+
// DefaultTaskfiles is the list of Taskfile file names supported by default.
16+
DefaultTaskfiles = []string{
1617
"Taskfile.yml",
1718
"taskfile.yml",
1819
"Taskfile.yaml",
@@ -66,7 +67,7 @@ func RemoteExists(ctx context.Context, u url.URL) (*url.URL, error) {
6667

6768
// If the request was not successful, append the default Taskfile names to
6869
// the URL and return the URL of the first successful request
69-
for _, taskfile := range defaultTaskfiles {
70+
for _, taskfile := range DefaultTaskfiles {
7071
// Fixes a bug with JoinPath where a leading slash is not added to the
7172
// path if it is empty
7273
if u.Path == "" {

taskfile/templates/default.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
version: '3'
44

55
vars:
6-
GREETING: Hello, World!
6+
GREETING: Hello, world!
77

88
tasks:
99
default:
10+
desc: Print a greeting message
1011
cmds:
1112
- echo "{{.GREETING}}"
1213
silent: true

website/src/docs/getting-started.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ vars:
4343

4444
tasks:
4545
default:
46+
desc: Print a greeting message
4647
cmds:
4748
- echo "{{.GREETING}}"
4849
silent: true
@@ -111,6 +112,7 @@ vars:
111112
112113
tasks:
113114
default:
115+
desc: Print a greeting message
114116
cmds:
115117
- echo "{{.GREETING}}"
116118
silent: true

0 commit comments

Comments
 (0)