From 97e2a2d6e3e2c83f8de8eeda15956c6987e28770 Mon Sep 17 00:00:00 2001 From: HeCorr <75134774+HeCorr@users.noreply.github.com> Date: Fri, 24 Jan 2025 22:23:47 -0300 Subject: [PATCH 01/14] feat: specify init filename with --taskfile flag previously, it was not possible to specify which filename to use when initializing a new Taskfile as it was hardcoded as "Taskfile.yml". now the --taskfile flag specifies where to write the file to, and the first * contained in it will be replaced by "Taskfile", so `task -it *.yaml` will create a `Taskfile.yaml` file. --- cmd/task/task.go | 9 ++++++++- init.go | 11 +++-------- internal/flags/flags.go | 2 +- task_test.go | 2 +- taskfile/taskfile.go | 2 ++ 5 files changed, 15 insertions(+), 11 deletions(-) diff --git a/cmd/task/task.go b/cmd/task/task.go index cf07a5be6a..7cb72d787e 100644 --- a/cmd/task/task.go +++ b/cmd/task/task.go @@ -13,6 +13,7 @@ import ( "github.com/go-task/task/v3/args" "github.com/go-task/task/v3/errors" "github.com/go-task/task/v3/internal/experiments" + "github.com/go-task/task/v3/internal/filepathext" "github.com/go-task/task/v3/internal/flags" "github.com/go-task/task/v3/internal/logger" "github.com/go-task/task/v3/internal/sort" @@ -77,7 +78,13 @@ func run() error { if err != nil { return err } - if err := task.InitTaskfile(os.Stdout, wd); err != nil { + name := taskfile.DefaultTaskInitFilename + if len(flags.Entrypoint) > 0 { + // Replace `*` with `Taskfile` so `*.yaml` results in `Taskfile.yaml` + name = strings.Replace(flags.Entrypoint, "*", "Taskfile", 1) + } + path := filepathext.SmartJoin(wd, name) + if err := task.InitTaskfile(os.Stdout, path); err != nil { return err } return nil diff --git a/init.go b/init.go index 79d04348bc..0785e17dab 100644 --- a/init.go +++ b/init.go @@ -6,7 +6,6 @@ import ( "os" "github.com/go-task/task/v3/errors" - "github.com/go-task/task/v3/internal/filepathext" ) const defaultTaskfile = `# https://taskfile.dev @@ -23,17 +22,13 @@ tasks: silent: true ` -const defaultTaskfileName = "Taskfile.yml" - // InitTaskfile Taskfile creates a new Taskfile -func InitTaskfile(w io.Writer, dir string) error { - f := filepathext.SmartJoin(dir, defaultTaskfileName) - - if _, err := os.Stat(f); err == nil { +func InitTaskfile(w io.Writer, filepath string) error { + if _, err := os.Stat(filepath); err == nil { return errors.TaskfileAlreadyExistsError{} } - if err := os.WriteFile(f, []byte(defaultTaskfile), 0o644); err != nil { + if err := os.WriteFile(filepath, []byte(defaultTaskfile), 0o644); err != nil { return err } fmt.Fprintf(w, "%s created in the current directory\n", defaultTaskfile) diff --git a/internal/flags/flags.go b/internal/flags/flags.go index 58762c405d..09adfd0b5f 100644 --- a/internal/flags/flags.go +++ b/internal/flags/flags.go @@ -103,7 +103,7 @@ func init() { pflag.BoolVar(&Summary, "summary", false, "Show summary about a task.") pflag.BoolVarP(&ExitCode, "exit-code", "x", false, "Pass-through the exit code of the task command.") pflag.StringVarP(&Dir, "dir", "d", "", "Sets directory of execution.") - pflag.StringVarP(&Entrypoint, "taskfile", "t", "", `Choose which Taskfile to run. Defaults to "Taskfile.yml".`) + pflag.StringVarP(&Entrypoint, "taskfile", "t", "", `Taskfile path to run or initialize. Defaults to "Taskfile.yml".`) pflag.StringVarP(&Output.Name, "output", "o", "", "Sets output style: [interleaved|group|prefixed].") pflag.StringVar(&Output.Group.Begin, "output-group-begin", "", "Message template to print before a task's grouped output.") pflag.StringVar(&Output.Group.End, "output-group-end", "", "Message template to print after a task's grouped output.") diff --git a/task_test.go b/task_test.go index e1a1a918f9..5ae5cd56b9 100644 --- a/task_test.go +++ b/task_test.go @@ -1022,7 +1022,7 @@ func TestInit(t *testing.T) { t.Errorf("Taskfile.yml should not exist") } - if err := task.InitTaskfile(io.Discard, dir); err != nil { + if err := task.InitTaskfile(io.Discard, file); err != nil { t.Error(err) } diff --git a/taskfile/taskfile.go b/taskfile/taskfile.go index d14b760361..0bbb785560 100644 --- a/taskfile/taskfile.go +++ b/taskfile/taskfile.go @@ -16,6 +16,8 @@ import ( "github.com/go-task/task/v3/internal/sysinfo" ) +const DefaultTaskInitFilename = "Taskfile.yml" + var ( defaultTaskfiles = []string{ "Taskfile.yml", From 0ea6e0086c561f06e74a63f4591c4abe6265f6b6 Mon Sep 17 00:00:00 2001 From: HeCorr <75134774+HeCorr@users.noreply.github.com> Date: Fri, 24 Jan 2025 23:15:47 -0300 Subject: [PATCH 02/14] docs: update CLI reference * fix Flags header being inside tip admonition * change -t flag's default column and add a description * add Default Filenames section --- website/docs/reference/cli.mdx | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/website/docs/reference/cli.mdx b/website/docs/reference/cli.mdx index 893236d148..b4a3972afe 100644 --- a/website/docs/reference/cli.mdx +++ b/website/docs/reference/cli.mdx @@ -16,10 +16,10 @@ task [--flags] [tasks...] [-- CLI_ARGS...] If `--` is given, all remaining arguments will be assigned to a special `CLI_ARGS` variable -## Flags - ::: +## Flags + | Short | Flag | Type | Default | Description | | ----- | --------------------------- | -------- | -------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `-c` | `--color` | `bool` | `true` | Colored output. Enabled by default. Set flag to `false` or use `NO_COLOR=1` to disable. | @@ -45,11 +45,30 @@ If `--` is given, all remaining arguments will be assigned to a special | `-y` | `--yes` | `bool` | `false` | Assume "yes" as answer to all prompts. | | | `--status` | `bool` | `false` | Exits with non-zero exit code if any of the given tasks is not up-to-date. | | | `--summary` | `bool` | `false` | Show summary about a task. | -| `-t` | `--taskfile` | `string` | `Taskfile.yml` or `Taskfile.yaml` | | +| `-t` | `--taskfile` | `string` | `Taskfile.yml` | Taskfile path to run or initialize.
The first `*` found when initializing will be replaced by "Taskfile".
Example: `*.yaml` -> `Taskfile.yaml` | | `-v` | `--verbose` | `bool` | `false` | Enables verbose mode. | | | `--version` | `bool` | `false` | Show Task version. | | `-w` | `--watch` | `bool` | `false` | Enables watch of the given task. +## Default Filenames + +Here is a list of default Taskfile names that will be tried in order: + +- `Taskfile.yml` +- `taskfile.yml` +- `Taskfile.yaml` +- `taskfile.yaml` +- `Taskfile.dist.yml` +- `taskfile.dist.yml` +- `Taskfile.dist.yaml` +- `taskfile.dist.yaml` + +:::note + +You can use the `--taskfile` flag to run files of any name. + +::: + ## Exit Codes Task will sometimes exit with specific exit codes. These codes are split into From e4bada3c5bb6c07a9881bd61098c4b645f13f83d Mon Sep 17 00:00:00 2001 From: HeCorr <75134774+HeCorr@users.noreply.github.com> Date: Sat, 25 Jan 2025 23:59:27 -0300 Subject: [PATCH 03/14] docs: revert adding Default Filenames section I didn't realize it already existed elsewhere. --- website/docs/reference/cli.mdx | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/website/docs/reference/cli.mdx b/website/docs/reference/cli.mdx index b4a3972afe..e7176c940a 100644 --- a/website/docs/reference/cli.mdx +++ b/website/docs/reference/cli.mdx @@ -50,25 +50,6 @@ If `--` is given, all remaining arguments will be assigned to a special | | `--version` | `bool` | `false` | Show Task version. | | `-w` | `--watch` | `bool` | `false` | Enables watch of the given task. -## Default Filenames - -Here is a list of default Taskfile names that will be tried in order: - -- `Taskfile.yml` -- `taskfile.yml` -- `Taskfile.yaml` -- `taskfile.yaml` -- `Taskfile.dist.yml` -- `taskfile.dist.yml` -- `Taskfile.dist.yaml` -- `taskfile.dist.yaml` - -:::note - -You can use the `--taskfile` flag to run files of any name. - -::: - ## Exit Codes Task will sometimes exit with specific exit codes. These codes are split into From 8b7e213163ae6826d41d7b7e1058c9157e38de2d Mon Sep 17 00:00:00 2001 From: HeCorr <75134774+HeCorr@users.noreply.github.com> Date: Sun, 26 Jan 2025 00:02:32 -0300 Subject: [PATCH 04/14] refactor: use path instead of filepath on InitTaskFile as requested to prevent ambiguity with the stdlib package. --- init.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/init.go b/init.go index 0785e17dab..14a5b39b3b 100644 --- a/init.go +++ b/init.go @@ -22,13 +22,13 @@ tasks: silent: true ` -// InitTaskfile Taskfile creates a new Taskfile -func InitTaskfile(w io.Writer, filepath string) error { - if _, err := os.Stat(filepath); err == nil { +// InitTaskfile Taskfile creates a new Taskfile at path +func InitTaskfile(w io.Writer, path string) error { + if _, err := os.Stat(path); err == nil { return errors.TaskfileAlreadyExistsError{} } - if err := os.WriteFile(filepath, []byte(defaultTaskfile), 0o644); err != nil { + if err := os.WriteFile(path, []byte(defaultTaskfile), 0o644); err != nil { return err } fmt.Fprintf(w, "%s created in the current directory\n", defaultTaskfile) From b44a517da8dcb63333c3e99dd941373c6fffe4fd Mon Sep 17 00:00:00 2001 From: HeCorr <75134774+HeCorr@users.noreply.github.com> Date: Thu, 30 Jan 2025 19:39:11 -0300 Subject: [PATCH 05/14] fix TestInit (incorrectly merged) --- init_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/init_test.go b/init_test.go index 5f4ccc79b9..4678923874 100644 --- a/init_test.go +++ b/init_test.go @@ -20,7 +20,7 @@ func TestInit(t *testing.T) { t.Errorf("Taskfile.yml should not exist") } - if err := task.InitTaskfile(io.Discard, dir); err != nil { + if err := task.InitTaskfile(io.Discard, file); err != nil { t.Error(err) } From 4b57f11798a21b282b1a2f58d893f826da11e70d Mon Sep 17 00:00:00 2001 From: HeCorr <75134774+HeCorr@users.noreply.github.com> Date: Thu, 30 Jan 2025 21:11:00 -0300 Subject: [PATCH 06/14] docs: remove outdated info on --taskfile flag --- website/docs/reference/cli.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/reference/cli.mdx b/website/docs/reference/cli.mdx index dfa2ab066f..325e5ca99d 100644 --- a/website/docs/reference/cli.mdx +++ b/website/docs/reference/cli.mdx @@ -45,7 +45,7 @@ If `--` is given, all remaining arguments will be assigned to a special | `-y` | `--yes` | `bool` | `false` | Assume "yes" as answer to all prompts. | | | `--status` | `bool` | `false` | Exits with non-zero exit code if any of the given tasks is not up-to-date. | | | `--summary` | `bool` | `false` | Show summary about a task. | -| `-t` | `--taskfile` | `string` | `Taskfile.yml` | Taskfile path to run or initialize.
The first `*` found when initializing will be replaced by "Taskfile".
Example: `*.yaml` -> `Taskfile.yaml` | +| `-t` | `--taskfile` | `string` | | Taskfile path to run. | | `-v` | `--verbose` | `bool` | `false` | Enables verbose mode. | | | `--version` | `bool` | `false` | Show Task version. | | `-w` | `--watch` | `bool` | `false` | Enables watch of the given task. From 20ca1a6b4b87c9020a6bd5379bb09fe2fd868faa Mon Sep 17 00:00:00 2001 From: HeCorr <75134774+HeCorr@users.noreply.github.com> Date: Fri, 31 Jan 2025 00:32:09 -0300 Subject: [PATCH 07/14] refactor task initialization changes - remove const DefaultTaskInitFilename from taskfile/taskfile.go - revert description of Entrypoint flag - make InitTaskfile accept a path to either a file or a directory, and join the default Taskfile name+ext to it if it is a directory - take the target file path from the first argument instead of the Entrypoint flag - detect extension-only filenames (".yaml") instead of replacing "*" with "Taskfile" - use different format in success log so that it makes sense at different paths than the current dir --- cmd/task/task.go | 27 +++++++++++++++++++-------- init.go | 17 +++++++++++++++-- internal/flags/flags.go | 2 +- taskfile/taskfile.go | 2 -- 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/cmd/task/task.go b/cmd/task/task.go index 3ea1c6cef4..b463cc3184 100644 --- a/cmd/task/task.go +++ b/cmd/task/task.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "os" + fp "path/filepath" "strings" "github.com/spf13/pflag" @@ -78,23 +79,33 @@ func run() error { if err != nil { return err } - name := taskfile.DefaultTaskInitFilename - if len(flags.Entrypoint) > 0 { - // Replace `*` with `Taskfile` so `*.yaml` results in `Taskfile.yaml` - name = strings.Replace(flags.Entrypoint, "*", "Taskfile", 1) + args, _, err := getArgs() + if err != nil { + return err + } + name := task.DefaultTaskFilename + path := wd + if len(args) > 0 { + name = args[0] + if fp.Base(name) == fp.Ext(name) { + // File has no name, only extension (i.e. `.yaml`) + // so prepend default file name to it + name = filepathext.SmartJoin(fp.Dir(name), "Taskfile"+fp.Ext(name)) + } + path = filepathext.SmartJoin(wd, name) } - path := filepathext.SmartJoin(wd, name) if err := task.InitTaskfile(os.Stdout, path); err != nil { return err } - if !flags.Silent { if flags.Verbose { log.Outf(logger.Default, "%s\n", task.DefaultTaskfile) } - log.Outf(logger.Green, "%s created in the current directory\n", task.DefaultTaskFilename) + if fi, err := os.Stat(path); err == nil && fi.IsDir() && path != wd { + name = filepathext.SmartJoin(name, task.DefaultTaskFilename) + } + log.Outf(logger.Green, "Taskfile created at %s\n", name) } - return nil } diff --git a/init.go b/init.go index 8dda86cf85..15cc26b09b 100644 --- a/init.go +++ b/init.go @@ -5,6 +5,7 @@ import ( "os" "github.com/go-task/task/v3/errors" + "github.com/go-task/task/v3/internal/filepathext" ) const DefaultTaskfile = `# https://taskfile.dev @@ -23,12 +24,24 @@ tasks: const DefaultTaskFilename = "Taskfile.yml" -// InitTaskfile creates a new Taskfile at path +// InitTaskfile creates a new Taskfile at path. +// +// path can be either a file path or a directory path. +// If path is a directory, path/Taskfile.yml will be created. func InitTaskfile(w io.Writer, path string) error { - if _, err := os.Stat(path); err == nil { + fi, err := os.Stat(path) + if err == nil && !fi.IsDir() { return 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 { + return errors.TaskfileAlreadyExistsError{} + } + } + if err := os.WriteFile(path, []byte(DefaultTaskfile), 0o644); err != nil { return err } diff --git a/internal/flags/flags.go b/internal/flags/flags.go index 09adfd0b5f..58762c405d 100644 --- a/internal/flags/flags.go +++ b/internal/flags/flags.go @@ -103,7 +103,7 @@ func init() { pflag.BoolVar(&Summary, "summary", false, "Show summary about a task.") pflag.BoolVarP(&ExitCode, "exit-code", "x", false, "Pass-through the exit code of the task command.") pflag.StringVarP(&Dir, "dir", "d", "", "Sets directory of execution.") - pflag.StringVarP(&Entrypoint, "taskfile", "t", "", `Taskfile path to run or initialize. Defaults to "Taskfile.yml".`) + pflag.StringVarP(&Entrypoint, "taskfile", "t", "", `Choose which Taskfile to run. Defaults to "Taskfile.yml".`) pflag.StringVarP(&Output.Name, "output", "o", "", "Sets output style: [interleaved|group|prefixed].") pflag.StringVar(&Output.Group.Begin, "output-group-begin", "", "Message template to print before a task's grouped output.") pflag.StringVar(&Output.Group.End, "output-group-end", "", "Message template to print after a task's grouped output.") diff --git a/taskfile/taskfile.go b/taskfile/taskfile.go index 0bbb785560..d14b760361 100644 --- a/taskfile/taskfile.go +++ b/taskfile/taskfile.go @@ -16,8 +16,6 @@ import ( "github.com/go-task/task/v3/internal/sysinfo" ) -const DefaultTaskInitFilename = "Taskfile.yml" - var ( defaultTaskfiles = []string{ "Taskfile.yml", From c7b239ac2c49b9b1df24a65f558df6e307a3d04c Mon Sep 17 00:00:00 2001 From: HeCorr <75134774+HeCorr@users.noreply.github.com> Date: Fri, 31 Jan 2025 00:48:33 -0300 Subject: [PATCH 08/14] print colon instead of "at" it's a lot cleaner in most cases. --- cmd/task/task.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/task/task.go b/cmd/task/task.go index b463cc3184..af41bc2d6e 100644 --- a/cmd/task/task.go +++ b/cmd/task/task.go @@ -104,7 +104,7 @@ func run() error { if fi, err := os.Stat(path); err == nil && fi.IsDir() && path != wd { name = filepathext.SmartJoin(name, task.DefaultTaskFilename) } - log.Outf(logger.Green, "Taskfile created at %s\n", name) + log.Outf(logger.Green, "Taskfile created: %s\n", name) } return nil } From 7871a9d9c82343f73b5cfc4ec88910c7cfc9daae Mon Sep 17 00:00:00 2001 From: HeCorr <75134774+HeCorr@users.noreply.github.com> Date: Fri, 31 Jan 2025 01:27:23 -0300 Subject: [PATCH 09/14] rewrite init tests test both initializing to a directory path and a file path --- init_test.go | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/init_test.go b/init_test.go index 4678923874..a091bbaedc 100644 --- a/init_test.go +++ b/init_test.go @@ -9,7 +9,7 @@ import ( "github.com/go-task/task/v3/internal/filepathext" ) -func TestInit(t *testing.T) { +func TestInitDir(t *testing.T) { t.Parallel() const dir = "testdata/init" @@ -20,12 +20,34 @@ func TestInit(t *testing.T) { t.Errorf("Taskfile.yml should not exist") } - if err := task.InitTaskfile(io.Discard, file); err != nil { + if err := task.InitTaskfile(io.Discard, dir); err != nil { t.Error(err) } if _, err := os.Stat(file); err != nil { t.Errorf("Taskfile.yml should exist") } + + _ = os.Remove(file) +} + +func TestInitFile(t *testing.T) { + t.Parallel() + + const dir = "testdata/init" + file := filepathext.SmartJoin(dir, "Tasks.yml") + + _ = os.Remove(file) + if _, err := os.Stat(file); err == nil { + t.Errorf("Tasks.yml should not exist") + } + + if err := task.InitTaskfile(io.Discard, file); err != nil { + t.Error(err) + } + + if _, err := os.Stat(file); err != nil { + t.Errorf("Tasks.yml should exist") + } _ = os.Remove(file) } From eecf72d1cda4c0e07833cce3a530e39d2547d456 Mon Sep 17 00:00:00 2001 From: HeCorr <75134774+HeCorr@users.noreply.github.com> Date: Mon, 3 Feb 2025 01:46:15 -0300 Subject: [PATCH 10/14] return final path from InitTaskfile ...and print it's relative representation --- cmd/task/task.go | 10 ++++++---- init.go | 12 +++++++----- init_test.go | 4 ++-- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/cmd/task/task.go b/cmd/task/task.go index af41bc2d6e..c4ff923fa2 100644 --- a/cmd/task/task.go +++ b/cmd/task/task.go @@ -94,17 +94,19 @@ func run() error { } path = filepathext.SmartJoin(wd, name) } - if err := task.InitTaskfile(os.Stdout, path); err != nil { + finalPath, err := task.InitTaskfile(os.Stdout, path) + if err != nil { return err } if !flags.Silent { if flags.Verbose { log.Outf(logger.Default, "%s\n", task.DefaultTaskfile) } - if fi, err := os.Stat(path); err == nil && fi.IsDir() && path != wd { - name = filepathext.SmartJoin(name, task.DefaultTaskFilename) + relPath, err := fp.Rel(wd, finalPath) + if err != nil { + relPath = finalPath } - log.Outf(logger.Green, "Taskfile created: %s\n", name) + log.Outf(logger.Green, "Taskfile created: %s\n", relPath) } return nil } diff --git a/init.go b/init.go index 15cc26b09b..04f0c0b766 100644 --- a/init.go +++ b/init.go @@ -28,23 +28,25 @@ const DefaultTaskFilename = "Taskfile.yml" // // path can be either a file path or a directory path. // If path is a directory, path/Taskfile.yml will be created. -func InitTaskfile(w io.Writer, path string) error { +// +// The final file path is always returned and may be different from the input path. +func InitTaskfile(w io.Writer, path string) (string, error) { fi, err := os.Stat(path) if err == nil && !fi.IsDir() { - return errors.TaskfileAlreadyExistsError{} + 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 { - return errors.TaskfileAlreadyExistsError{} + return path, errors.TaskfileAlreadyExistsError{} } } if err := os.WriteFile(path, []byte(DefaultTaskfile), 0o644); err != nil { - return err + return path, err } - return nil + return path, nil } diff --git a/init_test.go b/init_test.go index a091bbaedc..ce401c0881 100644 --- a/init_test.go +++ b/init_test.go @@ -20,7 +20,7 @@ func TestInitDir(t *testing.T) { t.Errorf("Taskfile.yml should not exist") } - if err := task.InitTaskfile(io.Discard, dir); err != nil { + if _, err := task.InitTaskfile(io.Discard, dir); err != nil { t.Error(err) } @@ -42,7 +42,7 @@ func TestInitFile(t *testing.T) { t.Errorf("Tasks.yml should not exist") } - if err := task.InitTaskfile(io.Discard, file); err != nil { + if _, err := task.InitTaskfile(io.Discard, file); err != nil { t.Error(err) } From e35df16e2260672c3f65c6b86661f465927fd2ec Mon Sep 17 00:00:00 2001 From: HeCorr <75134774+HeCorr@users.noreply.github.com> Date: Mon, 3 Feb 2025 02:04:47 -0300 Subject: [PATCH 11/14] fix lint error (ineffassign) --- cmd/task/task.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cmd/task/task.go b/cmd/task/task.go index c4ff923fa2..07e7c36ba1 100644 --- a/cmd/task/task.go +++ b/cmd/task/task.go @@ -83,10 +83,9 @@ func run() error { if err != nil { return err } - name := task.DefaultTaskFilename path := wd if len(args) > 0 { - name = args[0] + name := args[0] if fp.Base(name) == fp.Ext(name) { // File has no name, only extension (i.e. `.yaml`) // so prepend default file name to it From 450a61aa89e76a9351d69635ee9091e537f6b8fc Mon Sep 17 00:00:00 2001 From: HeCorr <75134774+HeCorr@users.noreply.github.com> Date: Mon, 3 Feb 2025 02:12:01 -0300 Subject: [PATCH 12/14] use filepathext.TryAbsToRel() instead --- cmd/task/task.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/cmd/task/task.go b/cmd/task/task.go index 07e7c36ba1..217c9c7af4 100644 --- a/cmd/task/task.go +++ b/cmd/task/task.go @@ -101,11 +101,7 @@ func run() error { if flags.Verbose { log.Outf(logger.Default, "%s\n", task.DefaultTaskfile) } - relPath, err := fp.Rel(wd, finalPath) - if err != nil { - relPath = finalPath - } - log.Outf(logger.Green, "Taskfile created: %s\n", relPath) + log.Outf(logger.Green, "Taskfile created: %s\n", filepathext.TryAbsToRel(finalPath)) } return nil } From 371f881c7609a0f560aae308826d34209f063325 Mon Sep 17 00:00:00 2001 From: HeCorr <75134774+HeCorr@users.noreply.github.com> Date: Mon, 3 Feb 2025 02:28:15 -0300 Subject: [PATCH 13/14] define and use filepathext.IsExtOnly() --- cmd/task/task.go | 4 +--- internal/filepathext/filepathext.go | 6 ++++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/cmd/task/task.go b/cmd/task/task.go index 217c9c7af4..aeb57e99cb 100644 --- a/cmd/task/task.go +++ b/cmd/task/task.go @@ -86,9 +86,7 @@ func run() error { path := wd if len(args) > 0 { name := args[0] - if fp.Base(name) == fp.Ext(name) { - // File has no name, only extension (i.e. `.yaml`) - // so prepend default file name to it + if filepathext.IsExtOnly(name) { name = filepathext.SmartJoin(fp.Dir(name), "Taskfile"+fp.Ext(name)) } path = filepathext.SmartJoin(wd, name) diff --git a/internal/filepathext/filepathext.go b/internal/filepathext/filepathext.go index db64695c8e..f2a1ba15a0 100644 --- a/internal/filepathext/filepathext.go +++ b/internal/filepathext/filepathext.go @@ -55,3 +55,9 @@ func TryAbsToRel(abs string) string { return rel } + +// IsExtOnly checks whether path points to a file with no name but with +// an extension, i.e. ".yaml" +func IsExtOnly(path string) bool { + return filepath.Base(path) == filepath.Ext(path) +} From 07c12f8156fdace061d84deb2b8463520a86cb30 Mon Sep 17 00:00:00 2001 From: HeCorr <75134774+HeCorr@users.noreply.github.com> Date: Mon, 3 Feb 2025 02:47:38 -0300 Subject: [PATCH 14/14] link to default filenames list in cli ref docs (specifically in the --taskfile flag description) --- website/docs/reference/cli.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/reference/cli.mdx b/website/docs/reference/cli.mdx index 325e5ca99d..4afe84b836 100644 --- a/website/docs/reference/cli.mdx +++ b/website/docs/reference/cli.mdx @@ -45,7 +45,7 @@ If `--` is given, all remaining arguments will be assigned to a special | `-y` | `--yes` | `bool` | `false` | Assume "yes" as answer to all prompts. | | | `--status` | `bool` | `false` | Exits with non-zero exit code if any of the given tasks is not up-to-date. | | | `--summary` | `bool` | `false` | Show summary about a task. | -| `-t` | `--taskfile` | `string` | | Taskfile path to run. | +| `-t` | `--taskfile` | `string` | | Taskfile path to run.
Check the list of default filenames [here](../usage/#supported-file-names). | | `-v` | `--verbose` | `bool` | `false` | Enables verbose mode. | | | `--version` | `bool` | `false` | Show Task version. | | `-w` | `--watch` | `bool` | `false` | Enables watch of the given task.