feat: param required function
This commit is contained in:
parent
5270b253fe
commit
c59ba0e86b
2 changed files with 73 additions and 31 deletions
|
@ -7,10 +7,8 @@ FILE_NAME="nekoray.zip"
|
||||||
|
|
||||||
install_nekoray() {
|
install_nekoray() {
|
||||||
download_file "$DOWNLOAD_URL" "$FILE_NAME"
|
download_file "$DOWNLOAD_URL" "$FILE_NAME"
|
||||||
|
extract_file "/tmp/$FILE_NAME"
|
||||||
opt_installer "/tmp/$FILE_NAME"
|
move_to_opt "/tmp/nekoray"
|
||||||
}
|
}
|
||||||
|
|
||||||
check_sudo
|
|
||||||
|
|
||||||
install_nekoray
|
install_nekoray
|
||||||
|
|
|
@ -21,6 +21,16 @@ check() {
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
params_required() {
|
||||||
|
local param_name="$1"
|
||||||
|
local param_value="$2"
|
||||||
|
|
||||||
|
if [ -z "$param_value" ]; then
|
||||||
|
log "Parameter '$param_name' is required but not provided."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
check_command() {
|
check_command() {
|
||||||
if ! command -v $1 &>/dev/null; then
|
if ! command -v $1 &>/dev/null; then
|
||||||
log "$1 is not installed. Please install $1 to continue."
|
log "$1 is not installed. Please install $1 to continue."
|
||||||
|
@ -40,35 +50,9 @@ ask_prompt() {
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
opt_installer() {
|
|
||||||
check_command unzip
|
|
||||||
|
|
||||||
local file_path="$1"
|
|
||||||
local filename=$(basename "$file_path")
|
|
||||||
local opt_dir="/opt"
|
|
||||||
local target_file="$opt_dir/$filename"
|
|
||||||
|
|
||||||
if [ ! -e $file_path ]; then
|
|
||||||
log "File $file_path not found."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ ! -e $target_file ]; then
|
|
||||||
cp $file_path "$opt_dir/$filename"
|
|
||||||
fi
|
|
||||||
|
|
||||||
unzip -o $target_file -d $opt_dir
|
|
||||||
|
|
||||||
if [ $? -eq 0 ]; then
|
|
||||||
log "File moved to $opt_dir successfully."
|
|
||||||
else
|
|
||||||
log "Failed to move file to $opt_dir."
|
|
||||||
return 1
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
download_file() {
|
download_file() {
|
||||||
check_command wget
|
check_command wget
|
||||||
|
params_required "URL" "$1"
|
||||||
|
|
||||||
local url="$1"
|
local url="$1"
|
||||||
local tmp_dir="/tmp"
|
local tmp_dir="/tmp"
|
||||||
|
@ -93,3 +77,63 @@ download_file() {
|
||||||
log "Failed to download file from $url"
|
log "Failed to download file from $url"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extract_file() {
|
||||||
|
params_required "File" "$1"
|
||||||
|
check_command unzip
|
||||||
|
|
||||||
|
local file_path="$1"
|
||||||
|
local tmp_dir="/tmp"
|
||||||
|
local target_destination=${2:-$tmp_dir}
|
||||||
|
|
||||||
|
if [ ! -e $file_path ]; then
|
||||||
|
log "File $file_path not found."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
unzip -o $file_path -d $target_destination
|
||||||
|
|
||||||
|
if [ $? -eq 0 ]; then
|
||||||
|
log "File extracted successfully: $tmp_dir/$file_path"
|
||||||
|
else
|
||||||
|
log "Failed to extract file !"
|
||||||
|
fi
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
move_to_opt() {
|
||||||
|
params_required "Source" "$1"
|
||||||
|
|
||||||
|
local source_path="$1"
|
||||||
|
|
||||||
|
if [ -z "$source_path" ]; then
|
||||||
|
log "No source path provided."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ ! -e "$source_path" ]; then
|
||||||
|
log "Source path '$source_path' does not exist."
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ ! -d "$source_path" ] && [ ! -f "$source_path" ]; then
|
||||||
|
log "Source path '$source_path' is neither a directory nor a file."
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
local filename="$(basename "$source_path")"
|
||||||
|
local destination="/opt/$filename"
|
||||||
|
|
||||||
|
if [ -e "$destination" ]; then
|
||||||
|
if ask_prompt "File or directory '$destination' already exists in /opt. Do you want to cancel ?"; then
|
||||||
|
log "Continuing with existing file or directory: $destination"
|
||||||
|
exit 1
|
||||||
|
else
|
||||||
|
log "Deleting existing file or directory: $destination"
|
||||||
|
sudo rm -rf "$destination"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
sudo mv "$source_path" "$destination"
|
||||||
|
fi
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue