Skip to content

Instantly share code, notes, and snippets.

@wpcarro
Last active August 9, 2019 13:18
Show Gist options
  • Save wpcarro/04ee63bb3af96aba2c22448a99955329 to your computer and use it in GitHub Desktop.
Save wpcarro/04ee63bb3af96aba2c22448a99955329 to your computer and use it in GitHub Desktop.
################################################################################
# 8-bit colors
################################################################################
black='\u001b[30m'
red='\u001b[31m'
green='\u001b[32m'
yellow='\u001b[33m'
blue='\u001b[34m'
magenta='\u001b[35m'
cyan='\u001b[36m'
white='\u001b[37m'
bright_black='\u001b[30;1m'
bright_red='\u001b[31;1m'
bright_green='\u001b[32;1m'
bright_yellow='\u001b[33;1m'
bright_blue='\u001b[34;1m'
bright_magenta='\u001b[35;1m'
bright_cyan='\u001b[36;1m'
bright_white='\u001b[37;1m'
################################################################################
# Documentation and error messages
################################################################################
echo_info() {
# Echos an informational message.
#
# depends_variable blue
echo -e "${blue}[INFO]: $1"
}
echo_warn() {
# Echos a warning message.
#
# depends_variable yellow
echo -e "${yellow}[WARNING]: $1"
}
echo_error() {
# Echos an error message.
#
# depends_variable red
echo -e "${red}[ERROR]: $1"
}
unsupported_input() {
# Generic error message. Consume herein to standardize the error messages.
# Pass the supported inputs as $1.
#
# depends error_error
echo_error "Unsupported input. This function only supports the following inputs: $1. Exiting..."
}
depends() {
# Prints a message explaining a function's dependencies. Consume here to
# standardize the error messages.
# Pass the dependencies as $1.
#
# depends echo_info
echo_info "This function depends on the following functions: $@"
}
depends_variable() {
# Prints a message explaining a dependency on a variable. Consume here to
# standardize the error messages.
# Pass the dependencies as $1.
#
# depends echo_info
echo_info "This function depends on the following variables: $@"
}
compliments() {
# Prints a message explaining that a function compliments another function.
# Think of complimentary functions as `zip` and `unzip`.
#
# depends echo_info
echo_info "This function compliments the \`$1\` function."
}
################################################################################
# Filesystem operations
################################################################################
tar_dir() {
# Tars dir as dir.tar. Removes dir.
# compliments untar_dir
tar -cf "$1.tar" "$(basename $1)" && rm -rf "$1"
}
untar_dir() {
# Untars dir.tar as dir. Removes dir.tar.
# compliments tar_dir
tar -xvf "$1" && rm "$1"
}
targz_dir() {
# Tars a dir as dir.tar.gz.
# compliments untargz_dir
tar -czf "$1.tar.gz" "$(basename $1)"; rm -rf "$1"
}
untargz_dir() {
# Untars dir.tar.gz as dir. Removes dir.tar.gz.
# compliments targz_dir
tar -xzvf "$1" && rm "$1"
}
zip_dir() {
# Zips dir as dir.zip. Removes dir.
# compliments unzip_dir
zip -r "$1.zip" "$(basename $1)" && rm -rf "$1"
}
unzip_dir() {
# Unzips dir.zip as dir. Removes dir.zip.
# compliments zip_dir
unzip "$1" && rm "$1"
}
archive() {
# Generic function for archiving directories
#
# depends tar_dir targz_dir zip_dir
# compliments unarchive
printf "Which type of archive would you like to like create? (tar, tar.gz, zip) "
case $(read -e) in
tar) tar_dir "$1";;
tar.gz) targz_dir "$1";;
zip) zip_dir "$1";;
*) unsupported_input "tar, tar.gz, zip";;
esac
}
unarchive() {
# Generic way to unarchive files.
# Currently supports the following extensions:
# - .tar
# - .tar.gz
# - .zip
#
# depends untar unzip
# compliments archive
case $1 in
*.tar.gz) untargz_dir "$1";;
*.tar) untar_dir "$1";;
*.zip) unzip_dir "$1";;
*) unsupported_input ".tar, .tar.zip, .zip"
esac
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment