v0.2 - updated and neatened code

This commit is contained in:
Ze'ev Schurmann 2024-08-01 20:01:59 +02:00
parent dd65e03810
commit 1e869ee9f2
2 changed files with 68 additions and 46 deletions

57
achar
View File

@ -1,5 +1,5 @@
## Title : ACHAR - Auto Completion of Hosts Add/Remove script ## Title : ACHAR - Auto Completion of Hosts Add/Remove script
## Version : 0.1 ## Version : 0.2
## GIT Repo : https://git.zaks.web.za/zaks-web/achar ## GIT Repo : https://git.zaks.web.za/zaks-web/achar
## Author : Ze'ev Schurmann ## Author : Ze'ev Schurmann
## Company : ZAKS Web ## Company : ZAKS Web
@ -12,7 +12,7 @@
## USAGE ## USAGE
## ##
## From the bash terminal, type: ## From the bash terminal, type:
## $ achar {command} {hostname} ## $ achar {command} {context}
## ##
## COMMANDS ## COMMANDS
## ##
@ -22,6 +22,7 @@
## ##
## disable - disables auto completion of hostnames for current system user ## disable - disables auto completion of hostnames for current system user
## $ achar disable ## $ achar disable
## * does not delete the list of hosts stored at ~/.achar/hosts
## ##
## enable - enables auto completion of hostnames for current system user ## enable - enables auto completion of hostnames for current system user
## $ achar enable ## $ achar enable
@ -29,15 +30,26 @@
## help - displays this block of text ## help - displays this block of text
## $ achar help ## $ achar help
## ##
## license - displays the GPL3 and Later license
## $ achar license
##
## list - list all existing hosts from your list of common hosts ## list - list all existing hosts from your list of common hosts
## $ achar list ## $ achar list
## * You can use grep to search for a specific hostname or part there of ## * You can use context to search for a specific hostname or part there of
## $ achar list | grep username ## $ achar list username
## $ achar list | grep hostname ## $ achar list part-of-hostname
## ##
## remove - removes an existing host from your list of common hosts ## remove - removes an existing host from your list of common hosts
## $ achar rem username@hostname.domainname.tld ## $ achar remove username@hostname.domainname.tld
## $ achar rem username@1.2.3.4 ## $ achar remove username@1.2.3.4
##
## reset - deletes all saved hosts
## $ achar reset
##
## uninstall - uninstalls achar from your system
## $ sudo achar uninstall
## # achar uninstall
## * does not delete the list of hosts stored at ~/.achar/hosts
## ##
## update - checks for an update to ACHAR and prompts you to install it ## update - checks for an update to ACHAR and prompts you to install it
## $ achar update ## $ achar update
@ -51,9 +63,7 @@ function achar_add {
local username_and_hostname local username_and_hostname
username_and_hostname=$1 username_and_hostname=$1
validate_achar_installation hosts validate_achar_installation hosts
validate_host "$username_and_hostname" validate_host "$username_and_hostname"
if [[ -z $(cat ~/.achar/hosts | grep "$username_and_hostname") ]]; then if [[ -z $(cat ~/.achar/hosts | grep "$username_and_hostname") ]]; then
@ -63,7 +73,6 @@ function achar_add {
else else
echo "$username_and_hostname is already added!" >&2 echo "$username_and_hostname is already added!" >&2
exit 1 exit 1
fi fi
@ -148,7 +157,6 @@ function achar_help {
function achar_license { function achar_license {
echo echo
curl -s https://git.zaks.web.za/zaks-web/achar/raw/branch/main/LICENSE | fmt curl -s https://git.zaks.web.za/zaks-web/achar/raw/branch/main/LICENSE | fmt
if [[ $? != 0 ]]; then if [[ $? != 0 ]]; then
@ -203,9 +211,12 @@ function achar_remove {
function achar_reset { function achar_reset {
validate_achar_installation hosts validate_achar_installation hosts
echo "This will delete all saved hosts and"
echo "and start from scratch."
echo
echo "Press ENTER to continue or CTRL+C to STOP!"
read text
rm ~/.achar/hosts rm ~/.achar/hosts
touch ~/.achar/hosts touch ~/.achar/hosts
} }
@ -223,10 +234,11 @@ function achar_uninstall {
echo echo
echo "If you want to disable the autocompletions, then" echo "If you want to disable the autocompletions, then"
echo "you must run \"achar disable\" for each user." echo "you must run \"achar disable\" for each user."
echo "This is recommended if you are planning to install"
echo "a new version."
echo echo
echo "Press ENTER to continue or CTRL+C to STOP!" echo "Press ENTER to continue or CTRL+C to STOP!"
read text read text
rm $script_path rm $script_path
else else
@ -290,8 +302,8 @@ function achar_update {
fi fi
echo "current version is $version_number" echo "Current version is $version_number"
echo "latest version is $stable_version_number" echo "Latest version is $stable_version_number"
update_available="false" update_available="false"
for ((i=0; i<${#version_number_array[@]}; i++)); do for ((i=0; i<${#version_number_array[@]}; i++)); do
@ -326,6 +338,12 @@ function achar_update {
if [[ $update_available == "true" ]]; then if [[ $update_available == "true" ]]; then
echo "Update is avaiable!" echo "Update is avaiable!"
echo
echo "First run \"achar disable\" for all users."
echo "Then run \"achar uninstall\" as root or sudo."
echo "Lastly, follow the instructions at"
echo "https://git.zaks.web.za/zaks-web/achar"
echo "to install the latest version."
else else
@ -339,7 +357,6 @@ function validate_achar_installation {
local achar_installation_error_code="" local achar_installation_error_code=""
local what_test local what_test
what_test=$1 what_test=$1
if [[ -d ~/.achar ]]; then if [[ -d ~/.achar ]]; then
@ -373,7 +390,6 @@ function validate_achar_installation {
else else
echo "File corrupt: ~/.achar/completions" >&2 echo "File corrupt: ~/.achar/completions" >&2
exit 1 exit 1
fi fi
@ -465,7 +481,6 @@ function validate_host {
if [[ -z $1 ]]; then if [[ -z $1 ]]; then
echo "Unexpected Error!" echo "Unexpected Error!"
exit 1 exit 1
fi fi
@ -476,7 +491,6 @@ function validate_host {
if [[ ${#temp_array[@]} != 1 ]]; then if [[ ${#temp_array[@]} != 1 ]]; then
echo "Invalid Hostname!" echo "Invalid Hostname!"
exit 1 exit 1
fi fi
@ -489,7 +503,6 @@ function validate_host {
if [[ ${#temp_array[@]} != 2 ]]; then if [[ ${#temp_array[@]} != 2 ]]; then
echo "Invalid Hostname!" echo "Invalid Hostname!"
exit 1 exit 1
fi fi
@ -508,7 +521,6 @@ function validate_host {
else else
echo "Hostname is not an valid!" echo "Hostname is not an valid!"
exit 1 exit 1
fi fi
@ -516,7 +528,6 @@ function validate_host {
if [[ ${username:0:1} =~ ^[0-9]$ ]] || [[ ! $username =~ ^[A-Za-z0-9_.-]+$ ]]; then if [[ ${username:0:1} =~ ^[0-9]$ ]] || [[ ! $username =~ ^[A-Za-z0-9_.-]+$ ]]; then
echo "Username is not a valid format!" echo "Username is not a valid format!"
exit 1 exit 1
fi fi

View File

@ -1,5 +1,5 @@
## Title : ACHAR - Auto Completion of Hosts Add/Remove script ## Title : ACHAR - Auto Completion of Hosts Add/Remove script
## Version : 0.1 ## Version : 0.2
## GIT Repo : https://git.zaks.web.za/zaks-web/achar ## GIT Repo : https://git.zaks.web.za/zaks-web/achar
## Author : Ze'ev Schurmann ## Author : Ze'ev Schurmann
## Company : ZAKS Web ## Company : ZAKS Web
@ -12,7 +12,7 @@
## USAGE ## USAGE
## ##
## From the bash terminal, type: ## From the bash terminal, type:
## $ achar {command} {hostname} ## $ achar {command} {context}
## ##
## COMMANDS ## COMMANDS
## ##
@ -22,6 +22,7 @@
## ##
## disable - disables auto completion of hostnames for current system user ## disable - disables auto completion of hostnames for current system user
## $ achar disable ## $ achar disable
## * does not delete the list of hosts stored at ~/.achar/hosts
## ##
## enable - enables auto completion of hostnames for current system user ## enable - enables auto completion of hostnames for current system user
## $ achar enable ## $ achar enable
@ -29,15 +30,26 @@
## help - displays this block of text ## help - displays this block of text
## $ achar help ## $ achar help
## ##
## license - displays the GPL3 and Later license
## $ achar license
##
## list - list all existing hosts from your list of common hosts ## list - list all existing hosts from your list of common hosts
## $ achar list ## $ achar list
## * You can use grep to search for a specific hostname or part there of ## * You can use context to search for a specific hostname or part there of
## $ achar list | grep username ## $ achar list username
## $ achar list | grep hostname ## $ achar list part-of-hostname
## ##
## remove - removes an existing host from your list of common hosts ## remove - removes an existing host from your list of common hosts
## $ achar rem username@hostname.domainname.tld ## $ achar remove username@hostname.domainname.tld
## $ achar rem username@1.2.3.4 ## $ achar remove username@1.2.3.4
##
## reset - deletes all saved hosts
## $ achar reset
##
## uninstall - uninstalls achar from your system
## $ sudo achar uninstall
## # achar uninstall
## * does not delete the list of hosts stored at ~/.achar/hosts
## ##
## update - checks for an update to ACHAR and prompts you to install it ## update - checks for an update to ACHAR and prompts you to install it
## $ achar update ## $ achar update
@ -51,9 +63,7 @@ function achar_add {
local username_and_hostname local username_and_hostname
username_and_hostname=$1 username_and_hostname=$1
validate_achar_installation hosts validate_achar_installation hosts
validate_host "$username_and_hostname" validate_host "$username_and_hostname"
if [[ -z $(cat ~/.achar/hosts | grep "$username_and_hostname") ]]; then if [[ -z $(cat ~/.achar/hosts | grep "$username_and_hostname") ]]; then
@ -63,7 +73,6 @@ function achar_add {
else else
echo "$username_and_hostname is already added!" >&2 echo "$username_and_hostname is already added!" >&2
exit 1 exit 1
fi fi
@ -148,7 +157,6 @@ function achar_help {
function achar_license { function achar_license {
echo echo
curl -s https://git.zaks.web.za/zaks-web/achar/raw/branch/main/LICENSE | fmt curl -s https://git.zaks.web.za/zaks-web/achar/raw/branch/main/LICENSE | fmt
if [[ $? != 0 ]]; then if [[ $? != 0 ]]; then
@ -203,9 +211,12 @@ function achar_remove {
function achar_reset { function achar_reset {
validate_achar_installation hosts validate_achar_installation hosts
echo "This will delete all saved hosts and"
echo "and start from scratch."
echo
echo "Press ENTER to continue or CTRL+C to STOP!"
read text
rm ~/.achar/hosts rm ~/.achar/hosts
touch ~/.achar/hosts touch ~/.achar/hosts
} }
@ -223,10 +234,11 @@ function achar_uninstall {
echo echo
echo "If you want to disable the autocompletions, then" echo "If you want to disable the autocompletions, then"
echo "you must run \"achar disable\" for each user." echo "you must run \"achar disable\" for each user."
echo "This is recommended if you are planning to install"
echo "a new version."
echo echo
echo "Press ENTER to continue or CTRL+C to STOP!" echo "Press ENTER to continue or CTRL+C to STOP!"
read text read text
rm $script_path rm $script_path
else else
@ -290,8 +302,8 @@ function achar_update {
fi fi
echo "current version is $version_number" echo "Current version is $version_number"
echo "latest version is $stable_version_number" echo "Latest version is $stable_version_number"
update_available="false" update_available="false"
for ((i=0; i<${#version_number_array[@]}; i++)); do for ((i=0; i<${#version_number_array[@]}; i++)); do
@ -326,6 +338,12 @@ function achar_update {
if [[ $update_available == "true" ]]; then if [[ $update_available == "true" ]]; then
echo "Update is avaiable!" echo "Update is avaiable!"
echo
echo "First run \"achar disable\" for all users."
echo "Then run \"achar uninstall\" as root or sudo."
echo "Lastly, follow the instructions at"
echo "https://git.zaks.web.za/zaks-web/achar"
echo "to install the latest version."
else else
@ -339,7 +357,6 @@ function validate_achar_installation {
local achar_installation_error_code="" local achar_installation_error_code=""
local what_test local what_test
what_test=$1 what_test=$1
if [[ -d ~/.achar ]]; then if [[ -d ~/.achar ]]; then
@ -373,7 +390,6 @@ function validate_achar_installation {
else else
echo "File corrupt: ~/.achar/completions" >&2 echo "File corrupt: ~/.achar/completions" >&2
exit 1 exit 1
fi fi
@ -465,7 +481,6 @@ function validate_host {
if [[ -z $1 ]]; then if [[ -z $1 ]]; then
echo "Unexpected Error!" echo "Unexpected Error!"
exit 1 exit 1
fi fi
@ -476,7 +491,6 @@ function validate_host {
if [[ ${#temp_array[@]} != 1 ]]; then if [[ ${#temp_array[@]} != 1 ]]; then
echo "Invalid Hostname!" echo "Invalid Hostname!"
exit 1 exit 1
fi fi
@ -489,7 +503,6 @@ function validate_host {
if [[ ${#temp_array[@]} != 2 ]]; then if [[ ${#temp_array[@]} != 2 ]]; then
echo "Invalid Hostname!" echo "Invalid Hostname!"
exit 1 exit 1
fi fi
@ -508,7 +521,6 @@ function validate_host {
else else
echo "Hostname is not an valid!" echo "Hostname is not an valid!"
exit 1 exit 1
fi fi
@ -516,7 +528,6 @@ function validate_host {
if [[ ${username:0:1} =~ ^[0-9]$ ]] || [[ ! $username =~ ^[A-Za-z0-9_.-]+$ ]]; then if [[ ${username:0:1} =~ ^[0-9]$ ]] || [[ ! $username =~ ^[A-Za-z0-9_.-]+$ ]]; then
echo "Username is not a valid format!" echo "Username is not a valid format!"
exit 1 exit 1
fi fi