From 55be991490490e37bb0aced51ac865622ac398d5 Mon Sep 17 00:00:00 2001 From: fosslinux Date: Sun, 20 Jan 2019 16:05:30 +1100 Subject: [PATCH] HUGE refractor --- README.md | 4 ++ debian/changelog | 7 +++ install.sh | 37 +++++++------- openbox_files.sh | 66 ++++++++++++------------- setup.sh | 124 +++++++++++++++++++++++------------------------ setup_gui.sh | 62 ++++++++++++------------ sshfs_gui.sh | 36 +++++++------- 7 files changed, 173 insertions(+), 163 deletions(-) mode change 100644 => 100755 install.sh diff --git a/README.md b/README.md index 27b00fe..d7dc887 100644 --- a/README.md +++ b/README.md @@ -23,3 +23,7 @@ Part of TildeLinux. - Equal signs before all yad options - Move shared functions into separate files - Error handling for file creation + +### Changelog + +version diff --git a/debian/changelog b/debian/changelog index 4c679eb..5d357f1 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,10 @@ +tildemerge (0.2-1) unstable; urgency=medium + + * Code cleanup + * Refractor + + -- ***REMOVED*** Sun, 20 Jan 2019 16:00:56 +1100 + tildemerge (0.1-1) unstable; urgency=medium * Initial release diff --git a/install.sh b/install.sh old mode 100644 new mode 100755 index 88a14dc..a33803c --- a/install.sh +++ b/install.sh @@ -2,30 +2,31 @@ if test -z "$1" then - install_dir="$HOME/.local/bin" + install_dir="$HOME/.local/bin" else - install_dir="$1" + install_dir="$1" fi -install_scripts(){ - for f in *.sh - do - if ! echo "$f" | grep -q "install.sh" - then - cp "$f" "$install_dir" - echo "Copied $f to $install_dir/$f" - chmod +x "$install_dir/$f" - echo "Made $install_dir/$f executable" - echo "" - fi - done +install_scripts() { + for f in *.sh + do + if ! printf "%s" "$f" | grep -q "install.sh" + then + full_path="${install_dir}/${f}" + cp "$f" "$install_dir" + printf "Copied %s to %s.\\n" "$f" "$full_path" + chmod +x "%s" "$full_path" + printf "Made %s executable.\\n" "$full_path" + printf "" + fi + done } if test -f "$install_dir" then - install_scripts + install_scripts else - echo "$install_dir not found, creating..." - mkdir -p "$install_dir" - install_scripts + printf "%s not found, creating...\\n" "$install_dir" + mkdir -p "$install_dir" + install_scripts fi diff --git a/openbox_files.sh b/openbox_files.sh index 90b7e74..a3b9a27 100755 --- a/openbox_files.sh +++ b/openbox_files.sh @@ -1,38 +1,38 @@ #!/bin/sh ssh_config_file="$HOME/.ssh/config" -get_locations(){ - if ! grep -q "SSHFS Mount " "$ssh_config_file" - then - echo "" - echo " " - echo " $HOME/.local/bin/sshfs_gui.sh" - echo " " - echo "" - else - while read -r line - do - location_path="" - if echo "$line" | grep -q "SSHFS Mount " - then - location_path=$(echo "$line" | rev | cut -f 1 -d ' ' | rev) - location_name=$(basename "$location_path") - echo "" - echo " " - echo " thunar $location_path" - echo " " - echo "" - fi - done < "$ssh_config_file" - fi +get_locations() { + if ! grep -q "SSHFS Mount " "$ssh_config_file" + then + printf "\\n" + printf " \\n" + printf " %s/.local/bin/sshfs_gui.sh\\n" "$HOME" + printf " \\n" + printf "\\n" + else + while read -r line + do + location_path="" + if printf "%s" "$line" | grep -q "SSHFS Mount " + then + location_path=$(printf "%s" "$line" | rev | cut -f 1 -d ' ' | rev) + location_name=$(basename "$location_path") + printf "\\n" "$location_name" + printf " \\n" + printf " thunar %s\\n" "$location_path" + printf " \\n" + printf "\\n" + fi + done < "$ssh_config_file" + fi } -echo "" -echo "" -echo " " -echo " thunar" -echo " " -echo "" -echo "" - get_locations -echo "" +printf "\\n" +printf "\\n" +printf " \\n" +printf " thunar\\n" +printf " \\n" +printf "\\n" +printf "\\n" +get_locations +printf "\\n" diff --git a/setup.sh b/setup.sh index 59d9a59..d1db5ee 100755 --- a/setup.sh +++ b/setup.sh @@ -1,6 +1,6 @@ #!/bin/sh -# Copyright (C) 2018 oneseveneight +# Copyright (c) 2018-2019 TildeLinux Maintainers # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -15,16 +15,16 @@ # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -# -# Email: oneseveneight@airmail.cc +# +# Email: tildelinux@tildeverse.org -is_substring(){ +is_substring() { case $2 in - *$1*) - return 0 + *$1*) + return 0 ;; - *) - return 1 + *) + return 1 ;; esac } @@ -33,18 +33,18 @@ configure_directories() { server_name="$1" local_dir="" remote_dir="" - - echo "What would you like to call the local directory? (~/Remote/...)?" + + printf "What would you like to call the local directory? (~/Remote/...)?\\n" read -r answer - local_dir="$HOME/Remote/$answer" + local_dir="${HOME}/Remote/${answer}" mkdir -p "$local_dir" - echo "Created directory $local_dir." - echo "What directory on \"$server_name\" would you like to link it to? (absolute path)" + printf "Created directory %s.\\n" "$local_dir" + printf "What directory on \"%s\" would you like to link it to? (absolute path)\\n" "$server_name" read -r answer remote_dir="$answer" - echo "Linking directory $local_dir to $remote_dir on \"$server_name\"." - sshfs "$server_name:$remote_dir" "$local_dir" - echo "Done, enjoy!" + printf "Linking directory %s to %s on \"%s\".\\n" "$local_dir" "$remote_dir" "$server_name" + sshfs "${server_name}:${remote_dir}" "$local_dir" + printf "Done, enjoy!\\n" } attempt_login() { @@ -52,28 +52,27 @@ attempt_login() { ssh_temp_config="$2" ssh_config="$3" - echo "Attempting to log in with configuration for \"$server_name\"." - if ssh -qF "$ssh_temp_config" "$server_name" exit - then - echo "Login successful! Writing ssh config..." + printf "Attempting to log in with configuration for \"%s\".\\n" "$server_name" + if ssh -qF "$ssh_temp_config" "$server_name" exit ; then + printf "Login successful! Writing ssh config...\\n" cp "$ssh_temp_config" "$ssh_config" - echo "Would you like to configure sshfs for this server? (y/n)" + printf "Would you like to configure sshfs for this server? (y/n)\\n" read -r answer case "$answer" in "y") configure_directories "$server_name" ;; "n") - echo "All right, you're all done!" - echo "Exiting..." + printf "All right, you're all done!\\n" + printf "Exiting...\\n" ;; "*") - echo "\"$answer\" was not recognized as an answer, please enter 'y' or 'n'" + printf "\"%s\" was not recognized as an answer, please enter 'y' or 'n'\\n" "$answer" esac else - echo "Login failed, what would you like to do?" - echo " 1) Try logging in again." - echo " 2) Re-configure my credentials." + printf "Login failed, what would you like to do?\\n" + printf " 1) Try logging in again.\\n" + printf " 2) Re-configure my credentials.\\n" read -r answer case "$answer" in "1") @@ -94,44 +93,43 @@ setup_keys() { ssh_dir="" key_type="" - echo "Configuring \"$server_address\" as \"$server_name\"." - echo "What is your login name for this server?" + printf "Configuring \"%s\" as \"%s\".\\n" "$server_address" "$server_name" + printf "What is your login name for this server?\\n" read -r login - echo "Where is the private key you use for this account (absolute path)" + printf "Where is the private key you use for this account? (absolute path)\\n" read -r key_path - if test -r "$key_path" - then - echo "File found, configuring ssh..." - ssh_dir="$HOME/.ssh" - ssh_host_dir="$HOME/.ssh/$server_name" + if test -r "$key_path" ; then + printf "File found, configuring ssh...\\n" + ssh_dir="${HOME}/.ssh" + ssh_host_dir="${HOME}/.ssh/${server_name}" key_type=$(head -n 1 "$key_path" \ | cut -f 2 -d ' ' \ | tr '[:upper:]' '[:lower:]') test -d "$ssh_dir" || mkdir "$ssh_dir" test -d "$ssh_host_dir" || mkdir "$ssh_host_dir" - echo "Key appears to be of type \"$key_type\"" - echo "Generating public key..." - ssh-keygen -y -f "$key_path" > "$ssh_host_dir/id_$key_type.pub" - echo "Copying private key..." - cp "$key_path" "$ssh_host_dir/id_$key_type" - echo "Saving configuration..." - test -e "$ssh_dir/config" || touch "$ssh_dir/config" + printf "Key appears to be of type \"%s\"." "$key_type\\n" + printf "Generating public key...\\n" + ssh-keygen -y -f "$key_path" > "${ssh_host_dir}/id_${key_type}.pub" + printf "Copying private key...\\n" + cp "$key_path" "${ssh_host_dir}/id_${key_type}" + printf "Saving configuration...\\n" + test -e "${ssh_dir}/config" || touch "${ssh_dir}/config" - cp "$ssh_dir/config" "$ssh_dir/config.tmp" - { - echo "Host $server_name" - echo " HostName $server_address" - echo " port 22" - echo " User $login" - echo " IdentityFile $ssh_host_dir/id_$key_type" - } >> "$ssh_dir/config.tmp" - echo "Right now your configuration looks like this:" - cat "$ssh_dir/config.tmp" + cp "${ssh_dir}/config" "${ssh_dir}/config.tmp" + { + printf "Host %s\\n" "$server_name" + printf " HostName %s\\n" "$server_address" + printf " port 22\\n" + printf " User %s\\n" "$login" + printf " IdentityFile %s/id_%s\\n" "$ssh_host_dir" "$key_type" + } >> "${ssh_dir}/config.tmp" + printf "Right now your configuration looks like this:\\n" + cat "${ssh_dir}/config.tmp" - attempt_login "$server_name" "$ssh_dir/config.tmp" "$ssh_dir/config" + attempt_login "$server_name" "${ssh_dir}/config.tmp" "${ssh_dir}/config" else - echo "$key_path could not be read." - echo "Either it doesn't exist, or you dont have the correct permissions, please try again." + printf "%s could not be read.\\n" "$key_path" + printf "Either it doesn't exist, or you dont have the correct permissions, please try again.\\n" setup_keys "$server_address" "$server_name" fi } @@ -139,14 +137,14 @@ setup_keys() { pick_account() { answers="1 2 3 4" answer="" - echo "What account would you like to configure?" - echo " 1) tilde.town" - echo " 2) tilde.team" - echo " 3) yourtilde.com" - echo " 4) cosmic.voyage" - echo " 5) tilde.institute" + printf "What account would you like to configure?\\n" + printf " 1) tilde.town\\n" + printf " 2) tilde.team\\n" + printf " 3) yourtilde.com\\n" + printf " 4) cosmic.voyage\\n" + printf " 5) tilde.institute\\n" read -r answer - + if is_substring "$answer" "$answers" then case "$answer" in @@ -167,7 +165,7 @@ pick_account() { ;; esac else - echo "\"$answer\" is not a valid option." + printf "\"%s\" is not a valid option.\\n" "$answer" pick_account fi } diff --git a/setup_gui.sh b/setup_gui.sh index feb9eab..7dd88bc 100755 --- a/setup_gui.sh +++ b/setup_gui.sh @@ -12,14 +12,14 @@ ssh_port="22" ssh_dir="$HOME/.ssh" priv_key_path=" " -get_field(){ +get_field() { field_num="$1" info="$2" - echo "$info" | cut -f "$field_num" -d '|' + printf "%s" "$info" | cut -f "$field_num" -d '|' } -error_box(){ +error_box() { error_text="$1" yad \ --posx="$mouse_x" \ @@ -31,7 +31,7 @@ error_box(){ --button="OK":0 } -succ_box(){ +succ_box() { succ_text="$1" yad \ --posx="$mouse_x" \ @@ -43,7 +43,7 @@ succ_box(){ --button="OK":0 } -dec_box(){ +dec_box() { error_text="$1" yad \ --posx="$mouse_x" \ @@ -57,7 +57,7 @@ dec_box(){ return $? } -info_box(){ +info_box() { info_text="$1" yad \ --posx="$mouse_x" \ @@ -96,17 +96,17 @@ get_info() { ssh_port=$(get_field 4 "$info") ssh_dir=$(get_field 5 "$info") priv_key_path=$(get_field 6 "$info") - + if test -z "$login" then error_msg="No login provided!" - error_num=$(( error_num + 1)) + error_num=$((error_num + 1)) fi - if test -d "$(echo "$priv_key_path" | sed 's/\/ *$//')" + if test -d "$(printf "%s" "$priv_key_path" | sed 's/\/ *$//')" then - error_msg="$error_msg\\nNo private key provided!" - error_num=$(( error_num + 1)) + error_msg="${error_msg}\\nNo private key provided!" + error_num=$((error_num + 1)) fi if [ $error_num -gt 0 ] @@ -119,10 +119,10 @@ get_info() { } configure_server() { - ssh_current_config="$ssh_dir/config" - ssh_temp_config="$ssh_dir/config.tmp" - ssh_host_dir="$ssh_dir/$server_address" - key_type=$(head -n 1 "$priv_key_path" \ + ssh_current_config="${ssh_dir}/config" + ssh_temp_config="${ssh_dir}/config.tmp" + ssh_host_dir="${ssh_dir}/${server_address}" + key_type=$(head -n 1 "${priv_key_path}" \ | cut -f 2 -d ' ' \ | tr '[:upper:]' '[:lower:]') @@ -130,9 +130,9 @@ configure_server() { || touch "$ssh_current_config" cp "$ssh_current_config" "$ssh_temp_config" - if grep -qi "Host $server_name" "$ssh_current_config" + if grep -qi "Host ${server_name}" "$ssh_current_config" then - dec_box "Host named $server_name already exists, overwrite?" \ + dec_box "Host named ${server_name} already exists, overwrite?" \ || get_info if ! dec_box "Keep old config in file? (ssh will use the new one)" @@ -141,40 +141,40 @@ configure_server() { in_old_host_block=0 while read -r line do - if echo "$line" | grep -q "Host $server_name" + if printf "%s" "$line" | grep -q "Host ${server_name}" then in_old_host_block=0 - elif echo "$line" | grep -q "Host " + elif printf "%s" "$line" | grep -q "Host " then in_old_host_block=1 fi - + [ $in_old_host_block -eq 1 ] \ - && echo "$line" + && printf "%s" "$line" done < "$ssh_current_config" > "$ssh_temp_config" fi fi mkdir -p "$ssh_host_dir" - cp "$priv_key_path" "$ssh_host_dir/id_$key_type" + cp "$priv_key_path" "${ssh_host_dir}/id_${key_type}" - { - echo "Host $server_name" - echo " HostName $server_address" - echo " Port $ssh_port" - echo " User $login" - echo " IdentityFile $ssh_host_dir/id_$key_type" + { + printf "Host %s\\n" "$server_name" + printf " HostName %s\\n" "$server_address" + printf " Port %s\\n" "$ssh_port" + printf " User %s\\n" "$login" + printf " IdentityFile %s/id_%s\\n" "$ssh_host_dir" "$key_type" } >> "$ssh_temp_config" info_box "Attempting to log in with the provided credentials..." & info_pid=$(( $! + 2 )) # yikes, is this safe? - echo "$ssh_temp_config" - echo "$server_address" + printf "%s\\n" "$ssh_temp_config" + printf "%s\\n" "$server_address" if ssh -qF "$ssh_temp_config" "$server_name" exit then kill $info_pid - succ_box "Login success!\\nAccount for $server_address created!" + succ_box "Login success!\\nAccount for ${server_address} created!" cp "$ssh_temp_config" "$ssh_current_config" rm "$ssh_temp_config" exit # Without this the "Host already exists" box appears, investigate. diff --git a/sshfs_gui.sh b/sshfs_gui.sh index bff8fcc..ba23271 100755 --- a/sshfs_gui.sh +++ b/sshfs_gui.sh @@ -11,17 +11,17 @@ server_name=$(grep -E "[H,h]ost " "$ssh_config_file" \ | cut -f 2 -d ' ' \ | tr '\n' '!' \ | sed 's/!$//') # This only works in posix sh -sshfs_local_path="$HOME/Remote/myfolder" +sshfs_local_path="${HOME}/Remote/myfolder" sshfs_remote_path="$HOME" -get_field(){ +get_field() { field_num="$1" info="$2" - echo "$info" | cut -f "$field_num" -d '|' + printf "%s" "$info" | cut -f "$field_num" -d '|' } -error_box(){ +error_box() { error_text="$1" yad \ --posx="$mouse_x" \ @@ -33,7 +33,7 @@ error_box(){ --button="OK":0 } -succ_box(){ +succ_box() { succ_text="$1" yad \ --posx="$mouse_x" \ @@ -45,7 +45,7 @@ succ_box(){ --button="OK":0 } -dec_box(){ +dec_box() { error_text="$1" yad \ --posx="$mouse_x" \ @@ -59,7 +59,7 @@ dec_box(){ return $? } -info_box(){ +info_box() { info_text="$1" yad \ --posx="$mouse_x" \ @@ -91,14 +91,14 @@ get_info() { --button="Continue":0 \ "$ssh_config_file" "$server_name" "$sshfs_local_path" "$sshfs_remote_path") info_return="$?" - if [ $info_return -eq 1 ] + if [ $info_return -eq 1 ] then exit elif [ $info_return -eq 2 ] then if ! "$setup_script_path" then - error_box "$setup_script_path did not finish successfully" + error_box "${setup_script_path} did not finish successfully!" get_info fi fi @@ -110,13 +110,13 @@ get_info() { if test -z "$sshfs_local_path" then error_msg="No local path provided!" - error_num=$(( error_num + 1)) + error_num=$((error_num + 1)) fi if test -z "$sshfs_remote_path" then - error_msg="$error_msg\\nNo remote path provided" - error_num=$(( error_num + 1)) + error_msg="${error_msg}\\nNo remote path provided!" + error_num=$((error_num + 1)) fi if [ $error_num -gt 0 ] @@ -135,27 +135,27 @@ configure_mount() { test -e "$sshfs_local_path" \ || mkdir -p "$sshfs_local_path" - if ! ssh "$server_name" "test -d $sshfs_remote_path" + if ! ssh "$server_name" "test -d \${sshfs_remote_path}" then if dec_box "Directory $sshfs_remote_path does not exist on remote host, create?" then - if ! error_msg=$(ssh "$server_name" "mkdir $sshfs_remote_path" 2>&1) + if ! error_msg=$(ssh "$server_name" "mkdir \${sshfs_remote_path}" 2>&1) then - error_box "Failed to create remote directory $error_msg" + error_box "Failed to create remote directory. ${error_msg}" fi fi fi if error_msg=$(sshfs \ - "$server_name:$sshfs_remote_path" \ + "${server_name}:${sshfs_remote_path}" \ "$sshfs_local_path" \ -oauto_cache,reconnect,Compression=no 2>&1) then kill $info_pid - succ_box "Successfuly linked $sshfs_local_path to $sshfs_remote_path on $server_name" + succ_box "Successfuly linked ${sshfs_local_path} to ${sshfs_remote_path} on ${server_name}" else kill $info_pid - error_box "Failed to link $sshfs_local_path to $sshfs_remote_path on $server_name\\n$error_msg" + error_box "Failed to link ${sshfs_local_path} to ${sshfs_remote_path} on ${server_name}\\n${error_msg}" fi }