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

Conversation

HeCorr
Copy link
Contributor

@HeCorr HeCorr commented Jan 25, 2025

Closes #2008.

Task currently does not provide a way to specify which filename to use when initializing a file (hardcoded as Taskfile.yml), forcing users to rename the file/extension after creation.

This PR fixes that by taking the target filename from the first non-flag argument:

$ task --init tasks.yml
Taskfile created: tasks.yml

If a path with no name (extension only) is provided, the default name "Taskfile" is prepended to it:

$ task --init .yaml
Taskfile created: Taskfile.yaml

Different directories are also supported:

$ task --init sub/NotMakefile.yml
Taskfile created: sub/NotMakefile.yml
$ task --init sub/.yaml
Taskfile created: sub/Taskfile.yaml

This PR also includes the following updates to the docs:

  • Fixed the Flags header being inside the Tip admonition in CLI Reference page:

image image

  • Updated the --taskfile flag information:

image
image

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.
* fix Flags header being inside tip admonition
* change -t flag's default column and add a description
* add Default Filenames section
@HeCorr HeCorr marked this pull request as draft January 25, 2025 03:42
Copy link
Member

@pd93 pd93 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this! Generally supportive of this improvement. Just a couple of discussion points on the implementation/docs.

website/docs/reference/cli.mdx Outdated Show resolved Hide resolved
taskfile/taskfile.go Outdated Show resolved Hide resolved
init.go Outdated Show resolved Hide resolved
cmd/task/task.go Outdated Show resolved Hide resolved
I didn't realize it already existed elsewhere.
as requested to prevent ambiguity with the stdlib package.
@HeCorr HeCorr changed the title feat: specify init filename with --taskfile flag feat: specify --init filename and path Jan 30, 2025
@HeCorr

This comment was marked as resolved.

@vmaerten
Copy link
Member

@pd93 not sure why TestInit is failing now. It didn't fail in #2011, did it?

It didn't. It fails because, previously in the test, we passed the directory containing the Taskfile as the second parameter, and we concatenated the filename in the InitTaskfile method.
Now, the concatenation has been removed, os.stat is performed on the directory itself, and it already exists
It prevents the InitTaskfile to override existing file.

(see :

if err := task.InitTaskfile(io.Discard, dir); err != nil {
and https://github.com/go-task/task/pull/2018/files#diff-09d6aefefdc39400d949bac4606f951988cbe618740e96ecfee50236859da58bR26-R32)

@HeCorr
Copy link
Contributor Author

HeCorr commented Jan 30, 2025

oh, oops. I must have messed up the merge then. thanks.

@HeCorr

This comment was marked as resolved.

@HeCorr

This comment was marked as resolved.

- 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
it's a lot cleaner in most cases.
@HeCorr HeCorr changed the title feat: specify --init filename and path feat: specify --init filename/path Jan 31, 2025
@HeCorr
Copy link
Contributor Author

HeCorr commented Jan 31, 2025

Refactor complete, main post updated, manual tests passing, ready to remove Draft status.

Tested commands:

Command Result
task --init Taskfile created: Taskfile.yml
task --init tasks.yml Taskfile created: tasks.yml
task --init .yaml Taskfile created: Taskfile.yaml
task --init sub/ Taskfile created: sub/Taskfile.yml
task --init sub/tasks.yml Taskfile created: sub/tasks.yml
task --init sub/.yaml Taskfile created: sub/Taskfile.yaml

@HeCorr HeCorr marked this pull request as ready for review January 31, 2025 04:07
test both initializing to a directory path and a file path
...and print it's relative representation
@HeCorr
Copy link
Contributor Author

HeCorr commented Feb 3, 2025

Modified InitTaskfile() to return the final calculated file path as suggested, which was honestly the right call as that may also help with error handling in case initialization fails.

Only a small quirk with the current implementation which prints the path relative to the current dir is that this may be printed depending on the input path:

$ task --init ~/Desktop
Taskfile created: ../../../../../../Desktop/Taskfile.yml

...but it is generally much shorter:

$ task --init
Taskfile created: Taskfile.yml

@HeCorr
Copy link
Contributor Author

HeCorr commented Feb 3, 2025

Updated the docs one last time as well as the original PR message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Specify --init filename/extension
4 participants