Skip to content

Instantly share code, notes, and snippets.

@ARHEIO
Created July 26, 2021 02:32
Show Gist options
  • Save ARHEIO/bb181e6726b2529ab703dd474eeadfe2 to your computer and use it in GitHub Desktop.
Save ARHEIO/bb181e6726b2529ab703dd474eeadfe2 to your computer and use it in GitHub Desktop.
Do something with bash script
#!/bin/bash
set -euo pipefail
function print_bold {
local text_bold=$(tput bold)
local text_normal=$(tput sgr0)
echo "${text_bold}$1${text_normal}"
}
function print_error {
local text_red=$(tput setaf 1)
local text_normal=$(tput sgr0)
echo "${text_red}$1${text_normal}"
}
function print_info {
local text_yellow=$(tput setaf 3)
local text_normal=$(tput sgr0)
echo "${text_yellow}$1${text_normal}"
}
function print_usage {
print_bold "Usage::"
print_info " ./bin/do <command> <arguments>"
echo
print_bold "Commands::"
print_info " help"
echo " - displays this usage"
print_info " generate"
echo " - automatically generates something based on input"
print_info " component <name>"
echo " - Creates a component skeleton"
echo " - <name> is in UpperCamelCase"
}
function run_command {
BASE_LOCATION=./src/components
COMMAND=$1
SUBJECT=$2
ARGUMENTS=$3
LOCATION=$BASE_LOCATION/$ARGUMENTS
if [[ $COMMAND == generate ]]; then
echo Generating a component called $ARGUMENTS in $LOCATION
ts-node ./bin/$COMMAND/index.ts $SUBJECT $ARGUMENTS
fi
}
function main {
if [[ $@ == help ]]; then
print_usage
exit 0
fi
if [[ "$#" -ge 3 ]]; then
run_command $@
else
echo
print_error "Invalid usage"
echo
print_usage
echo
exit 0
fi
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment