jq icon indicating copy to clipboard operation
jq copied to clipboard

Add functions to work with filesystem paths: `expand_path` , `get_home`, `get_abspath`

Open Florents-Tselai opened this issue 1 year ago • 10 comments

This PR brings to the public interface three functions already available in util.h.

  • get_home
  • get_abspath
  • expand_path

Along with the already available env function, they can make jq play more nicely with the filesystem.

Here's what they look like.

image

Florents-Tselai avatar Mar 02 '24 23:03 Florents-Tselai

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.

Florents-Tselai avatar Mar 02 '24 23:03 Florents-Tselai

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?

wader avatar Mar 03 '24 08:03 wader

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?

I'm for homedir and absdir

@nicowilliams how would this fit naming-wise with the other IO stuff you worked on?

Florents-Tselai avatar Mar 03 '24 09:03 Florents-Tselai

Also those functions have 0 arguments (just the input), not 1. The error messages should say /0, not /1.

emanuele6 avatar Mar 03 '24 09:03 emanuele6

How about some docs?

nicowilliams avatar Mar 07 '24 19:03 nicowilliams

Some tests would be nice too

wader avatar Mar 07 '24 19:03 wader

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.

itchyny avatar Mar 22 '24 07:03 itchyny

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 ?

Florents-Tselai avatar Mar 24 '24 12:03 Florents-Tselai

I'm thinking this can be archived if system() (#1614) is included. Long term feature request though.

itchyny avatar Mar 24 '24 13:03 itchyny

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.

jemc avatar Apr 12 '24 20:04 jemc