HUGE refractor

This commit is contained in:
fosslinux 2019-01-20 16:05:30 +11:00 committed by Samuel Tyler
parent dae9fcb783
commit 55be991490
7 changed files with 173 additions and 163 deletions

View File

@ -23,3 +23,7 @@ Part of TildeLinux.
- Equal signs before all yad options - Equal signs before all yad options
- Move shared functions into separate files - Move shared functions into separate files
- Error handling for file creation - Error handling for file creation
### Changelog
version

7
debian/changelog vendored
View File

@ -1,3 +1,10 @@
tildemerge (0.2-1) unstable; urgency=medium
* Code cleanup
* Refractor
-- ***REMOVED*** <fosslinux@tilde.team> Sun, 20 Jan 2019 16:00:56 +1100
tildemerge (0.1-1) unstable; urgency=medium tildemerge (0.1-1) unstable; urgency=medium
* Initial release * Initial release

37
install.sh Normal file → Executable file
View File

@ -2,30 +2,31 @@
if test -z "$1" if test -z "$1"
then then
install_dir="$HOME/.local/bin" install_dir="$HOME/.local/bin"
else else
install_dir="$1" install_dir="$1"
fi fi
install_scripts(){ install_scripts() {
for f in *.sh for f in *.sh
do do
if ! echo "$f" | grep -q "install.sh" if ! printf "%s" "$f" | grep -q "install.sh"
then then
cp "$f" "$install_dir" full_path="${install_dir}/${f}"
echo "Copied $f to $install_dir/$f" cp "$f" "$install_dir"
chmod +x "$install_dir/$f" printf "Copied %s to %s.\\n" "$f" "$full_path"
echo "Made $install_dir/$f executable" chmod +x "%s" "$full_path"
echo "" printf "Made %s executable.\\n" "$full_path"
fi printf ""
done fi
done
} }
if test -f "$install_dir" if test -f "$install_dir"
then then
install_scripts install_scripts
else else
echo "$install_dir not found, creating..." printf "%s not found, creating...\\n" "$install_dir"
mkdir -p "$install_dir" mkdir -p "$install_dir"
install_scripts install_scripts
fi fi

View File

@ -1,38 +1,38 @@
#!/bin/sh #!/bin/sh
ssh_config_file="$HOME/.ssh/config" ssh_config_file="$HOME/.ssh/config"
get_locations(){ get_locations() {
if ! grep -q "SSHFS Mount " "$ssh_config_file" if ! grep -q "SSHFS Mount " "$ssh_config_file"
then then
echo "<item label=\"Configure Locations...\">" printf "<item label=\"Configure Locations...\">\\n"
echo " <action name=\"Execute\">" printf " <action name=\"Execute\">\\n"
echo " <command>$HOME/.local/bin/sshfs_gui.sh</command>" printf " <command>%s/.local/bin/sshfs_gui.sh</command>\\n" "$HOME"
echo " </action>" printf " </action>\\n"
echo "</item>" printf "</item>\\n"
else else
while read -r line while read -r line
do do
location_path="" location_path=""
if echo "$line" | grep -q "SSHFS Mount " if printf "%s" "$line" | grep -q "SSHFS Mount "
then then
location_path=$(echo "$line" | rev | cut -f 1 -d ' ' | rev) location_path=$(printf "%s" "$line" | rev | cut -f 1 -d ' ' | rev)
location_name=$(basename "$location_path") location_name=$(basename "$location_path")
echo "<item label=\"$location_name\">" printf "<item label=\"%s\">\\n" "$location_name"
echo " <action name=\"Execute\">" printf " <action name=\"Execute\">\\n"
echo " <command>thunar $location_path</command>" printf " <command>thunar %s</command>\\n" "$location_path"
echo " </action>" printf " </action>\\n"
echo "</item>" printf "</item>\\n"
fi fi
done < "$ssh_config_file" done < "$ssh_config_file"
fi fi
} }
echo "<openbox_pipe_menu>" printf "<openbox_pipe_menu>\\n"
echo "<item label=\"File Manager\">" printf "<item label=\"File Manager\">\\n"
echo " <action name=\"Execute\">" printf " <action name=\"Execute\">\\n"
echo " <command>thunar</command>" printf " <command>thunar</command>\\n"
echo " </action>" printf " </action>\\n"
echo "</item>" printf "</item>\\n"
echo "<separator label=\"Locations\" />" printf "<separator label=\"Locations\" />\\n"
get_locations get_locations
echo "</openbox_pipe_menu>" printf "</openbox_pipe_menu>\\n"

