@@ -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