Last active
October 12, 2022 00:22
-
-
Save mattseabrook/ed793dd7f8950556997d22e01dfd71a6 to your computer and use it in GitHub Desktop.
Bash/Shell boilerplate I created to manage any Linux-based project in a Git Repo that has a corresponding Dockerfile. Command-line interface, with a couple variables exposed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
: ' | |
// boilerplate.sh | |
v.0.1 - 2022-10-11 - Matt Seabrook | |
See README.md or CTRL + F "Main Entrypoint" in this file | |
' | |
APP_PORT=4000 # Port to run the app on | |
DOCKER_IMAGE="my_app_img" # Name of the docker image | |
DOCKER_CONTAINER="my_app" # Name of the docker container | |
: ' | |
/* | |
================ | |
cli_interface | |
Process CLI arguments | |
@param $@ string(s) All of the ARGVs provided via stdin, $2, $3, etc. | |
================ | |
*/ | |
' | |
function cli_interface { | |
case $1 in | |
# | |
# Attach to the Docker Container | |
# | |
-a|-A|--a|--A|-attach|--attach) | |
# Fix this later, to check if the Container even exists | |
if [ $(docker inspect -f '{{.State.Running}}' $DOCKER_CONTAINER) = "true" ]; then | |
docker attach $DOCKER_CONTAINER | |
else | |
echo Container $DOCKER_CONTAINER is not running. Use: docker start $DOCKER_CONTAINER | |
fi | |
;; | |
# | |
# Build the Docker Image | |
# | |
-b|-B|--b|--B|-build|--build) | |
docker build -t $DOCKER_IMAGE . | |
;; | |
# | |
# Create & Run the Docker Container | |
# | |
-c|-C|--c|--C|-create|--create) | |
docker run \ | |
-it \ | |
-p 127.0.0.1:$APP_PORT:$APP_PORT \ | |
--name $DOCKER_CONTAINER \ | |
$DOCKER_IMAGE \ | |
bash | |
;; | |
# | |
# Help | |
# | |
-h|-H|--h|--H|-help|--help) | |
cli_help | |
;; | |
# No arguments | |
"") | |
cli_help | |
;; | |
*) | |
echo "ERROR: Invalid command line arguments/parameters: $@" | |
echo | |
echo "Usage: ./boilerplate.sh {switch} {parameter}" | |
echo | |
cli_help | |
;; | |
esac | |
} | |
: ' | |
/* | |
================ | |
cli_help | |
Print help related text on the screen like a standard CLI application | |
================ | |
*/ | |
' | |
function cli_help { | |
echo | |
echo -e "Usage: ./boilerplate.sh\t{switch}\tOptions..." | |
echo | |
echo -e "\t-a, -A, --a, --A, -attach, --attach\tAttach to this project's Docker Container." | |
echo -e "\t-b, -B, --b, --B, -build, --build\t\Build Docker Image as specified in the current folder (Dockerfile)." | |
echo -e "\t-c, -C, --c, --C, -create, --create\tCreate & Run the Docker Container in terminal." | |
echo -e "\t-h, -H, --h, --H, -help, --help\tDisplay this help text." | |
echo | |
echo "Please see README.md for more specific information" | |
echo | |
} | |
: ' | |
//========================================================================== | |
/* | |
================ | |
Main Entrypoint | |
Upon execution of the shell script | |
================ | |
*/ | |
' | |
cli_interface "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment