From 550d730963c3c71e419d0059485724cf3c238009 Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Sun, 21 Sep 2025 16:54:50 -0300 Subject: [PATCH] fix: 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). --- init.go | 25 +++++++++++++++++-------- taskfile/node_file.go | 2 +- taskfile/taskfile.go | 5 +++-- taskfile/templates/default.yml | 3 ++- website/src/docs/getting-started.md | 2 ++ 5 files changed, 25 insertions(+), 12 deletions(-) diff --git a/init.go b/init.go index b377cc3f77..807d201d1c 100644 --- a/init.go +++ b/init.go @@ -6,9 +6,10 @@ import ( "github.com/go-task/task/v3/errors" "github.com/go-task/task/v3/internal/filepathext" + "github.com/go-task/task/v3/taskfile" ) -const defaultTaskFilename = "Taskfile.yml" +const defaultFilename = "Taskfile.yml" //go:embed taskfile/templates/default.yml var DefaultTaskfile string @@ -20,22 +21,30 @@ var DefaultTaskfile string // // The final file path is always returned and may be different from the input path. func InitTaskfile(path string) (string, error) { - fi, err := os.Stat(path) - if err == nil && !fi.IsDir() { + info, err := os.Stat(path) + if err == nil && !info.IsDir() { return path, errors.TaskfileAlreadyExistsError{} } - if fi != nil && fi.IsDir() { - path = filepathext.SmartJoin(path, defaultTaskFilename) - // path was a directory, so check if Taskfile.yml exists in it - if _, err := os.Stat(path); err == nil { + if info != nil && info.IsDir() { + // path was a directory, check if there is a Taskfile already + if hasDefaultTaskfile(path) { return path, errors.TaskfileAlreadyExistsError{} } + path = filepathext.SmartJoin(path, defaultFilename) } if err := os.WriteFile(path, []byte(DefaultTaskfile), 0o644); err != nil { return path, err } - return path, nil } + +func hasDefaultTaskfile(dir string) bool { + for _, name := range taskfile.DefaultTaskfiles { + if _, err := os.Stat(filepathext.SmartJoin(dir, name)); err == nil { + return true + } + } + return false +} diff --git a/taskfile/node_file.go b/taskfile/node_file.go index e25483382f..2a66cd08a2 100644 --- a/taskfile/node_file.go +++ b/taskfile/node_file.go @@ -19,7 +19,7 @@ type FileNode struct { func NewFileNode(entrypoint, dir string, opts ...NodeOption) (*FileNode, error) { // Find the entrypoint file - resolvedEntrypoint, err := fsext.Search(entrypoint, dir, defaultTaskfiles) + resolvedEntrypoint, err := fsext.Search(entrypoint, dir, DefaultTaskfiles) if err != nil { return nil, err } diff --git a/taskfile/taskfile.go b/taskfile/taskfile.go index e209444acc..488749de2b 100644 --- a/taskfile/taskfile.go +++ b/taskfile/taskfile.go @@ -12,7 +12,8 @@ import ( ) var ( - defaultTaskfiles = []string{ + // DefaultTaskfiles is the list of Taskfile file names supported by default. + DefaultTaskfiles = []string{ "Taskfile.yml", "taskfile.yml", "Taskfile.yaml", @@ -66,7 +67,7 @@ func RemoteExists(ctx context.Context, u url.URL) (*url.URL, error) { // If the request was not successful, append the default Taskfile names to // the URL and return the URL of the first successful request - for _, taskfile := range defaultTaskfiles { + for _, taskfile := range DefaultTaskfiles { // Fixes a bug with JoinPath where a leading slash is not added to the // path if it is empty if u.Path == "" { diff --git a/taskfile/templates/default.yml b/taskfile/templates/default.yml index 51b2a82a4a..36e33e01d7 100644 --- a/taskfile/templates/default.yml +++ b/taskfile/templates/default.yml @@ -3,10 +3,11 @@ version: '3' vars: - GREETING: Hello, World! + GREETING: Hello, world! tasks: default: + desc: Print a greeting message cmds: - echo "{{.GREETING}}" silent: true diff --git a/website/src/docs/getting-started.md b/website/src/docs/getting-started.md index f61d40c2b9..a6e8f7d91a 100644 --- a/website/src/docs/getting-started.md +++ b/website/src/docs/getting-started.md @@ -43,6 +43,7 @@ vars: tasks: default: + desc: Print a greeting message cmds: - echo "{{.GREETING}}" silent: true @@ -111,6 +112,7 @@ vars: tasks: default: + desc: Print a greeting message cmds: - echo "{{.GREETING}}" silent: true