mirror of
https://github.com/go-task/task.git
synced 2025-12-16 11:47:44 +01:00
fix: a couple of fixes and improvements on task --init (#2433)
* 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). * Changed top comment to YAML language server comment.
This commit is contained in:
25
init.go
25
init.go
@@ -6,9 +6,10 @@ import (
|
|||||||
|
|
||||||
"github.com/go-task/task/v3/errors"
|
"github.com/go-task/task/v3/errors"
|
||||||
"github.com/go-task/task/v3/internal/filepathext"
|
"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
|
//go:embed taskfile/templates/default.yml
|
||||||
var DefaultTaskfile string
|
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.
|
// The final file path is always returned and may be different from the input path.
|
||||||
func InitTaskfile(path string) (string, error) {
|
func InitTaskfile(path string) (string, error) {
|
||||||
fi, err := os.Stat(path)
|
info, err := os.Stat(path)
|
||||||
if err == nil && !fi.IsDir() {
|
if err == nil && !info.IsDir() {
|
||||||
return path, errors.TaskfileAlreadyExistsError{}
|
return path, errors.TaskfileAlreadyExistsError{}
|
||||||
}
|
}
|
||||||
|
|
||||||
if fi != nil && fi.IsDir() {
|
if info != nil && info.IsDir() {
|
||||||
path = filepathext.SmartJoin(path, defaultTaskFilename)
|
// path was a directory, check if there is a Taskfile already
|
||||||
// path was a directory, so check if Taskfile.yml exists in it
|
if hasDefaultTaskfile(path) {
|
||||||
if _, err := os.Stat(path); err == nil {
|
|
||||||
return path, errors.TaskfileAlreadyExistsError{}
|
return path, errors.TaskfileAlreadyExistsError{}
|
||||||
}
|
}
|
||||||
|
path = filepathext.SmartJoin(path, defaultFilename)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := os.WriteFile(path, []byte(DefaultTaskfile), 0o644); err != nil {
|
if err := os.WriteFile(path, []byte(DefaultTaskfile), 0o644); err != nil {
|
||||||
return path, err
|
return path, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return path, nil
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ type FileNode struct {
|
|||||||
|
|
||||||
func NewFileNode(entrypoint, dir string, opts ...NodeOption) (*FileNode, error) {
|
func NewFileNode(entrypoint, dir string, opts ...NodeOption) (*FileNode, error) {
|
||||||
// Find the entrypoint file
|
// Find the entrypoint file
|
||||||
resolvedEntrypoint, err := fsext.Search(entrypoint, dir, defaultTaskfiles)
|
resolvedEntrypoint, err := fsext.Search(entrypoint, dir, DefaultTaskfiles)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, os.ErrNotExist) {
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
return nil, errors.TaskfileNotFoundError{URI: entrypoint, Walk: false}
|
return nil, errors.TaskfileNotFoundError{URI: entrypoint, Walk: false}
|
||||||
|
|||||||
@@ -12,7 +12,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
defaultTaskfiles = []string{
|
// DefaultTaskfiles is the list of Taskfile file names supported by default.
|
||||||
|
DefaultTaskfiles = []string{
|
||||||
"Taskfile.yml",
|
"Taskfile.yml",
|
||||||
"taskfile.yml",
|
"taskfile.yml",
|
||||||
"Taskfile.yaml",
|
"Taskfile.yaml",
|
||||||
@@ -67,7 +68,7 @@ func RemoteExists(ctx context.Context, u url.URL) (*url.URL, error) {
|
|||||||
|
|
||||||
// If the request was not successful, append the default Taskfile names to
|
// If the request was not successful, append the default Taskfile names to
|
||||||
// the URL and return the URL of the first successful request
|
// 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
|
// Fixes a bug with JoinPath where a leading slash is not added to the
|
||||||
// path if it is empty
|
// path if it is empty
|
||||||
if u.Path == "" {
|
if u.Path == "" {
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
# https://taskfile.dev
|
# yaml-language-server: $schema=https://taskfile.dev/schema.json
|
||||||
|
|
||||||
version: '3'
|
version: '3'
|
||||||
|
|
||||||
vars:
|
vars:
|
||||||
GREETING: Hello, World!
|
GREETING: Hello, world!
|
||||||
|
|
||||||
tasks:
|
tasks:
|
||||||
default:
|
default:
|
||||||
|
desc: Print a greeting message
|
||||||
cmds:
|
cmds:
|
||||||
- echo "{{.GREETING}}"
|
- echo "{{.GREETING}}"
|
||||||
silent: true
|
silent: true
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ vars:
|
|||||||
|
|
||||||
tasks:
|
tasks:
|
||||||
default:
|
default:
|
||||||
|
desc: Print a greeting message
|
||||||
cmds:
|
cmds:
|
||||||
- echo "{{.GREETING}}"
|
- echo "{{.GREETING}}"
|
||||||
silent: true
|
silent: true
|
||||||
@@ -111,6 +112,7 @@ vars:
|
|||||||
|
|
||||||
tasks:
|
tasks:
|
||||||
default:
|
default:
|
||||||
|
desc: Print a greeting message
|
||||||
cmds:
|
cmds:
|
||||||
- echo "{{.GREETING}}"
|
- echo "{{.GREETING}}"
|
||||||
silent: true
|
silent: true
|
||||||
|
|||||||
Reference in New Issue
Block a user