Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: specify --init filename/path #2018

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
9 changes: 8 additions & 1 deletion cmd/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
HeCorr marked this conversation as resolved.
Show resolved Hide resolved
path := filepathext.SmartJoin(wd, name)
if err := task.InitTaskfile(os.Stdout, path); err != nil {
return err
}
return nil
Expand Down
11 changes: 3 additions & 8 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
HeCorr marked this conversation as resolved.
Show resolved Hide resolved
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)
Expand Down
2 changes: 1 addition & 1 deletion internal/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
2 changes: 1 addition & 1 deletion task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 2 additions & 0 deletions taskfile/taskfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"github.com/go-task/task/v3/internal/sysinfo"
)

const DefaultTaskInitFilename = "Taskfile.yml"
HeCorr marked this conversation as resolved.
Show resolved Hide resolved

var (
defaultTaskfiles = []string{
"Taskfile.yml",
Expand Down
25 changes: 22 additions & 3 deletions website/docs/reference/cli.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand All @@ -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.<br/>The first `*` found when initializing will be replaced by "Taskfile".<br/>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.

:::

HeCorr marked this conversation as resolved.
Show resolved Hide resolved
## Exit Codes

Task will sometimes exit with specific exit codes. These codes are split into
Expand Down
Loading