From af88318630a0b23ffc76296ed98f3b79ed687209 Mon Sep 17 00:00:00 2001 From: Alan Weinstock Date: Tue, 2 May 2023 14:18:50 -0700 Subject: [PATCH 1/7] updated screenshot to latest --- README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index 83ba8e2..f71a8ad 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,4 @@ With all of these tools combined, you can make powerful interactive scripts with run `./demo.sh` for example output -![image](https://user-images.githubusercontent.com/8482805/224919713-d1ec4723-1909-470d-93d6-1b7091de8de8.png) -![image](https://user-images.githubusercontent.com/8482805/224919800-2d4450e4-9fc7-4ef7-b432-9ccfd0278880.png) -![image](https://user-images.githubusercontent.com/8482805/224919970-0444b849-5959-49b5-a187-b6a214ccc84f.png) +![image](https://user-images.githubusercontent.com/8482805/235781738-f31481a8-946d-4051-9210-3df28687a19c.png) From 17a30ba1da7e6446aea82b7c5cccad9b69f79fef Mon Sep 17 00:00:00 2001 From: pyr0ball Date: Sat, 6 May 2023 01:47:55 -0700 Subject: [PATCH 2/7] added dry-run functions --- functions | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/functions b/functions index f0285b3..5e6df50 100644 --- a/functions +++ b/functions @@ -421,6 +421,24 @@ logger(){ ) } +run(){ + _cmd=$@ + if [[ dry_run == true ]] ; then + boxline "DryRun: $_cmd" + else + $_cmd + fi +} + +run-and-log(){ + _cmd=$@ + if [[ dry_run == true ]] ; then + logger "DryRun: $_cmd" + else + logger $_cmd + fi +} + ctrl_c(){ echo -e "\n" fail "User interrupted with Ctrl-C" From 3987ccc007d2eb7b7bca64af2a0727ba1489b589 Mon Sep 17 00:00:00 2001 From: pyr0ball Date: Sat, 6 May 2023 01:48:31 -0700 Subject: [PATCH 3/7] added logo function --- functions | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/functions b/functions index 5e6df50..eba874f 100644 --- a/functions +++ b/functions @@ -444,6 +444,35 @@ ctrl_c(){ fail "User interrupted with Ctrl-C" } +prbl-logo(){ + echo -e \ +" + _!*(xL}}xvr!,. + >}KM3nLL}}}}}LLTh%5u>. + P8}. + ?g2!%888BBev****^!!!!!.LB&? + >gF=Rg88#5 ,^}#Be- + Pg'a888#B ,unnnnn}- .ZO_N#Bgr +-gZ-g888B, .U88!j#B#Qs. +.NM-D88N! .j888=XQQQOQQ= + 28>u8g< ,!!!!!!!!!!!!!!!!>LpQ888K,QQgy ,V> + _OM:K? .&#Q88888888888888#@@#888M:p##Q8G! . + ,WM!.!!^p#@@##Q88g8888QB#@@#Q888e!W#####BBg2! + .u&n^yg888Q##@@@@@@@@@@#B8888K*}Q###BQQQBB##gL. + _nDj?xPggg888QQQQQ888888Wx?zQ####BQB88&2vvnWQ( + .^yOjLLL}jP%OEOM5suLxLjQ#######Q#@Q8Z. r< + .!}%QQNWKessaG%gB###########BQ@@@#R?. + .!LP8B###Q5V%8B##B88QQB##BB#@@@@@Qe?, + _*TW#P_ !KQ#@BM^^}OB##BB#@@@@@@@Ba< + - + >sQ@@? + _nB#, + -hv +" +} # ensure ctrl-c to cancel script ends the entire process and not just the current function # trap ctrl_c INT # Commented out by default to prevent abnormal background exits From 326833ae42fda9bfd313f7c930ed060e48f6d5f2 Mon Sep 17 00:00:00 2001 From: pyr0ball Date: Sun, 7 May 2023 21:55:33 -0700 Subject: [PATCH 4/7] inverted logic trap for dry runs --- functions | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/functions b/functions index eba874f..2ac1cfa 100644 --- a/functions +++ b/functions @@ -337,10 +337,11 @@ boxseparator(){ # For printing center-justified text. To change the padding character, replace the ' ' <~~ whitespace in front of the '{' in the padding variable center() { + local _cmd=$@ local padding=""; padding="$(printf '%0.1s' \ {1..100})" #padding=" " - printf '%*.*s %s %*.*s\n' 0 "$(((BOXWIDTH-2-${#1})/2))" "${padding}" "$1" 0 "$(((BOXWIDTH-1-${#1})/2))" "${padding}" + printf '%*.*s %s %*.*s\n' 0 "$(((BOXWIDTH-2-${#_cmd[@]})/2))" "${padding}" "$_cmd" 0 "$(((BOXWIDTH-1-${#_cmd})/2))" "${padding}" } # For printing spanned text, e.g. single-pair lists ($name...$title) @@ -423,19 +424,19 @@ logger(){ run(){ _cmd=$@ - if [[ dry_run == true ]] ; then - boxline "DryRun: $_cmd" - else + if [[ $dry_run != true ]] ; then $_cmd + else + boxline "DryRun: $_cmd" fi } run-and-log(){ _cmd=$@ - if [[ dry_run == true ]] ; then - logger "DryRun: $_cmd" - else + if [[ $dry_run != true ]] ; then logger $_cmd + else + logger "DryRun: $_cmd" fi } From adf0fecbdbed55a6956c5a6f7a0f2909eb304055 Mon Sep 17 00:00:00 2001 From: pyr0ball Date: Sun, 7 May 2023 23:54:20 -0700 Subject: [PATCH 5/7] simplified colorization output --- functions | 54 +++++++++++++++++++++++++++--------------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/functions b/functions index 6068a39..c0ed681 100644 --- a/functions +++ b/functions @@ -2,7 +2,7 @@ # pyr0ball script functions library # Initial Vars -functionsrev=1.1.11 +functionsrev=1.1.12 #scriptname="${BASH_SOURCE[0]##*/}" #rundir="${BASH_SOURCE[0]%/*}" #rundir_absolute=$(cd $rundir && pwd) @@ -41,33 +41,33 @@ borderchar="#" # Sets default '#' if not already set # Colorization options if [[ "$TERM" != "linux" ]] ; then - red=$(echo -e "${ESC}[38;5;1m") # red - grn=$(echo -e "${ESC}[38;5;2m") # green - ylw=$(echo -e "${ESC}[38;5;3m") # yellow - blu=$(echo -e "${ESC}[38;5;27m") # blue - lbl=$(echo -e "${ESC}[38;5;69m") # light blue - mag=$(echo -e "${ESC}[38;5;5m") # magenta - cyn=$(echo -e "${ESC}[38;5;6m") # cyan - pur=$(echo -e "${ESC}[38;5;135m") # purple - ong=$(echo -e "${ESC}[38;5;166m") # orange - lyl=$(echo -e "${ESC}[38;5;228m") # light yellow - lrd=$(echo -e "${ESC}[38;5;196m") # light red - gry=$(echo -e "${ESC}[38;5;240m") # Grey - norm=$(echo -e "${ESC}[39m") # default/normal + red="${ESC}[38;5;1m" # red + grn="${ESC}[38;5;2m" # green + ylw="${ESC}[38;5;3m" # yellow + blu="${ESC}[38;5;27m" # blue + lbl="${ESC}[38;5;69m" # light blue + mag="${ESC}[38;5;5m" # magenta + cyn="${ESC}[38;5;6m" # cyan + pur="${ESC}[38;5;135m" # purple + ong="${ESC}[38;5;166m" # orange + lyl="${ESC}[38;5;228m" # light yellow + lrd="${ESC}[38;5;196m" # light red + gry="${ESC}[38;5;240m" # Grey + norm=$"${ESC}[39m" # default/normal # - bld=$(echo -e "${ESC}[1m") # bold - unb=$(echo -e "${ESC}[21m") # un-bold - dim=$(echo -e "${ESC}[2m") # dim - und=$(echo -e "${ESC}[22m") # un-dim - unl=$(echo -e "${ESC}[4m") # underline - nln=$(echo -e "${ESC}[24") # not-underline - blk=$(echo -e "${ESC}[5") # blinking - unbl=$(echo -e "${ESC}[25") # stop blinking - inv=$(echo -e "${ESC}[7m") # invert - rsinv=$(echo -e "${ESC}[27") # reset http://www.endmemo.com/unicode/unicodeconverter.phpnvert - hid=$(echo -e "${ESC}[8") # hidden - unh=$(echo -e "${ESC}[28") # unhide - dfl=$(echo -e "${ESC}[0m") # restore default + bld="${ESC}[1m" # bold + unb="${ESC}[21m" # un-bold + dim="${ESC}[2m" # dim + und="${ESC}[22m" # un-dim + unl="${ESC}[4m" # underline + nln="${ESC}[24" # not-underline + blk="${ESC}[5" # blinking + unbl="${ESC}[25" # stop blinking + inv="${ESC}[7m" # invert + rsinv="${ESC}[27" # reset http://www.endmemo.com/unicode/unicodeconverter.phpnvert + hid="${ESC}[8" # hidden + unh="${ESC}[28" # unhide + dfl="${ESC}[0m" # restore default fi # Extra Unicode Character Manipulation From 868ff8831fd5b4bf0f7e75673178fbf51da98a91 Mon Sep 17 00:00:00 2001 From: pyr0ball Date: Mon, 8 May 2023 16:45:51 -0700 Subject: [PATCH 6/7] added catch fo rmissing logfile --- functions | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/functions b/functions index c0ed681..c15a656 100644 --- a/functions +++ b/functions @@ -2,7 +2,7 @@ # pyr0ball script functions library # Initial Vars -functionsrev=1.1.12 +functionsrev=1.1.13 #scriptname="${BASH_SOURCE[0]##*/}" #rundir="${BASH_SOURCE[0]%/*}" #rundir_absolute=$(cd $rundir && pwd) @@ -411,19 +411,6 @@ popdfail(){ fail "$@" } -logger(){ - $@ 2>&1 | tee >( - while IFS= read -r line; do - # strip any escaped strings for logging output - _line=$(echo $line | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | tr -dc '[:print:]') - # only print out non-empty lines to log - if [[ "$_line" != "" ]] ; then - printf '[%s] %s\n' "${scriptname}][${pretty_date}" "$_line" - fi - done >> $logfile - ) -} - run(){ _cmd=$@ if [[ $dry_run != true ]] ; then @@ -442,6 +429,23 @@ run-and-log(){ fi } +logger(){ + if [ ! -f $logfile ] ; then + run mkdir -p ${rundir%/*} + touch $logfile + fi + $@ 2>&1 | tee >( + while IFS= read -r line; do + # strip any escaped strings for logging output + _line=$(echo $line | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | tr -dc '[:print:]') + # only print out non-empty lines to log + if [[ "$_line" != "" ]] ; then + printf '[%s] %s\n' "${scriptname}][${pretty_date}" "$_line" + fi + done >> $logfile + ) +} + ctrl_c(){ echo -e "\n" fail "User interrupted with Ctrl-C" From 87d3e369cd55f6e04765e64791e4c5a8e636823a Mon Sep 17 00:00:00 2001 From: pyr0ball Date: Mon, 8 May 2023 22:17:36 -0700 Subject: [PATCH 7/7] added file handling functions --- functions | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 89 insertions(+), 1 deletion(-) diff --git a/functions b/functions index c15a656..ef4778b 100644 --- a/functions +++ b/functions @@ -2,7 +2,7 @@ # pyr0ball script functions library # Initial Vars -functionsrev=1.1.13 +functionsrev=1.1.14 #scriptname="${BASH_SOURCE[0]##*/}" #rundir="${BASH_SOURCE[0]%/*}" #rundir_absolute=$(cd $rundir && pwd) @@ -446,6 +446,94 @@ logger(){ ) } +# File handling functions + +take-backup(){ + name="$1" + if [[ $update_run != true ]] ; then + # Check if a backup file or symbolic link already exists + if [[ -e "$name.bak" || -L "$name.bak" ]]; then + run boxline " $name.bak backup already exists" + else + # Check if the file is a hidden file (starts with a dot) + if [[ "$name" == .* ]]; then + # Add a dot to the beginning of the backup file name + backup_name=".${name}.bak" + else + # Create the backup file name by appending ".bak" to the original file name + backup_name="${name}.bak" + fi + # Copy the file to the backup file with preservation of file attributes + run cp -p "$name" "$backup_name" + # Add the original file to the list of backup files + backup_files+=("$name") + # Log the original file name to the backup file list file + run echo "$name" >> "$rundir/backup_files.list" + fi + fi +} + +restore-backup(){ + run echo "${#backup_files[@]}" + for file in "${backup_files[@]}" ; do + run cp "$file".bak $file + run echo "$file is restored" + done + backup_files=() + if [ -f $rundir/backup_files.list ] ; then + run rm $rundir/backup_files.list + fi +} + +install-file(){ + local _source="$1" + local _destination="$2" + local _source_root="$3" + local _filename=${_source##*/} + local _destination_file=${_destination}/${_filename#${_source_root}} + installed_files+=("${_destination_file}") + if [[ $update_run == true ]] ; then + run boxline "$scriptname: added file ${_destination_file} to list" + else + run cp -p $_source $_destination_file && boxline "Installed ${_filename}" || warn "Unable to install ${_filename}" + run echo "${_destination_file}" >> $rundir/installed_files.list + fi +} + +install-dir() { + local _source="$1" + local _destination="$2" + installed_dirs+=("$_source -> $_destination") + # Iterate through all files in the source directory recursively + while IFS= read -r -d '' source_file; do + # Construct the destination file path by removing the source directory path + # and appending it to the destination directory path + local _filename="${source_file#${_source}}" + local destination_file="${_destination}/${source_file#${_source}}" + # Create the parent directory of the destination file if it doesn't exist + # Log the destination file path to the logfile + #echo "$destination_file" >> "$logfile" + installed_files+=($destination_file) + if [[ $update_run == true ]] ; then + run boxline "$scriptname: added file ${destination_file} to list" + else + run mkdir -p "$(dirname "$destination_file")" + run cp -p ${_source}${_filename} $destination_file && boxline "Installed ${_filename}" || warn "Unable to install ${_filename}" + run echo "${destination_file}" >> $rundir/installed_files.list + fi + done < <(find "$_source" -type f -print0) +} + +clone-repo(){ + # git clone + local _url=$1 + local _destination=$2 + if [ ! -z $_destination ] ; then + run mkdir -p $_destination + fi + run git clone --recurse-submodules $_url $_destination +} + ctrl_c(){ echo -e "\n" fail "User interrupted with Ctrl-C"