mirror of
https://github.com/go-task/task.git
synced 2025-12-14 18:57:43 +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/internal/filepathext"
|
||||
"github.com/go-task/task/v3/taskfile"
|
||||
)
|
||||
|
||||
const defaultTaskFilename = "Taskfile.yml"
|
||||
const defaultFilename = "Taskfile.yml"
|
||||
|
||||
//go:embed taskfile/templates/default.yml
|
||||
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.
|
||||
func InitTaskfile(path string) (string, error) {
|
||||
fi, err := os.Stat(path)
|
||||
if err == nil && !fi.IsDir() {
|
||||
info, err := os.Stat(path)
|
||||
if err == nil && !info.IsDir() {
|
||||
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 {
|
||||
if info != nil && info.IsDir() {
|
||||
// path was a directory, check if there is a Taskfile already
|
||||
if hasDefaultTaskfile(path) {
|
||||
return path, errors.TaskfileAlreadyExistsError{}
|
||||
}
|
||||
path = filepathext.SmartJoin(path, defaultFilename)
|
||||
}
|
||||
|
||||
if err := os.WriteFile(path, []byte(DefaultTaskfile), 0o644); err != nil {
|
||||
return path, err
|
||||
}
|
||||
|
||||
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) {
|
||||
// Find the entrypoint file
|
||||
resolvedEntrypoint, err := fsext.Search(entrypoint, dir, defaultTaskfiles)
|
||||
resolvedEntrypoint, err := fsext.Search(entrypoint, dir, DefaultTaskfiles)
|
||||
if err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return nil, errors.TaskfileNotFoundError{URI: entrypoint, Walk: false}
|
||||
|
||||
@@ -12,7 +12,8 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
defaultTaskfiles = []string{
|
||||
// DefaultTaskfiles is the list of Taskfile file names supported by default.
|
||||
DefaultTaskfiles = []string{
|
||||
"Taskfile.yml",
|
||||
"taskfile.yml",
|
||||
"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
|
||||
// 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
|
||||
// path if it is empty
|
||||
if u.Path == "" {
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
# https://taskfile.dev
|
||||
# yaml-language-server: $schema=https://taskfile.dev/schema.json
|
||||
|
||||
version: '3'
|
||||
|
||||
vars:
|
||||
GREETING: Hello, World!
|
||||
GREETING: Hello, world!
|
||||
|
||||
tasks:
|
||||
default:
|
||||
desc: Print a greeting message
|
||||
cmds:
|
||||
- echo "{{.GREETING}}"
|
||||
silent: true
|
||||
|
||||
@@ -43,6 +43,7 @@ vars:
|
||||
|
||||
tasks:
|
||||
default:
|
||||
desc: Print a greeting message
|
||||
cmds:
|
||||
- echo "{{.GREETING}}"
|
||||
silent: true
|
||||
@@ -111,6 +112,7 @@ vars:
|
||||
|
||||
tasks:
|
||||
default:
|
||||
desc: Print a greeting message
|
||||
cmds:
|
||||
- echo "{{.GREETING}}"
|
||||
silent: true
|
||||
|
||||
Reference in New Issue
Block a user