Merge branch 'feature/configFlag'

This commit is contained in:
Guillaume Archambault 2024-08-04 10:31:15 +02:00
commit 0f87aff94d

View File

@ -19,29 +19,40 @@
set -eo pipefail; [[ $TRACE ]] && set -x set -eo pipefail; [[ $TRACE ]] && set -x
readonly VERSION="1.0.0" readonly VERSION="1.0.0"
CONFIG_FILE=${SSHM_CONFIG:-~/.ssh/config}
ssh_manager_version() { ssh_manager_version() {
echo "ssh_manager $VERSION" echo "ssh_manager $VERSION"
} }
ssh_manager_help() { ssh_manager_help() {
echo "Usage: ssh_manager command <command-specific-options>" echo "Usage: ssh_manager command [--config <file>|-c <file>] <command-specific-options>"
echo echo
cat<<EOF | column -c2 -t -s, echo "Commands:"
list, List SSH hosts and prompt for connection cat<<EOF | column -t -s $'\t'
connect <number|name>, Connect to SSH host by number or name list List SSH hosts and prompt for connection
ping <name>, Ping an SSH host to check availability connect <number|name> Connect to SSH host by number or name
view <name>, Check configuration of host ping <name> Ping an SSH host to check availability
delete <name>, Delete an SSH host from the configuration view <name> Check configuration of host
add, Add an SSH host to the configuration delete <name> Delete an SSH host from the configuration
help, Displays help add Add an SSH host to the configuration
version, Displays the current version help Displays help
version Displays the current version
EOF EOF
echo echo
echo "Flags:"
cat<<EOF | column -t -s $'\t'
-c, --config Select a specific ssh config file
EOF
echo
echo "Environment Variables:"
cat<<EOF | column -t -s $'\t'
SSHM_CONFIG Specify the path of an ssh config file"
EOF
} }
ssh_manager_list() { ssh_manager_list() {
local config_file=~/.ssh/config local config_file="$1"
echo -e "\nList of SSH hosts:" echo -e "\nList of SSH hosts:"
grep -E '^Host ' "$config_file" | awk '{print $2}' | grep -v '^#' | sort | nl grep -E '^Host ' "$config_file" | awk '{print $2}' | grep -v '^#' | sort | nl
@ -52,12 +63,12 @@ ssh_manager_list() {
exit 0 exit 0
fi fi
ssh_manager_connect "$host" ssh_manager_connect "$config_file" "$host"
} }
ssh_manager_connect() { ssh_manager_connect() {
local config_file=~/.ssh/config local config_file="$1"
local host="$1" local host="$2"
if [[ -z "$host" ]]; then if [[ -z "$host" ]]; then
echo "Error: please provide a host number or name." 1>&2 echo "Error: please provide a host number or name." 1>&2
exit 1 exit 1
@ -77,8 +88,8 @@ ssh_manager_connect() {
} }
ssh_manager_ping() { ssh_manager_ping() {
local config_file=~/.ssh/config local config_file="$1"
local host="$1" local host="$2"
if [[ -z "$host" ]]; then if [[ -z "$host" ]]; then
echo "Error: please provide a host name." 1>&2 echo "Error: please provide a host name." 1>&2
exit 1 exit 1
@ -99,8 +110,8 @@ ssh_manager_ping() {
} }
ssh_manager_view() { ssh_manager_view() {
local config_file=~/.ssh/config local config_file="$1"
local host="$1" local host="$2"
if [[ -z "$host" ]]; then if [[ -z "$host" ]]; then
echo "Error: please provide a host name." 1>&2 echo "Error: please provide a host name." 1>&2
exit 1 exit 1
@ -118,8 +129,8 @@ ssh_manager_view() {
} }
ssh_manager_delete() { ssh_manager_delete() {
local config_file=~/.ssh/config local config_file="$1"
local host="$1" local host="$2"
if [[ -z "$host" ]]; then if [[ -z "$host" ]]; then
echo "Error: please provide a host name." 1>&2 echo "Error: please provide a host name." 1>&2
exit 1 exit 1
@ -132,8 +143,8 @@ ssh_manager_delete() {
} }
ssh_manager_add() { ssh_manager_add() {
local config_file=~/.ssh/config local config_file="$1"
local host="$1" local host="$2"
local hostname local hostname
local user local user
local port local port
@ -181,8 +192,8 @@ ssh_manager_add() {
} }
ssh_manager_edit() { ssh_manager_edit() {
local config_file=~/.ssh/config local config_file="$1"
local host="$1" local host="$2"
if [[ -z "$host" ]]; then if [[ -z "$host" ]]; then
echo "Error: please provide a host name." 1>&2 echo "Error: please provide a host name." 1>&2
exit 1 exit 1
@ -215,7 +226,7 @@ ssh_manager_edit() {
new_identity_file=${new_identity_file:-${current_identity_file:-~/.ssh/id_rsa}} new_identity_file=${new_identity_file:-${current_identity_file:-~/.ssh/id_rsa}}
# Delete the old configuration # Delete the old configuration
ssh_manager_delete "$host" ssh_manager_delete "$config_file" "$host"
# Add the new configuration # Add the new configuration
{ {
@ -234,9 +245,22 @@ ssh_manager_edit() {
echo "Configuration for host $host updated successfully." echo "Configuration for host $host updated successfully."
} }
ssh_manager_main() { ssh_manager_main() {
local config_file="$CONFIG_FILE"
local command="$1" local command="$1"
shift
while [[ "$#" -gt 0 ]]; do
case "$1" in
--config|-c)
config_file="$2"
shift 2
;;
*)
break
;;
esac
done
if [[ -z $command ]]; then if [[ -z $command ]]; then
ssh_manager_version ssh_manager_version
@ -245,28 +269,27 @@ ssh_manager_main() {
exit 0 exit 0
fi fi
shift 1
case "$command" in case "$command" in
"list") "list")
ssh_manager_list ssh_manager_list "$config_file"
;; ;;
"connect") "connect")
ssh_manager_connect "$@" ssh_manager_connect "$config_file" "$@"
;; ;;
"ping") "ping")
ssh_manager_ping "$@" ssh_manager_ping "$config_file" "$@"
;; ;;
"view") "view")
ssh_manager_view "$@" ssh_manager_view "$config_file" "$@"
;; ;;
"delete") "delete")
ssh_manager_delete "$@" ssh_manager_delete "$config_file" "$@"
;; ;;
"add") "add")
ssh_manager_add "$@" ssh_manager_add "$config_file" "$@"
;; ;;
"edit") "edit")
ssh_manager_edit "$@" ssh_manager_edit "$config_file" "$@"
;; ;;
"version") "version")
ssh_manager_version ssh_manager_version