Skip to content

Commit b6d214d

Browse files
committed
ssh-helper: add list, get, hosts actions
for the upcoming `config-sync` command
1 parent dc34c38 commit b6d214d

2 files changed

Lines changed: 88 additions & 14 deletions

File tree

commands/gpg-helper

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ function gpg_helper() (
164164
# sub => 'public subkey'
165165

166166
# helpers
167-
function get_mode {
167+
function __get_mode {
168168
local mode="${1-}"
169169
choose \
170170
--question='What type of GPG key do you wish to fetch?' \
@@ -231,7 +231,7 @@ function gpg_helper() (
231231
# list [<mode>]
232232
function act_list {
233233
local mode="${1-}"
234-
mode="$(get_mode "$mode")"
234+
mode="$(__get_mode "$mode")"
235235
if [[ $mode == 'private' ]]; then
236236
gpg --keyid-format LONG -K
237237
else
@@ -242,7 +242,7 @@ function gpg_helper() (
242242
# get [<mode>] [<key>]
243243
function act_get {
244244
local mode="${1-}" key="${2-}" flag type
245-
mode="$(get_mode "$mode")"
245+
mode="$(__get_mode "$mode")"
246246
if [[ $mode == 'private' ]]; then
247247
flag="K"
248248
type='sec'
@@ -287,7 +287,7 @@ function gpg_helper() (
287287
# export [<mode>] [<key>]
288288
function act_export {
289289
local mode="${1-}" key="${2-}"
290-
mode="$(get_mode "$mode")"
290+
mode="$(__get_mode "$mode")"
291291
key="$(act_get "$mode" "$key")"
292292
if [[ $mode == 'private' ]]; then
293293
gpg --armor --export-options backup --export-secret-keys "$key"
@@ -299,7 +299,7 @@ function gpg_helper() (
299299
# delete [<mode>] [<key>]
300300
function act_delete {
301301
local mode="${1-}" key="${2-}"
302-
mode="$(get_mode "$mode")"
302+
mode="$(__get_mode "$mode")"
303303
key="$(act_get "$mode" "$key")"
304304
if confirm --bool --ppid=$$ -- "Confirm you wish to delete [$mode] key [$key]?"; then
305305
if [[ $mode == 'private' ]]; then

commands/ssh-helper

Lines changed: 83 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ function ssh_helper() (
3636
& add
3737
Make the SSH agent aware of all your SSH keys.
3838
39+
& list [<public|private>]
40+
List the SSH keys that you have inside \`\$HOME/.ssh\`.
41+
42+
& get [<public|private>]
43+
Select a single SSH key that you have inside \`\$HOME/.ssh\`.
44+
45+
& hosts
46+
List all hosts from \`\$HOME/.ssh/config\`. Returns [96] if no hosts were found.
47+
3948
& permissions
4049
Correct the permissions for the SSH files.
4150
@@ -49,6 +58,19 @@ function ssh_helper() (
4958
return 22 # EINVAL 22 Invalid argument
5059
}
5160

61+
# valid actions
62+
local actions=(
63+
'setup'
64+
'new'
65+
'add'
66+
'list'
67+
'get'
68+
'hosts'
69+
'permissions'
70+
'test'
71+
'connect'
72+
'export'
73+
)
5274
# process
5375
local item action='' option_args=() option_reconfigure='' option_fallback='' option_deps=''
5476
while [[ $# -ne 0 ]]; do
@@ -81,6 +103,7 @@ function ssh_helper() (
81103
'--no-slim'* | '--slim'*) __flag --source={item} --target={option_reconfigure} --target={option_fallback} --target={option_deps} --affirmative --coerce ;;
82104
# </shared options between many setup commands>
83105
'--'*) help 'An unrecognised flag was provided: ' --variable-value={item} ;;
106+
# all other things are assumed to be actions, which are validated later
84107
*)
85108
action="$item"
86109
option_args+=("$@")
@@ -101,15 +124,6 @@ function ssh_helper() (
101124
)
102125

103126
# ensure valid action
104-
local actions=(
105-
'setup'
106-
'new'
107-
'add'
108-
'permissions'
109-
'test'
110-
'connect'
111-
'export'
112-
)
113127
action="$(
114128
choose --required \
115129
--question='Which action to perform?' \
@@ -429,6 +443,62 @@ function ssh_helper() (
429443
fi
430444
}
431445

446+
function __get_mode {
447+
local mode="${1-}"
448+
if [[ $mode =~ ^($|public|private)$ ]]; then
449+
# empty means both public and private
450+
return 0
451+
fi
452+
# the choose is for filtering
453+
choose --question='What type of SSH key do you wish to fetch?' --skip-default --default="$mode" -- 'public' 'private'
454+
}
455+
456+
function __get_keys {
457+
local mode="$1" public_key keys=()
458+
if [[ $mode == 'public' ]]; then
459+
for public_key in "$HOME/.ssh/"*.pub; do
460+
keys+=("$public_key")
461+
done
462+
else
463+
local private_key
464+
if [[ $mode == 'private' ]]; then
465+
for public_key in "$HOME/.ssh/"*.pub; do
466+
private_key="${public_key%.pub}"
467+
if [[ -f $private_key ]]; then
468+
keys+=("$private_key")
469+
fi
470+
done
471+
else
472+
for public_key in "$HOME/.ssh/"*.pub; do
473+
keys+=("$public_key")
474+
private_key="${public_key%.pub}"
475+
if [[ -f $private_key ]]; then
476+
keys+=("$private_key")
477+
fi
478+
done
479+
fi
480+
fi
481+
482+
if [[ ${#keys[@]} -eq 0 ]]; then
483+
return 96 # ENODATA 96 No message available on STREAM
484+
fi
485+
__print_lines "${keys[@]}"
486+
}
487+
488+
function ssh_list {
489+
local mode="${1-}"
490+
mode="$(__get_mode "$mode")" || return $?
491+
__get_keys "$mode" || return $?
492+
}
493+
494+
function ssh_get {
495+
local mode="${1-}" key="${2-}" keys=()
496+
mode="$(__get_mode "$mode")" || return $?
497+
__split --target={keys} --no-zero-length --invoke -- \
498+
__get_keys "$mode" || return $?
499+
choose --question='Which SSH key to get?' --skip-default --default="$key" -- "${keys[@]}"
500+
}
501+
432502
function ssh_permissions {
433503
# create .ssh config file if necessary
434504
__mkdirp "$HOME/.ssh"
@@ -505,6 +575,10 @@ function ssh_helper() (
505575
fi
506576
}
507577

578+
function ssh_hosts {
579+
echo-regexp -gon 'Host (.+)' '$1' <~/.ssh/config | echo-regexp -g ' ' $'\n' | grep --invert-match --fixed-strings --regexp='*' | echo-or-fail --stdin
580+
}
581+
508582
# =====================================
509583
# Act
510584

0 commit comments

Comments
 (0)