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

42 lines
1.0 KiB
Go
Raw Normal View History

package task
import (
_ "embed"
"os"
2023-04-15 21:22:25 +01:00
"github.com/go-task/task/v3/errors"
"github.com/go-task/task/v3/internal/filepathext"
)
const defaultTaskFilename = "Taskfile.yml"
//go:embed taskfile/templates/default.yml
var DefaultTaskfile string
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)
2025-02-10 08:22:49 -03:00
// 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(path string) (string, error) {
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)
2025-02-10 08:22:49 -03:00
fi, err := os.Stat(path)
if err == nil && !fi.IsDir() {
return path, errors.TaskfileAlreadyExistsError{}
}
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)
2025-02-10 08:22:49 -03:00
if fi != nil && fi.IsDir() {
path = filepathext.SmartJoin(path, defaultTaskFilename)
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)
2025-02-10 08:22:49 -03:00
// path was a directory, so check if Taskfile.yml exists in it
if _, err := os.Stat(path); err == nil {
return path, errors.TaskfileAlreadyExistsError{}
}
}
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)
2025-02-10 08:22:49 -03:00
if err := os.WriteFile(path, []byte(DefaultTaskfile), 0o644); err != nil {
return path, err
}
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)
2025-02-10 08:22:49 -03:00
return path, nil
}