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

feat: add support for ssh completion using files in .ssh/config.d too #529

Conversation

typhoe
Copy link
Contributor

@typhoe typhoe commented Feb 20, 2024

From 7.3p1 and up, there is an Include keyword, which allows you to include additional configuration files.

Include
Include the specified configuration file(s). Multiple pathnames may be specified and each pathname may contain glob(3) wildcards and, for user configurations, shell-like “~” references to user home directories. Files without absolute paths are assumed to be in ~/.ssh if included in a user configuration file or /etc/ssh if included from the system configuration file. Include directive may appear inside a Match or Host block to perform conditional inclusion.
Source: ssh_config(5).

So if you have Include config.d/* in your ~/.ssh/config file, this pull request allows files put in ~/.ssh/config.d/ to be also parsed for the Host keyword to use completion when calling ssh command (in addition to the default ~/.ssh/config)

Why would you want to do that: It's easier to maintain lists of hosts in dedicated files (e.g. separate private-home-lan, company1-project1, company2-project3, etc...)

Copy link
Contributor

@akinomyoga akinomyoga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you extract the filenames from Include in ~/.ssh/config (instead of assuming a specific directory ~/.ssh/config.d)?

@typhoe typhoe changed the title feat: add support for ssh completion using files in .ssh/config.d too [WIP] feat: add support for ssh completion using files in .ssh/config.d too Feb 22, 2024
Copy link
Contributor

@akinomyoga akinomyoga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

You seem to directly source the extracted path "$additional_include_defined_file", but the description you quoted says

Files without absolute paths are assumed to be in ~/.ssh if included in a user configuration file

Could you test whether each additoinal_include_defined_path is a relative path or an absolute path, and prepend "$HOME/.ssh/" if it's a relative path?


edit: Ah, OK. It's WIP. No need to rush, so please take your time.

@typhoe typhoe changed the title [WIP] feat: add support for ssh completion using files in .ssh/config.d too feat: add support for ssh completion using files in .ssh/config.d too Feb 22, 2024
@typhoe
Copy link
Contributor Author

typhoe commented Feb 22, 2024

Sorry, it's my first pull request using github and I didn't realized it would push my commit directly to the pull request, I change the subjet to WIP as soon as I received the github email.
My bad !

I indeed found the 2 issues you immediately saw (missing file for awk cmd and not checking against absolute or relative path), there was also a type Î instead of I for the "Include" regex in the awk cmd and the need for a second 'for' loop to interpret possible globing in the Include option.
The last commit seems to work correctly this time
Tested with various Include:

Include config.d/*
Include file_in_.ssh_dir
Include /absolute_path/test_file

completions/ssh.completion.sh Outdated Show resolved Hide resolved
completions/ssh.completion.sh Outdated Show resolved Hide resolved
completions/ssh.completion.sh Outdated Show resolved Hide resolved
completions/ssh.completion.sh Show resolved Hide resolved
Copy link
Contributor

@akinomyoga akinomyoga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, can you consider using utilities _omb_util_split and _omb_util_glob_expand?

@typhoe typhoe marked this pull request as draft February 22, 2024 15:23
@typhoe
Copy link
Contributor Author

typhoe commented Feb 22, 2024

Also, can you consider using utilities _omb_util_split and _omb_util_glob_expand?

I just looked at these functions... but I4ll be honest and I'm not sure I understood what/how they do what they do... I'll try to test them and see if I manage to understand.
I'll try my best !

@akinomyoga
Copy link
Contributor

Thanks.

I just looked at these functions... but I4ll be honest and I'm not sure I understood what/how they do what they do...

Maybe I need to add some documentation. The word splitting and the pathname expansions by glob patterns can be affected by various shell settings. Those functions perform word splitting and pathname expansions, respectively, in safe ways.

In the present case, you first split the result of awk and, after modifying the relative path, perform the pathname expansion. In this case, we don't want to perform the pathname expansion in the first splitting stage. Also, the pathname expansions can be suppressed by shell settings of set -f or GLOBIGNORE. Other non-trivial shell options can also affect the behavior of the pathname expansions.

@akinomyoga
Copy link
Contributor

I just looked at these functions... but I4ll be honest and I'm not sure I understood what/how they do what they do...

Maybe I need to add some documentation.

b007c73

Also, can you consider using utilities _omb_util_split and _omb_util_glob_expand?

f5d48c3

@akinomyoga
Copy link
Contributor

I'm going to rebase.

@akinomyoga akinomyoga force-pushed the feat-extend-ssh_completion-to-config.d-directory branch from 0056806 to 1ecc6a3 Compare April 28, 2024 05:38
@akinomyoga akinomyoga marked this pull request as ready for review April 28, 2024 05:40
akinomyoga
akinomyoga previously approved these changes Apr 28, 2024
@akinomyoga
Copy link
Contributor

@typhoe I have updated the PR. Could you check it?

@typhoe
Copy link
Contributor Author

typhoe commented Apr 29, 2024

I checked your modifications and there was a typo I think:
In your "relative or absolute path, if relative transforms to absolute" rewrite, you forgot that the var was an array

With the change, the ssh completion sorks as intended.
Thank you for finishing this PR, I'm sorry I didn't had the time to do it myself

@akinomyoga
Copy link
Contributor

Ah, you are right. Thank you.

completions/ssh.completion.sh Outdated Show resolved Hide resolved
completions/ssh.completion.sh Outdated Show resolved Hide resolved
@akinomyoga akinomyoga force-pushed the feat-extend-ssh_completion-to-config.d-directory branch from 09f75cb to 8e38234 Compare May 9, 2024 01:40
@akinomyoga
Copy link
Contributor

rebased.

typhoe and others added 3 commits May 9, 2024 10:47
Co-authored-by: Koichi Murase <myoga.murase@gmail.com>
…actoring and performance improvements

* refactor(completions/ssh): move declaration of local variables
* perf(completions/ssh): include the for-loop inside the if-statement of ~/.ssh/config
* refactor(completions/ssh): use glob matching
* perf(completions/ssh): read config files in a single grep&awk
* perf(completions/ssh): match ^Host by awk to reduce use of grep
* fix(completions/ssh): use "_omb_util_{split,expand_glob}" for safer expansions
* refactor(completions/ssh): rename local variables
* refactor(completions/ssh): perform pathname expansion at once
* fix(completions/ssh): correct to array var in relative or absolute transform loop

Co-authored-by: Stéphane Juventy <sjuventy.ext@orange.com>
Co-authored-by: me <stephane.juventy@gmail.com>
@akinomyoga akinomyoga force-pushed the feat-extend-ssh_completion-to-config.d-directory branch from 8e38234 to c2ca9be Compare May 9, 2024 01:51
@akinomyoga akinomyoga merged commit f882b9a into ohmybash:master May 9, 2024
4 checks passed
@akinomyoga
Copy link
Contributor

Thank you for your contribution! I've merged it.

@typhoe typhoe deleted the feat-extend-ssh_completion-to-config.d-directory branch May 9, 2024 19:18
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 this pull request may close these issues.

2 participants