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
22 changes: 17 additions & 5 deletions cmd/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
fp "path/filepath"
"strings"

"github.com/spf13/pflag"
Expand All @@ -13,6 +14,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,18 +79,28 @@ func run() error {
if err != nil {
return err
}

if err := task.InitTaskfile(os.Stdout, wd); err != nil {
args, _, err := getArgs()
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
}

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)
log.Outf(logger.Green, "Taskfile created: %s\n", filepathext.TryAbsToRel(finalPath))
}

return nil
}

Expand Down
28 changes: 20 additions & 8 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,29 @@ tasks:

const DefaultTaskFilename = "Taskfile.yml"

// InitTaskfile creates a new Taskfile
func InitTaskfile(w io.Writer, dir string) error {
f := filepathext.SmartJoin(dir, DefaultTaskFilename)
// 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.
//
// 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.Stat(f); err == nil {
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 path, errors.TaskfileAlreadyExistsError{}
}
}

if err := os.WriteFile(f, []byte(DefaultTaskfile), 0o644); err != nil {
return err
if err := os.WriteFile(path, []byte(DefaultTaskfile), 0o644); err != nil {
return path, err
}

return nil
return path, nil
}
26 changes: 24 additions & 2 deletions init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -20,12 +20,34 @@ 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, 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)
}
6 changes: 6 additions & 0 deletions internal/filepathext/filepathext.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
6 changes: 3 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,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` 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. |
| | `--version` | `bool` | `false` | Show Task version. |
| `-w` | `--watch` | `bool` | `false` | Enables watch of the given task.
Expand Down
Loading