1
0
mirror of https://github.com/go-task/task.git synced 2025-05-13 22:16:31 +02:00
task/internal/filepathext/filepathext.go

64 lines
1.2 KiB
Go
Raw Permalink Normal View History

package filepathext
import (
2022-10-14 19:48:45 -03:00
"os"
"path/filepath"
"strings"
)
// SmartJoin joins two paths, but only if the second is not already an
// absolute path.
func SmartJoin(a, b string) string {
if IsAbs(b) {
return b
}
return filepath.Join(a, b)
}
2022-10-14 19:48:45 -03:00
func IsAbs(path string) bool {
// NOTE(@andreynering): If the path contains any if the special
// variables that we know are absolute, return true.
if isSpecialDir(path) {
return true
}
return filepath.IsAbs(path)
}
var knownAbsDirs = []string{
".ROOT_DIR",
".TASKFILE_DIR",
".USER_WORKING_DIR",
}
func isSpecialDir(dir string) bool {
for _, d := range knownAbsDirs {
if strings.Contains(dir, d) {
return true
}
}
return false
}
2022-10-14 19:48:45 -03:00
// TryAbsToRel tries to convert an absolute path to relative based on the
// process working directory. If it can't, it returns the absolute path.
func TryAbsToRel(abs string) string {
wd, err := os.Getwd()
if err != nil {
return abs
}
rel, err := filepath.Rel(wd, abs)
if err != nil {
return abs
}
return rel
}
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
// 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)
}