Last active
September 29, 2022 00:51
-
-
Save cjlyth/57a3457b010fafacb03c to your computer and use it in GitHub Desktop.
Shell script to start my docker containers while im developing
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
#!/usr/bin/env bash | |
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | |
[[ -f "$DIR/colors.tput.sh" ]] && { | |
source "$DIR/colors.tput.sh" | |
} || { | |
echo "Unable to find file: $DIR/colors.tput.sh" | |
exit 1 | |
} | |
reset="$(color)" | |
border_fmt="$(BG=1 color 'black';color 'dark_blue')%s${reset}" | |
printf -v border_sep "$border_fmt" "|" | |
function applyStyle () | |
{ | |
local NAME="${1?}" | |
local COLOR="$(color)" | |
printf "${reset}${border_sep}%b${reset}${border_sep}$(color)%20s" "$(STANDOUT=1 color)>" "${NAME}" | |
for s in "${@:2}" ; do | |
printf "${reset}$(eval 'local ${s^^}=1; color')%*s ${reset}" $((1+${#s})) "${s}" | |
done | |
echo | |
} | |
printf "$border_fmt" "$(color_line '~')" | |
for c in "${!COLOR_KEYS[@]}" ; do | |
applyStyle $c "HIDE_CURSOR" "REVERSE" "UNDERLINE" "NO_UNDERLINE" "BG" "BOLD" "boost" "dim" "STANDOUT" "NO_STANDOUT" "NO_HIDE_CURSOR" | |
done | |
printf "$border_fmt" "$(color_line '~')" | |
# echo -e "$(color blue)$(cat <<EOF | |
# $(color blue) | |
# hi | |
# $reset | |
# hi | |
# EOF | |
# )" |
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
declare -xA COLOR_KEYS=( [BLACK]=0 [RED]=1 [GREEN]=2 \ | |
[YELLOW]=3 [BLUE]=4 [MAGENTA]=5 [CYAN]=6 [WHITE]=7 \ | |
[LIGHT_YELLOW]=186 [DARK_BLUE]=17) | |
function printColors() | |
{ | |
( x=`tput op` y=`printf %$((${COLUMNS}-6))s`;for i in {0..256};do o=00$i;echo -e ${o:${#o}-3:3} `tput setaf $i;tput setab $i`${y// /=}$x;done; ) | |
} | |
function color_line() | |
{ | |
local line="$(tput cols | xargs printf '%*s')" | |
echo -n "${line// /${1:--}}" | |
} | |
function color_value() | |
{ | |
echo -n "${COLOR_KEYS[${1:-$color_name}]}" | |
} | |
function color_reset() | |
{ | |
tput -S <<-! | |
sgr0 | |
cnorm | |
rmul | |
rmso | |
! | |
} | |
function color() | |
{ | |
local color_name="${1:-${NAME}}" | |
[[ -z "$color_name" ]] && { | |
color_reset | |
return "$?" | |
} | |
local color_value="$(color_value ${color_name^^})" | |
[[ -z "$color_value" ]] && { | |
return "$?" | |
} | |
[[ -v "BOOST" && -n "$BOOST" ]] && (( color_value += 8 )) | |
[[ -z "$FG$BG" ]] && local FG=1 | |
tput -S <<-! | |
${HIDE_CURSOR:+civis} | |
${NO_HIDE_CURSOR:+cnorm} | |
${BG:+setab $color_value} | |
${FG:+setaf $color_value} | |
${BOLD:+bold} | |
${STANDOUT:+smso} | |
${NO_STANDOUT:+rmso} | |
${DIM:+dim} | |
${UNDERLINE:+smul} | |
${NO_UNDERLINE:+rmul} | |
${REVERSE:+rev} | |
! | |
} | |
export -f color color_value printColors color_reset |
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
#!/usr/bin/env bash | |
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | |
[[ -f "$DIR/colors.tput.sh" ]] && { | |
source "$DIR/colors.tput.sh" | |
} || { | |
echo "Unable to find file: $DIR/colors.tput.sh" | |
exit 1 | |
} | |
reset=$(color_reset) | |
red=$(color red) | |
blue=$(color blue) | |
green=$(color green) | |
cyan=$(BOOST=1;color cyan) | |
yellow=$(color yellow) | |
light_yellow=$(color LIGHT_YELLOW) | |
border_fmt="${reset}$(BG=1 color 'black';color 'dark_blue')%s${reset}" | |
function print-msg() | |
{ | |
printf "${reset}${1} %20s${4:+:}${reset} ${3}%s ${reset}\n" "${2}" "${4}" | |
} | |
function start-line() | |
{ | |
printf "$border_fmt" "|" "${1:- }" | |
} | |
function print-err() | |
{ | |
start-line "$(BG=1 color red;BOLD=1 color yellow)>" | |
print-msg "${yellow}" "${1}" "${light_yellow}" "${2}" | |
} | |
function print-val() | |
{ | |
start-line "${3:-$reset }" | |
print-msg "${4:-$cyan}" "${1}" "${5:-$blue}" "${2}" | |
} | |
function print-line() | |
{ | |
printf "$border_fmt\n" "$(color_line ${1:-'~'})" | |
} | |
function remove-containers () | |
{ | |
docker ps -qa | xargs --no-run-if-empty docker rm | |
# (docker ps -qa | xargs --no-run-if-empty docker rm) \ | |
# 2> >(sed "s/^/${red}# ${reset}Error: /" -) \ | |
# 1> >(sed "s/^/${green}# ${reset}Deleted: /" -) | |
} | |
function remove-images () | |
{ | |
(docker images -qa | xargs --no-run-if-empty docker rmi) \ | |
2> >(sed "s/^/$(print-err 'remove-images')${reset}/" -) \ | |
1> >(sed "s/^/$(print-val 'remove-images')${reset}/" -) | |
} | |
function do_exit () | |
{ | |
print-line '#' | |
# echo -e "${@}" | |
exit $1 | |
} | |
# tput reset | |
tput clear | |
print-line '#' | |
repo_src="$(cd ${1:-.} && pwd)" | |
[[ -f "$repo_src/Dockerfile" ]] || { | |
print-err "repo_src missing" "Dockerfile was not found at the expected location: ${red}$repo_src/Dockerfile${reset}" | |
do_exit 22 | |
} | |
repo_src_color="$(color LIGHT_YELLOW)${repo_src}${reset}" | |
[[ -s "$repo_src/Dockerfile" ]] || { | |
print-err "repo_src empty" "Unable to find a non empty $(color yellow)Dockerfile${reset} in $repo_src_color " | |
do_exit 2 | |
} | |
# Setup vars | |
docker_login="$(docker info 2>/dev/null | grep -Ei '^username: ' | awk '{print $2}')" | |
repo_parent="$(dirname $repo_src | xargs basename)" | |
docker_username="${docker_username:-${docker_login:-$repo_parent}}" | |
docker_username="${docker_username:-$(whoami)}" | |
login="$docker_username" | |
login_color="$(color LIGHT_YELLOW)${login}${reset}" | |
repo_name="${repo_src##*/}" | |
repo_name_color="$(color LIGHT_YELLOW)${repo_name}${reset}" | |
build_tag="$login/$repo_name" | |
build_tag_color="$login_color/$repo_name_color" | |
print-val "login" "$login_color" | |
print-val "repo_name" "$repo_name_color" | |
print-val "repo_src" "$repo_src_color" | |
success="0" | |
print-val "Building image" " " "$(BG=1 color cyan;BOLD=1 color black)v" | |
docker build -t "$build_tag" "$repo_src" \ | |
3> >(sed "s/^/$(print-val 'info')${reset}/" -) \ | |
2> >(sed "s/^/$(print-err 'error')${reset}/" -) \ | |
1> >(sed "s/^/$(print-val 'ok')${reset}/" -) | |
success="$?" | |
[[ "${success}" -eq "0" ]] || { | |
print-err "${red}Build failed${reset} for image" "${build_tag_color} with exit code ${red}${success}${reset}" | |
print-err "Check Dockerfile in directory" "${repo_src_color}" | |
docker rmi ${build_tag} &> /dev/null | |
do_exit 2 | |
} | |
print-val "Build finished for image" "${build_tag}" "$(BG=1 color green;BOLD=1 color black)$" | |
print-line | |
print-val "Running a non persistent interactive container" "" "$(BG=1 color cyan;BOLD=1 color black)v$reset" | |
vol_args="" | |
container_dir="/tmp/${build_tag}" | |
[[ -d "$container_dir" ]] && { | |
vol_args="-v $container_dir/data:/data" | |
print-val "Mounting $(color magenta)/data${reset} in local dir" "$container_dir" | |
} || { | |
print-val "create directory" "$container_dir to have it mounted automatically" "$(BG=1 color cyan;BOLD=1 color black) $reset" | |
} | |
docker run --rm -it ${vol_args} -P "${build_tag}" | |
print-line | |
print-val "Docker build $success" "${green}complete${reset}" "$(BG=1 color green;BOLD=1 color black)$" | |
do_exit 0 | |
# ${reset} | |
# remove-containers | |
# remove-images | |
# print-err "General" "..." | |
# print-val "docker" "build" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The previous version is not dependent on the color scripts, I think i like that version more