jq
jq copied to clipboard
Add functions to work with filesystem paths: `expand_path` , `get_home`, `get_abspath`
This PR brings to the public interface three functions already available in util.h.
get_homeget_abspathexpand_path
Along with the already available env function, they can make jq play more nicely with the filesystem.
Here's what they look like.
I've chosen slightly different names than those in util.h.
Also I'm not entirely sure that expand_path is worth having.
If this looks reasonable, I'll get to add a couple of tests too.
The get_-prefix feel a bit redundant? maybe get_home -> hoemdir, get_abspath -> abspath?
Currently in function names "path" is used to refer to a path expression, could this make things confusing?
@nicowilliams how would this fit naming-wise with the other IO stuff you worked on?
The
get_-prefix feel a bit redundant? maybeget_home->hoemdir,get_abspath->abspath?Currently in function names "path" is used to refer to a path expression, could this make things confusing?
I'm for homedir and absdir
@nicowilliams how would this fit naming-wise with the other IO stuff you worked on?
Also those functions have 0 arguments (just the input), not 1.
The error messages should say /0, not /1.
How about some docs?
Some tests would be nice too
homedir is good, it simply represents what it returns. abspath looks it converts any relative paths to absolute paths regardless of the existence, but current implementation does not seem to work with non-existing path. The command realpath prints the absolute path regardless of existence, so worth discussing which it should behave. expand_path looks an ambiguous term; expanding the home dir of the current user is what it does now (os.path.expanduser in Python), but there are possibility of misinterpreted as expanding wildcards, or expanding embed variables, or something else called shell expansion.
Yeah, you're right @itchyny; from these, only home_dir has clear semantics. The rest can be a bit flaky.
How about ditching the rest, and keep realpath and set behavior similar to https://www.man7.org/linux/man-pages/man3/realpath.3.html ?
I'm thinking this can be archived if system() (#1614) is included. Long term feature request though.
If my PR for the --sandbox flag (#3092) is merged first, then this PR should also be updated so that these new filesystem features are blocked by the --sandbox flag.