1
0
mirror of https://github.com/go-task/task.git synced 2025-04-25 12:25:07 +02:00

feat: specify --init filename/path (#2018)

* 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.

* 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

* docs: revert adding Default Filenames section

I didn't realize it already existed elsewhere.

* refactor: use path instead of filepath on InitTaskFile

as requested to prevent ambiguity with the stdlib package.

* fix TestInit (incorrectly merged)

* docs: remove outdated info on --taskfile flag

* 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

* print colon instead of "at"

it's a lot cleaner in most cases.

* rewrite init tests

test both initializing to a directory path and a file path

* return final path from InitTaskfile

...and print it's relative representation

* fix lint error (ineffassign)

* use filepathext.TryAbsToRel() instead

* define and use filepathext.IsExtOnly()

* link to default filenames list in cli ref docs

(specifically in the --taskfile flag description)
This commit is contained in:
Henrique Corrêa 2025-02-10 08:22:49 -03:00 committed by GitHub
parent 65a64a01ee
commit 0e23404d23
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 71 additions and 19 deletions

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"os" "os"
fp "path/filepath"
"strings" "strings"
"github.com/spf13/pflag" "github.com/spf13/pflag"
@ -13,6 +14,7 @@ import (
"github.com/go-task/task/v3/args" "github.com/go-task/task/v3/args"
"github.com/go-task/task/v3/errors" "github.com/go-task/task/v3/errors"
"github.com/go-task/task/v3/internal/experiments" "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/flags"
"github.com/go-task/task/v3/internal/logger" "github.com/go-task/task/v3/internal/logger"
"github.com/go-task/task/v3/internal/sort" "github.com/go-task/task/v3/internal/sort"
@ -77,18 +79,28 @@ func run() error {
if err != nil { if err != nil {
return err return err
} }
args, _, err := getArgs()
if err := task.InitTaskfile(os.Stdout, wd); err != nil { if err != nil {
return err
}
path := wd
if len(args) > 0 {
name := args[0]
if filepathext.IsExtOnly(name) {
name = filepathext.SmartJoin(fp.Dir(name), "Taskfile"+fp.Ext(name))
}
path = filepathext.SmartJoin(wd, name)
}
finalPath, err := task.InitTaskfile(os.Stdout, path)
if err != nil {
return err return err
} }
if !flags.Silent { if !flags.Silent {
if flags.Verbose { if flags.Verbose {
log.Outf(logger.Default, "%s\n", task.DefaultTaskfile) log.Outf(logger.Default, "%s\n", task.DefaultTaskfile)
} }
log.Outf(logger.Green, "%s created in the current directory\n", task.DefaultTaskFilename) log.Outf(logger.Green, "Taskfile created: %s\n", filepathext.TryAbsToRel(finalPath))
} }
return nil return nil
} }

30
init.go
View File

@ -24,17 +24,29 @@ tasks:
const DefaultTaskFilename = "Taskfile.yml" const DefaultTaskFilename = "Taskfile.yml"
// InitTaskfile creates a new Taskfile // InitTaskfile creates a new Taskfile at path.
func InitTaskfile(w io.Writer, dir string) error { //
f := filepathext.SmartJoin(dir, DefaultTaskFilename) // path can be either a file path or a directory path.
// If path is a directory, path/Taskfile.yml will be created.
if _, err := os.Stat(f); err == nil { //
return errors.TaskfileAlreadyExistsError{} // 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 path, errors.TaskfileAlreadyExistsError{}
} }
if err := os.WriteFile(f, []byte(DefaultTaskfile), 0o644); err != nil { if fi != nil && fi.IsDir() {
return err 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 path, errors.TaskfileAlreadyExistsError{}
}
} }
return nil if err := os.WriteFile(path, []byte(DefaultTaskfile), 0o644); err != nil {
return path, err
}
return path, nil
} }

View File

@ -9,7 +9,7 @@ import (
"github.com/go-task/task/v3/internal/filepathext" "github.com/go-task/task/v3/internal/filepathext"
) )
func TestInit(t *testing.T) { func TestInitDir(t *testing.T) {
t.Parallel() t.Parallel()
const dir = "testdata/init" const dir = "testdata/init"
@ -20,12 +20,34 @@ func TestInit(t *testing.T) {
t.Errorf("Taskfile.yml should not exist") 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) t.Error(err)
} }
if _, err := os.Stat(file); err != nil { if _, err := os.Stat(file); err != nil {
t.Errorf("Taskfile.yml should exist") 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) _ = os.Remove(file)
} }

View File

@ -55,3 +55,9 @@ func TryAbsToRel(abs string) string {
return rel 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)
}

View File

@ -16,10 +16,10 @@ task [--flags] [tasks...] [-- CLI_ARGS...]
If `--` is given, all remaining arguments will be assigned to a special If `--` is given, all remaining arguments will be assigned to a special
`CLI_ARGS` variable `CLI_ARGS` variable
## Flags
::: :::
## Flags
| Short | Flag | Type | Default | Description | | 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. | | `-c` | `--color` | `bool` | `true` | Colored output. Enabled by default. Set flag to `false` or use `NO_COLOR=1` to disable. |
@ -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. | | `-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. | | | `--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. | | | `--summary` | `bool` | `false` | Show summary about a task. |
| `-t` | `--taskfile` | `string` | `Taskfile.yml` or `Taskfile.yaml` | | | `-t` | `--taskfile` | `string` | | Taskfile path to run.<br />Check the list of default filenames [here](../usage/#supported-file-names). |
| `-v` | `--verbose` | `bool` | `false` | Enables verbose mode. | | `-v` | `--verbose` | `bool` | `false` | Enables verbose mode. |
| | `--version` | `bool` | `false` | Show Task version. | | | `--version` | `bool` | `false` | Show Task version. |
| `-w` | `--watch` | `bool` | `false` | Enables watch of the given task. | `-w` | `--watch` | `bool` | `false` | Enables watch of the given task.