feat: add joinEnv and joinUrl string functions and 2 new system vars #2408
Conversation
trulede
left a comment
There was a problem hiding this comment.
@solvingj there are quite a few functions already in https://go-task.github.io/slim-sprig/ and perhaps its better to use those existing list functions (join, append, prepend) since that gives a better control over how such lists/strings are generated.
The PathSeparator and PathListSeparator are useful however I would suggest following the existing most-predominate naming convention:
- PathSeparator -> osPathSeparator (note also the absence of "file")
- PathListSeparator -> osPathListSeparator
since the allcaps just looks ODD.
Observation: this could also be done with a .env file, introducing these constants (and others). Perhaps this PR is more effort than value.
|
Up to @pd93 and @andreynering I suppose. |
vmaerten
left a comment
There was a problem hiding this comment.
Hello!
Sorry for the delay, we have been busy lately.
PATH_LIST_SEPARATOR / FILE_PATH_SEPARATOR does not work in the templating system.
If you take this Taskfile :
# https://taskfile.dev
version: '3'
tasks:
default:
cmds:
- echo "{{.PATH_LIST_SEPARATOR}}"
- echo "{{.FILE_PATH_SEPARATOR}}"
You will get this result:
For these variables, you need to define them in getSpecialVars inside compiler.go
Ah, of course, moved... |
|
@solvingj the lint is failing. Do you still want to work on it? |
|
Yah I can fix it tomorrow. Just to confirm, are you sure you want to merge it? |
| return true | ||
| } | ||
|
|
||
| func joinEnv(elem ...string) string { |
There was a problem hiding this comment.
Why is this called joinEnv? It has nothing to do with environment.
There is already a join function in slim-sprig which takes a second argument for the separator. Would this not be more suitable, along with the added seperator (in this PR).
There was a problem hiding this comment.
Why is this called
joinEnv? It has nothing to do with environment.
os.PathListSeparator is abstract rune which resolves to the character ":" when compiled on posix operating systems, and ";" on windows. It represents the special value used by these operating systems when parsing 1 or more filesystem paths from an environment variable, such as in the special case of PATH.
This function returns a string, which is the combination of input strings, but with the appropriate platform-specific character placed in-between each. In this way, it returns a string which is valid to be assigned to an environment variable which is designated to hold 1 or more filesystem paths.
Thus, this function is related to environment variables, and that is why it was called joinEnv.
I apologize for not explaining this more clearly in the original description.
| } | ||
|
|
||
| func joinUrl(elem ...string) string { | ||
| return path.Join(elem...) |
There was a problem hiding this comment.
There is already a joinPath function. Any reason not to use that? It calls filepath.Join.
Also, there is an existing urlJoin which is actually joining a URL. And a corresponding urlParse. Those are both working with URLs, the function presented here is not...
There was a problem hiding this comment.
There is already a
joinPathfunction. Any reason not to use that? It callsfilepath.Join.
filepath.Join is abstract method which takes 1 or more strings understood to be filesystem paths, and combines them by putting the platform-specific separator in-between each element. When compiled on posix operating systems, the separator used is "/", when compiled on windows, the separator is "\".
When joining path elements for a URL, the inputs must be combined together, with the character "/" placed in-between each element. The path.Join function does this. The use of joinPath on posix operating-systems returns a valid URL with the "/" character, however when called on windows with the same inputs, the function returns a string with the separator "\" placed in-between each element. This result is not a valid URL.
This is one of two key reason why this function is implemented, and why it used the path.Join function instead of filepath.Join.
Also, there is an existing
urlJoinwhich is actually joining a URL. And a correspondingurlParse. Those are both working with URLs, the function presented here is not...
urlJoin is a function which takes an input of type map and combines them into a URL based on a number of optional inputs, according to RFC 3986. It looks for the following keys (with example values provided):
scheme: 'http'
host: 'server.com:8080'
path: '/api'
query: 'list=false'
opaque: nil
fragment: 'anchor'
userinfo: 'admin:secret'
The path key supports a single string value. If the path of the url has multiple path elements, such as api,foo, and bar, they must be combined into a single string prior to calling this function. To be valid, they must be combined with the "/" character in-between each element, such as /api/foo/bar.
This is the second of the two key reason why this function is implemented. It provides a first-class way to prepare a path string for use with URLs, including for use with the existing urlJoin function.
I apologize for not explaining these things more clearly in the original description.
|
@solvingj why not just add For your problem, its much neater than dealing with the separators. It seems like it would just solve your problem (sorry I don't exactly know the templating syntax to use here, but you will get the point):
|
This suggested implementation does not result in the desired behavior (it does not pass the included unit tests). To try to help understand why, I have provided some responses to your other comments and questions in this PR, with some key details about how the functions you mentioned work. Your confusion is understandable, as the details of these functions are somewhat esoteric, and cross-platform nuances are often not well-understood. I apologize I did not give more detailed explanations in my original PR description. |
|
Hi @solvingj, Looks like this build is broken. This needs a rebase. |
…constants: - PATH_LIST_SEPARATOR -> os.PathListSeparator - FILE_PATH_SEPARATOR -> os.PathSeparator Add joinEnv string function which is sibling to joinPath: - strings.Join(elem, string(os.PathListSeparator)) Add joinUrl string function which is sibling to joinPath: - path.Join(elem...) Add all to docs closes go-task#2406
- Rebased on latest main branch (origin/main) - Resolved conflicts in compiler.go: merged filepath.ToSlash() changes with new special variables - Fixed type conversion for os.PathListSeparator and os.PathSeparator (rune to string) - Added PATH_LIST_SEPARATOR and FILE_PATH_SEPARATOR to summary filter to prevent them from appearing in task summaries (consistent with other special variables) - All tests passing including linting
|
@andreynering done. All tests pass locally now. |
|
@andreynering any additional blockers? |
andreynering
left a comment
There was a problem hiding this comment.
Thank you for your patience!
joinEnv and joinUrl string functions and 2 new system vars
Add two new system variables which map directly to platform specific go constants:
Add joinEnv string function which is sibling to joinPath:
Add joinUrl string function which is sibling to joinPath:
Add all to docs
closes #2406