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

Follow Include to extract more SSH hosts #993

Merged
merged 1 commit into from
Dec 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions custom-completions/ssh/ssh-completions.nu
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -54,4 +61,4 @@ def "nu-complete ssh-host" [] {
}
$result
} | flatten
}
}
Loading