124
setup.sh
View File

@ -1,6 +1,6 @@
#!/bin/sh #!/bin/sh
# Copyright (C) 2018 oneseveneight # Copyright (c) 2018-2019 TildeLinux Maintainers
# #
# This program is free software; you can redistribute it and/or modify # 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 # 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 # 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., # with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # 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 case $2 in
*$1*) *$1*)
return 0 return 0
;; ;;
*) *)
return 1 return 1
;; ;;
esac esac
} }
@ -33,18 +33,18 @@ configure_directories() {
server_name="$1" server_name="$1"
local_dir="" local_dir=""
remote_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 read -r answer
local_dir="$HOME/Remote/$answer" local_dir="${HOME}/Remote/${answer}"
mkdir -p "$local_dir" mkdir -p "$local_dir"
echo "Created directory $local_dir." printf "Created directory %s.\\n" "$local_dir"
echo "What directory on \"$server_name\" would you like to link it to? (absolute path)" printf "What directory on \"%s\" would you like to link it to? (absolute path)\\n" "$server_name"
read -r answer read -r answer
remote_dir="$answer" remote_dir="$answer"
echo "Linking directory $local_dir to $remote_dir on \"$server_name\"." printf "Linking directory %s to %s on \"%s\".\\n" "$local_dir" "$remote_dir" "$server_name"
sshfs "$server_name:$remote_dir" "$local_dir" sshfs "${server_name}:${remote_dir}" "$local_dir"
echo "Done, enjoy!" printf "Done, enjoy!\\n"
} }
attempt_login() { attempt_login() {
@ -52,28 +52,27 @@ attempt_login() {
ssh_temp_config="$2" ssh_temp_config="$2"
ssh_config="$3" ssh_config="$3"
echo "Attempting to log in with configuration for \"$server_name\"." printf "Attempting to log in with configuration for \"%s\".\\n" "$server_name"
if ssh -qF "$ssh_temp_config" "$server_name" exit if ssh -qF "$ssh_temp_config" "$server_name" exit ; then
then printf "Login successful! Writing ssh config...\\n"
echo "Login successful! Writing ssh config..."
cp "$ssh_temp_config" "$ssh_config" 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 read -r answer
case "$answer" in case "$answer" in
"y") "y")
configure_directories "$server_name" configure_directories "$server_name"
;; ;;
"n") "n")
echo "All right, you're all done!" printf "All right, you're all done!\\n"
echo "Exiting..." 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 esac
else else
echo "Login failed, what would you like to do?" printf "Login failed, what would you like to do?\\n"
echo " 1) Try logging in again." printf " 1) Try logging in again.\\n"
echo " 2) Re-configure my credentials." printf " 2) Re-configure my credentials.\\n"
read -r answer read -r answer
case "$answer" in case "$answer" in
"1") "1")
@ -94,44 +93,43 @@ setup_keys() {
ssh_dir="" ssh_dir=""
key_type="" key_type=""
echo "Configuring \"$server_address\" as \"$server_name\"." printf "Configuring \"%s\" as \"%s\".\\n" "$server_address" "$server_name"
echo "What is your login name for this server?" printf "What is your login name for this server?\\n"
read -r login 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 read -r key_path
if test -r "$key_path" if test -r "$key_path" ; then
then printf "File found, configuring ssh...\\n"
echo "File found, configuring ssh..." ssh_dir="${HOME}/.ssh"
ssh_dir="$HOME/.ssh" ssh_host_dir="${HOME}/.ssh/${server_name}"
ssh_host_dir="$HOME/.ssh/$server_name"
key_type=$(head -n 1 "$key_path" \ key_type=$(head -n 1 "$key_path" \
| cut -f 2 -d ' ' \ | cut -f 2 -d ' ' \
| tr '[:upper:]' '[:lower:]') | tr '[:upper:]' '[:lower:]')
test -d "$ssh_dir" || mkdir "$ssh_dir" test -d "$ssh_dir" || mkdir "$ssh_dir"
test -d "$ssh_host_dir" || mkdir "$ssh_host_dir" test -d "$ssh_host_dir" || mkdir "$ssh_host_dir"
echo "Key appears to be of type \"$key_type\"" printf "Key appears to be of type \"%s\"." "$key_type\\n"
echo "Generating public key..." printf "Generating public key...\\n"
ssh-keygen -y -f "$key_path" > "$ssh_host_dir/id_$key_type.pub" ssh-keygen -y -f "$key_path" > "${ssh_host_dir}/id_${key_type}.pub"
echo "Copying private key..." printf "Copying private key...\\n"
cp "$key_path" "$ssh_host_dir/id_$key_type" cp "$key_path" "${ssh_host_dir}/id_${key_type}"
echo "Saving configuration..." printf "Saving configuration...\\n"
test -e "$ssh_dir/config" || touch "$ssh_dir/config" test -e "${ssh_dir}/config" || touch "${ssh_dir}/config"
cp "$ssh_dir/config" "$ssh_dir/config.tmp" cp "${ssh_dir}/config" "${ssh_dir}/config.tmp"
{ {
echo "Host $server_name" printf "Host %s\\n" "$server_name"
echo " HostName $server_address" printf " HostName %s\\n" "$server_address"
echo " port 22" printf " port 22\\n"
echo " User $login" printf " User %s\\n" "$login"
echo " IdentityFile $ssh_host_dir/id_$key_type" printf " IdentityFile %s/id_%s\\n" "$ssh_host_dir" "$key_type"
} >> "$ssh_dir/config.tmp" } >> "${ssh_dir}/config.tmp"
echo "Right now your configuration looks like this:" printf "Right now your configuration looks like this:\\n"
cat "$ssh_dir/config.tmp" 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 else
echo "$key_path could not be read." printf "%s could not be read.\\n" "$key_path"
echo "Either it doesn't exist, or you dont have the correct permissions, please try again." printf "Either it doesn't exist, or you dont have the correct permissions, please try again.\\n"
setup_keys "$server_address" "$server_name" setup_keys "$server_address" "$server_name"
fi fi
} }
@ -139,14 +137,14 @@ setup_keys() {
pick_account() { pick_account() {
answers="1 2 3 4" answers="1 2 3 4"
answer="" answer=""
echo "What account would you like to configure?" printf "What account would you like to configure?\\n"
echo " 1) tilde.town" printf " 1) tilde.town\\n"
echo " 2) tilde.team" printf " 2) tilde.team\\n"
echo " 3) yourtilde.com" printf " 3) yourtilde.com\\n"
echo " 4) cosmic.voyage" printf " 4) cosmic.voyage\\n"
echo " 5) tilde.institute" printf " 5) tilde.institute\\n"
read -r answer read -r answer
if is_substring "$answer" "$answers" if is_substring "$answer" "$answers"
then then
case "$answer" in case "$answer" in
@ -167,7 +165,7 @@ pick_account() {
;; ;;
esac esac
else else
echo "\"$answer\" is not a valid option." printf "\"%s\" is not a valid option.\\n" "$answer"
pick_account pick_account
fi fi
} }

View File

@ -12,14 +12,14 @@ ssh_port="22"
ssh_dir="$HOME/.ssh" ssh_dir="$HOME/.ssh"
priv_key_path=" " priv_key_path=" "
get_field(){ get_field() {
field_num="$1" field_num="$1"
info="$2" info="$2"
echo "$info" | cut -f "$field_num" -d '|' printf "%s" "$info" | cut -f "$field_num" -d '|'
} }
error_box(){ error_box() {
error_text="$1" error_text="$1"
yad \ yad \
--posx="$mouse_x" \ --posx="$mouse_x" \
@ -31,7 +31,7 @@ error_box(){
--button="OK":0 --button="OK":0
} }
succ_box(){ succ_box() {
succ_text="$1" succ_text="$1"
yad \ yad \
--posx="$mouse_x" \ --posx="$mouse_x" \
@ -43,7 +43,7 @@ succ_box(){
--button="OK":0 --button="OK":0
} }
dec_box(){ dec_box() {
error_text="$1" error_text="$1"
yad \ yad \
--posx="$mouse_x" \ --posx="$mouse_x" \
@ -57,7 +57,7 @@ dec_box(){
return $? return $?
} }
info_box(){ info_box() {
info_text="$1" info_text="$1"
yad \ yad \
--posx="$mouse_x" \ --posx="$mouse_x" \
@ -96,17 +96,17 @@ get_info() {
ssh_port=$(get_field 4 "$info") ssh_port=$(get_field 4 "$info")
ssh_dir=$(get_field 5 "$info") ssh_dir=$(get_field 5 "$info")
priv_key_path=$(get_field 6 "$info") priv_key_path=$(get_field 6 "$info")
if test -z "$login" if test -z "$login"
then then
error_msg="No login provided!" error_msg="No login provided!"
error_num=$(( error_num + 1)) error_num=$((error_num + 1))
fi fi
if test -d "$(echo "$priv_key_path" | sed 's/\/ *$//')" if test -d "$(printf "%s" "$priv_key_path" | sed 's/\/ *$//')"
then then
error_msg="$error_msg\\nNo private key provided!" error_msg="${error_msg}\\nNo private key provided!"
error_num=$(( error_num + 1)) error_num=$((error_num + 1))
fi fi
if [ $error_num -gt 0 ] if [ $error_num -gt 0 ]
@ -119,10 +119,10 @@ get_info() {
} }
configure_server() { configure_server() {
ssh_current_config="$ssh_dir/config" ssh_current_config="${ssh_dir}/config"
ssh_temp_config="$ssh_dir/config.tmp" ssh_temp_config="${ssh_dir}/config.tmp"
ssh_host_dir="$ssh_dir/$server_address" ssh_host_dir="${ssh_dir}/${server_address}"
key_type=$(head -n 1 "$priv_key_path" \ key_type=$(head -n 1 "${priv_key_path}" \
| cut -f 2 -d ' ' \ | cut -f 2 -d ' ' \
| tr '[:upper:]' '[:lower:]') | tr '[:upper:]' '[:lower:]')
@ -130,9 +130,9 @@ configure_server() {
|| touch "$ssh_current_config" || touch "$ssh_current_config"
cp "$ssh_current_config" "$ssh_temp_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 then
dec_box "Host named $server_name already exists, overwrite?" \ dec_box "Host named ${server_name} already exists, overwrite?" \
|| get_info || get_info
if ! dec_box "Keep old config in file? (ssh will use the new one)" 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 in_old_host_block=0
while read -r line while read -r line
do do
if echo "$line" | grep -q "Host $server_name" if printf "%s" "$line" | grep -q "Host ${server_name}"
then then
in_old_host_block=0 in_old_host_block=0
elif echo "$line" | grep -q "Host " elif printf "%s" "$line" | grep -q "Host "
then then
in_old_host_block=1 in_old_host_block=1
fi fi
[ $in_old_host_block -eq 1 ] \ [ $in_old_host_block -eq 1 ] \
&& echo "$line" && printf "%s" "$line"
done < "$ssh_current_config" > "$ssh_temp_config" done < "$ssh_current_config" > "$ssh_temp_config"
fi fi
fi fi
mkdir -p "$ssh_host_dir" 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" printf "Host %s\\n" "$server_name"
echo " HostName $server_address" printf " HostName %s\\n" "$server_address"
echo " Port $ssh_port" printf " Port %s\\n" "$ssh_port"
echo " User $login" printf " User %s\\n" "$login"
echo " IdentityFile $ssh_host_dir/id_$key_type" printf " IdentityFile %s/id_%s\\n" "$ssh_host_dir" "$key_type"
} >> "$ssh_temp_config" } >> "$ssh_temp_config"
info_box "Attempting to log in with the provided credentials..." & info_box "Attempting to log in with the provided credentials..." &
info_pid=$(( $! + 2 )) # yikes, is this safe? info_pid=$(( $! + 2 )) # yikes, is this safe?
echo "$ssh_temp_config" printf "%s\\n" "$ssh_temp_config"
echo "$server_address" printf "%s\\n" "$server_address"
if ssh -qF "$ssh_temp_config" "$server_name" exit if ssh -qF "$ssh_temp_config" "$server_name" exit
then then
kill $info_pid 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" cp "$ssh_temp_config" "$ssh_current_config"
rm "$ssh_temp_config" rm "$ssh_temp_config"
exit # Without this the "Host already exists" box appears, investigate. exit # Without this the "Host already exists" box appears, investigate.

View File

@ -11,17 +11,17 @@ server_name=$(grep -E "[H,h]ost " "$ssh_config_file" \
| cut -f 2 -d ' ' \ | cut -f 2 -d ' ' \
| tr '\n' '!' \ | tr '\n' '!' \
| sed 's/!$//') # This only works in posix sh | sed 's/!$//') # This only works in posix sh
sshfs_local_path="$HOME/Remote/myfolder" sshfs_local_path="${HOME}/Remote/myfolder"
sshfs_remote_path="$HOME" sshfs_remote_path="$HOME"
get_field(){ get_field() {
field_num="$1" field_num="$1"
info="$2" info="$2"
echo "$info" | cut -f "$field_num" -d '|' printf "%s" "$info" | cut -f "$field_num" -d '|'
} }
error_box(){ error_box() {
error_text="$1" error_text="$1"
yad \ yad \
--posx="$mouse_x" \ --posx="$mouse_x" \
@ -33,7 +33,7 @@ error_box(){
--button="OK":0 --button="OK":0
} }
succ_box(){ succ_box() {
succ_text="$1" succ_text="$1"
yad \ yad \
--posx="$mouse_x" \ --posx="$mouse_x" \
@ -45,7 +45,7 @@ succ_box(){
--button="OK":0 --button="OK":0
} }
dec_box(){ dec_box() {
error_text="$1" error_text="$1"
yad \ yad \
--posx="$mouse_x" \ --posx="$mouse_x" \
@ -59,7 +59,7 @@ dec_box(){
return $? return $?
} }
info_box(){ info_box() {
info_text="$1" info_text="$1"
yad \ yad \
--posx="$mouse_x" \ --posx="$mouse_x" \
@ -91,14 +91,14 @@ get_info() {
--button="Continue":0 \ --button="Continue":0 \
"$ssh_config_file" "$server_name" "$sshfs_local_path" "$sshfs_remote_path") "$ssh_config_file" "$server_name" "$sshfs_local_path" "$sshfs_remote_path")
info_return="$?" info_return="$?"
if [ $info_return -eq 1 ] if [ $info_return -eq 1 ]
then then
exit exit
elif [ $info_return -eq 2 ] elif [ $info_return -eq 2 ]
then then
if ! "$setup_script_path" if ! "$setup_script_path"
then then
error_box "$setup_script_path did not finish successfully" error_box "${setup_script_path} did not finish successfully!"
get_info get_info
fi fi
fi fi
@ -110,13 +110,13 @@ get_info() {
if test -z "$sshfs_local_path" if test -z "$sshfs_local_path"
then then
error_msg="No local path provided!" error_msg="No local path provided!"
error_num=$(( error_num + 1)) error_num=$((error_num + 1))
fi fi
if test -z "$sshfs_remote_path" if test -z "$sshfs_remote_path"
then then
error_msg="$error_msg\\nNo remote path provided" error_msg="${error_msg}\\nNo remote path provided!"
error_num=$(( error_num + 1)) error_num=$((error_num + 1))
fi fi
if [ $error_num -gt 0 ] if [ $error_num -gt 0 ]
@ -135,27 +135,27 @@ configure_mount() {
test -e "$sshfs_local_path" \ test -e "$sshfs_local_path" \
|| mkdir -p "$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 then
if dec_box "Directory $sshfs_remote_path does not exist on remote host, create?" if dec_box "Directory $sshfs_remote_path does not exist on remote host, create?"
then 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 then
error_box "Failed to create remote directory $error_msg" error_box "Failed to create remote directory. ${error_msg}"
fi fi
fi fi
fi fi
if error_msg=$(sshfs \ if error_msg=$(sshfs \
"$server_name:$sshfs_remote_path" \ "${server_name}:${sshfs_remote_path}" \
"$sshfs_local_path" \ "$sshfs_local_path" \
-oauto_cache,reconnect,Compression=no 2>&1) -oauto_cache,reconnect,Compression=no 2>&1)
then then
kill $info_pid 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 else
kill $info_pid 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 fi
} }