From 8424b5d7b51d6f5499257f5f10cd9ab2e487dbe2 Mon Sep 17 00:00:00 2001 From: Guillaume Archambault Date: Fri, 9 Aug 2024 11:50:39 +0200 Subject: [PATCH] Refactor: Replace --config/-c flag with context management system --- sshm.bash | 155 +++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 125 insertions(+), 30 deletions(-) diff --git a/sshm.bash b/sshm.bash index 705bd0c..5b8424c 100755 --- a/sshm.bash +++ b/sshm.bash @@ -9,24 +9,34 @@ # # http://www.apache.org/licenses/LICENSE-2.0 # -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# Unless required by applicable law ou agreed to in writing, software +# distributed under the License est distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OU CONDITIONS OF ANY KIND, either express ou implied. # See the License for the specific language governing permissions and # limitations under the License. ############################################################################### set -eo pipefail; [[ $TRACE ]] && set -x -readonly VERSION="1.0.0" -CONFIG_FILE=${SSHM_CONFIG:-~/.ssh/config} +readonly VERSION="2.0.0" +readonly CONFIG_DIR="${HOME}/.config/sshm" +readonly DEFAULT_CONFIG="${HOME}/.ssh/config" +readonly CURRENT_CONTEXT_FILE="${CONFIG_DIR}/.current_context" + +mkdir -p "$CONFIG_DIR" + +if [[ -f "$CURRENT_CONTEXT_FILE" ]]; then + CONFIG_FILE=$(cat "$CURRENT_CONTEXT_FILE") +else + CONFIG_FILE="$DEFAULT_CONFIG" +fi ssh_manager_version() { echo "ssh_manager $VERSION" } ssh_manager_help() { - echo "Usage: ssh_manager command [--config |-c ] " + echo "Usage: ssh_manager command " echo echo "Commands:" cat< Check configuration of host delete Delete an SSH host from the configuration add Add an SSH host to the configuration + context list List available contexts + context use Use a specific context + context create Create a new context + context delete Delete an existing context help Displays help version Displays the current version -EOF - echo - echo "Flags:" - cat<&2 @@ -245,23 +249,91 @@ ssh_manager_edit() { echo "Configuration for host $host updated successfully." } +context_list() { + local current_context + current_context=$(cat "$CURRENT_CONTEXT_FILE" 2>/dev/null || echo "$DEFAULT_CONFIG") + + echo "Available contexts:" + if [[ "$current_context" == "$DEFAULT_CONFIG" ]]; then + echo "* default" + else + echo " default" + fi + + for context in "$CONFIG_DIR"/*; do + if [[ -f "$context" ]]; then # Vérifie que c'est bien un fichier existant + local context_name + context_name=$(basename "$context") + if [[ "$CONFIG_DIR/$context_name" == "$current_context" ]]; then + echo "* $context_name" + else + echo " $context_name" + fi + fi + done +} + +context_use() { + local context="$1" + if [[ -z "$context" ]]; then + echo "Error: please provide a context name." 1>&2 + exit 1 + fi + + if [[ "$context" == "default" ]]; then + echo "$DEFAULT_CONFIG" > "$CURRENT_CONTEXT_FILE" + echo "Switched to default context." + elif [[ ! -f "$CONFIG_DIR/$context" ]]; then + echo "Error: context '$context' does not exist." 1>&2 + exit 1 + else + echo "$CONFIG_DIR/$context" > "$CURRENT_CONTEXT_FILE" + echo "Switched to context '$context'." + fi +} + +context_create() { + local context="$1" + if [[ -z "$context" ]]; then + echo "Error: please provide a context name." 1>&2 + exit 1 + fi + + if [[ -f "$CONFIG_DIR/$context" ]]; then + echo "Error: context '$context' already exists." 1>&2 + exit 1 + fi + + touch "$CONFIG_DIR/$context" + echo "Context '$context' created." +} + +context_delete() { + local context="$1" + if [[ -z "$context" ]]; then + echo "Error: please provide a context name." 1>&2 + exit 1 + fi + + if [[ ! -f "$CONFIG_DIR/$context" ]]; then + echo "Error: context '$context' does not exist." 1>&2 + exit 1 + fi + + rm -f "$CONFIG_DIR/$context" + echo "Context '$context' deleted." + + if [[ "$(cat "$CURRENT_CONTEXT_FILE")" == "$CONFIG_DIR/$context" ]]; then + echo "$DEFAULT_CONFIG" > "$CURRENT_CONTEXT_FILE" + echo "Switched to default context." + fi +} + ssh_manager_main() { local config_file="$CONFIG_FILE" 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 ssh_manager_version echo @@ -291,6 +363,29 @@ ssh_manager_main() { "edit") ssh_manager_edit "$config_file" "$@" ;; + "context") + local subcommand="$1" + shift + case "$subcommand" in + "list") + context_list "$@" + ;; + "use") + context_use "$@" + ;; + "create") + context_create "$@" + ;; + "delete") + context_delete "$@" + ;; + *) + echo "Error: invalid context subcommand." 1>&2 + ssh_manager_help + exit 3 + ;; + esac + ;; "version") ssh_manager_version ;;