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

Bash completion doesn't match non-canonical device names or files #842

Closed
kevinoid opened this issue Aug 14, 2019 · 6 comments · Fixed by #870
Closed

Bash completion doesn't match non-canonical device names or files #842

kevinoid opened this issue Aug 14, 2019 · 6 comments · Fixed by #870

Comments

@kevinoid
Copy link
Contributor

The Bash completion scripts for commands which usually operate on block devices (blkdiscard, *disk, mkfs*) won't complete files other than the names of block devices from lsblk -pnro name. This excludes block devices under /dev/disk/, disk image files, custom device nodes, and probably other things which make the tools frustrating to use in these cases.

Would you be open to accepting a PR which completes these, either by default or as a fallback if compgen -W "$(lsblk -pnro name)" returns no matches?

Thanks for considering,
Kevin

@kerolasa
Copy link
Member

I'm sure everyone agrees improvements are welcome. But why only as fallback? If there useful possibilities just list everything by default.

@kevinoid
Copy link
Contributor Author

For sure. I'd be happy to change compgen -W "$(lsblk -pnro name)" to compgen -f. The only down-sides that I can think of are: 1) <tab> and /<tab> show contents of / instead of jumping straight to block devices, and 2) there will be a lot more output from /dev/<tab> which will show everything instead of just block devices.

I think compgen -f would be preferable, but assumed it would get push back from the author of compgen -W "$(lsblk -pnro name)" and would be harder to justify than fallback. What do you think?

@kerolasa
Copy link
Member

Isn't there no better way to find custom devices paths than compgen -f? If not then scrapping lsblk -pnro name in favour of compgen -f is probably best option.

@kevinoid
Copy link
Contributor Author

Sure, I could come up with a way to limit completions to block devices (either with or without fallback to regular files), but it may be annoying/surprising for users who want to run against a image file (e.g. sfdisk -d driveimage.raw), which might make compgen -f preferable (both for simplicity of implementation and behavior).

@karelzak
Copy link
Collaborator

hmm... add /dev/disk/by-* to the default will generate huge list with many duplicates and aliases. I think this directory should be added to the list only if user explicitly specifies /dev/disk/ on command line. The same with the current directory, for example:

blkdiscard   <tab>                 --> show $(lsblk -pnro name)
blkdiscard  /dev/disk  <tab>       --> show all the funny aliases
blkdiscard  ./   <tab>             --> show all files 

I have no clue how difficult is to implement it, but it would be nice if user can control it by path prefix (/dev/disk, ./ ...) and default should be $(lsblk -pnro name) is nothing specified.

@kevinoid
Copy link
Contributor Author

Thanks @karelzak, sounds good to me. That should be reasonably straightforward to implement. I'll take a stab at it soon.

kevinoid added a commit to kevinoid/util-linux that referenced this issue Sep 19, 2019
Some of the fsck and mkfs commands complete differently than the others,
and differently than the desired behavior.[1]  Standardize on completing
with $(lsblk -pnro name):

* fsck: Don't complete completes on all block devices and device links
  under /dev immediately (which is excessive and prone to search
  problems).
* mkfs, mkfs.bfs: Don't complete "/path/to/file" literally.  I assume
  this was copy/pasted from example code, since it does not appear to be
  a valid argument unless it is a valid path, which is rare.
* fsck.cramfs, mkfs, mkfs.bfs, mkfs.cramfs, mkswap: Don't complete on
  all filenames initially.  The desired behavior is to complete
  filenames only if there are no canonical matches.[1]

Note: A subsequent commit will add the desired fallback behavior.

[1]: util-linux#842 (comment)

Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
kevinoid added a commit to kevinoid/util-linux that referenced this issue Sep 19, 2019
For commands which support operating on files (i.e. disk images), it is
desirable for bash-completion to complete matching file names.  It is
also desirable to complete on block device symlinks (e.g. under
/dev/disk).  To complete common use cases, often on canonical device
names, continue to try completion using canonical device names, then
fall back to matching any file incrementally as Bash does by default.[1]

[1]: util-linux#842 (comment)

Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
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 a pull request may close this issue.

3 participants