1
Fork 0
mirror of https://github.com/RGBCube/nu_scripts synced 2025-08-01 06:37:46 +00:00

Follow Include to extract more SSH hosts (#993)

Sometimes, we have so many SSH servers that divide to extra config files
and include in *~/.ssh/config*

This PR follow the `Include` directive to retrieve more hosts.

Example of such config:

```ssh_config
Include extra/agriconnect
Include "extra/Old servers"
```

This PR also fix an issue that orginal code grabs this:

```ssh_config
Host *
```
This commit is contained in:
Nguyễn Hồng Quân 2024-12-14 19:49:27 +07:00 committed by GitHub
parent cc62dd7f0f
commit 8c21f6e5a6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -34,14 +34,21 @@ def "nu-complete ssh-host" [] {
let files = [
'/etc/ssh/ssh_config',
'~/.ssh/config'
] | filter { |file| $file | path exists }
] | filter {|file| $file | path exists }
$files | each { |file|
let included_files = $files | each {|file|
let folder = $file | path expand | path dirname
let rel_subfiles = $file | open | lines | str trim | where { |s| $s | str starts-with 'Include' } | each { |s| $s | parse --regex '^Include\s+(?<subfile>.+)' | get subfile | str replace -a '"' '' } | flatten
$rel_subfiles | each { |f| $folder | path join $f }
} | flatten | filter { |p| $p | path exists }
[ ...$files, ...$included_files ] | each {|file|
let lines = $file | open | lines | str trim
mut result = []
for $line in $lines {
let data = $line | parse --regex '^Host\s+(?<host>.+)'
let data = $line | parse --regex '^Host\s+(?<host>[-\.\w]+)'
if ($data | is-not-empty) {
$result = ($result | append { 'value': ($data.host | first), 'description': "" })
continue;
@ -54,4 +61,4 @@ def "nu-complete ssh-host" [] {
}
$result
} | flatten
}
}