Skip to content

Commit

Permalink
Refactor: Organize Bash scripts and introduce centralized configuration
Browse files Browse the repository at this point in the history
- Moved existing Bash scripts into designated directories for better organization.
- Added config.sh to define file paths centrally.
- Created load.sh to source all dependencies and script files from the newly defined paths.
- Implemented driver.sh to handle user interactions and function invocations.
- Ensured load.sh is executed within driver.sh to initialize all configurations and dependencies.
  • Loading branch information
hiroTochigi committed Apr 27, 2024
1 parent 0d63785 commit 4b13aaf
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 37 deletions.
9 changes: 2 additions & 7 deletions callUpdate.sh → src/utils/callUpdate.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
#!/bin/bash

source update.sh
source retrieve.sh
source array.sh
source replace.sh

host="myserver"


Expand Down Expand Up @@ -53,9 +48,9 @@ function update(){
TCPKeepAlive=$newValue
;;
"RemoteForward")
currentValue=$newValue # Assign newValue to currentValue
currentValue=$newValue
if [[ -n "$4" ]]; then
newValue=$4 # Update newValue if a fourth argument is provided
newValue=$4
fi
allArgument=$(extractBlocks "$RemoteForward")
RemoteForward=$(replaceFirstOccurrence "$allArgument" "$currentValue" "$newValue")
Expand Down
22 changes: 2 additions & 20 deletions create.sh → src/utils/create.sh
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
dummy=config

#create_ssh_config() {
# HOST_NAME="$1"
# HOST_ENTRY=$(cat <<EOF
#
#Host $HOST_NAME
# HostName $2
# User $3
# Port $4
# IdentityFile $5
# RemoteForward $6 localhost:$7
# ServerAliveInterval 30
# ServerAliveCountMax 3
# ExitOnForwardFailure yes
# TCPKeepAlive yes
#EOF
#)

create_ssh_config() {
HOST_NAME="$1"
Expand All @@ -34,10 +16,10 @@ Host $HOST_NAME
EOF
)

if grep -q "^Host $HOST_NAME$" $dummy; then
if grep -q "^Host $HOST_NAME$" $CONFIG; then
echo "Configuration for $HOST_NAME already exists."
else
echo "$HOST_ENTRY" >> $dummy
echo "$HOST_ENTRY" >> $CONFIG
echo "Configuration for $HOST_NAME created."
fi
}
Expand Down
3 changes: 1 addition & 2 deletions delete.sh → src/utils/delete.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

dummy=config

delete_ssh_config() {
HOST_NAME="$1"
Expand All @@ -12,7 +11,7 @@ delete_ssh_config() {
$1 == "Host" && $2 == host {skip = 1}
$1 == "Host" && $2 != host && skip {skip = 0; print ""}
!skip {print}
' $dummy > "$TEMP_FILE" && mv "$TEMP_FILE" $dummy
' $CONFIG > "$TEMP_FILE" && mv "$TEMP_FILE" $CONFIG

echo "Configuration for $HOST_NAME deleted."
}
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions src/utils/dependencies/config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#CONFIG=~/.ssh/config
CONFIG=~/Projects/luftballon/config # Test
3 changes: 1 addition & 2 deletions read.sh → src/utils/dependencies/read.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
get_ssh_config_values() {
HOST_NAME=$1
CONFIG_FILE=config
FOUND_HOST=0

while IFS= read -r line; do
Expand All @@ -12,7 +11,7 @@ get_ssh_config_values() {
elif [[ $FOUND_HOST -eq 1 ]]; then
echo "$line"
fi
done < "$CONFIG_FILE"
done < "$CONFIG"

if [ $FOUND_HOST -eq 0 ]; then
echo "No configuration found for $HOST_NAME."
Expand Down
File renamed without changes.
1 change: 0 additions & 1 deletion retrieve.sh → src/utils/dependencies/retrieve.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
source read.sh
extractSshConfigToVariables() {
# Capture the output of get_ssh_config_values
CONFIG_OUTPUT=$(get_ssh_config_values "$1")
Expand Down
39 changes: 39 additions & 0 deletions src/utils/driver.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
source load.sh

function sshConfigManagerUsage() {
echo "Usage: driver <command> [options]"
echo ""
echo "Commands:"
echo " create <name> <host> <user> <port> Create a new SSH configuration"
echo " update <name> <host> <user> <port> Update an existing SSH configuration"
echo " delete <name> Delete an SSH configuration"
echo ""
echo "Options:"
echo " -h, --help Show help information"
}

function driver() {

# Check if at least one argument is provided
if [ $# -eq 0 ]; then
sshConfigManagerUsage
fi

# Execute the appropriate command
case "$1" in
create)
create "${@:2}"
;;
update)
callUpdate "${@:2}"
;;
delete)
delete "${@:2}"
;;
*)
echo "Error: Invalid command."
sshConfigManagerUsage
;;
esac

}
11 changes: 11 additions & 0 deletions src/utils/load.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
manageConfigPath=$(pwd)
source $manageConfigPath/dependencies/config.sh
source $manageConfigPath/dependencies/array.sh
source $manageConfigPath/dependencies/read.sh
source $manageConfigPath/dependencies/replace.sh
source $manageConfigPath/dependencies/retrieve.sh

source $manageConfigPath/create.sh
source $manageConfigPath/update.sh
source $manageConfigPath/delete.sh
source $manageConfigPath/callUpdate.sh
6 changes: 1 addition & 5 deletions update.sh → src/utils/update.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
source create.sh
dummy=config
update_ssh_config() {
HOST_NAME="$1"
TEMP_FILE=$(mktemp)
Expand All @@ -11,10 +9,8 @@ update_ssh_config() {
$1 == "Host" && $2 == host {skip = 1}
$1 == "Host" && $2 != host && skip {skip = 0; print ""}
!skip {print}
' $dummy > "$TEMP_FILE" && mv "$TEMP_FILE" $dummy
' $CONFIG > "$TEMP_FILE" && mv "$TEMP_FILE" $CONFIG

create_ssh_config "$@"
echo "Configuration for $HOST_NAME updated."
}

#update_ssh_config "remoteserver" "newremoteserver.com" "newremoteuser" "newremoteport" "/new/path/to/remote/server/private/key" "2222" "88"

0 comments on commit 4b13aaf

Please sign in to comment.