Skip to content

Commit 5cc14f3

Browse files
committed
better align script with best and local practices
1 parent f8285e4 commit 5cc14f3

File tree

1 file changed

+48
-10
lines changed

1 file changed

+48
-10
lines changed

bin/tools.sh

+48-10
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,65 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22
#
33
# Run Python code tools (isort, black, flake8)
44
#
5+
#### SETUP ####################################################################
6+
7+
set -o errexit
58
set -o errtrace
69
set -o nounset
710

8-
# Change directory to cc-legal-tools-app (grandparent directory of this script)
9-
cd ${0%/*}/../
11+
# shellcheck disable=SC2154
12+
trap '_es=${?};
13+
printf "${0}: line ${LINENO}: \"${BASH_COMMAND}\"";
14+
printf " exited with a status of ${_es}\n";
15+
exit ${_es}' ERR
16+
17+
DIR_REPO="$(cd -P -- "${0%/*}/.." && pwd -P)"
18+
# https://en.wikipedia.org/wiki/ANSI_escape_code
19+
E0="$(printf "\e[0m")" # reset
20+
E30="$(printf "\e[30m")" # black foreground
21+
E31="$(printf "\e[31m")" # red foreground
22+
E107="$(printf "\e[107m")" # bright white background
23+
24+
#### FUNCTIONS ################################################################
25+
26+
check_docker() {
27+
local _msg
28+
if ! docker compose exec app true 2>/dev/null; then
29+
_msg='The app container/services is not avaialable.'
30+
_msg="${_msg}\n First run \`docker compose up\`."
31+
error_exit "${_msg}"
32+
fi
33+
}
1034

11-
if ! docker compose exec app true 2>/dev/null; then
12-
echo 'The app container/services is not avaialable.' 1>&2
13-
echo 'First run `docker compose up`.' 1>&2
35+
error_exit() {
36+
# Echo error message and exit with error
37+
echo -e "${E31}ERROR:${E0} ${*}" 1>&2
1438
exit 1
15-
fi
39+
}
40+
41+
print_header() {
42+
# Print 80 character wide black on white heading with time
43+
printf "${E30}${E107}# %-69s$(date '+%T') ${E0}\n" "${@}"
44+
}
45+
46+
#### MAIN #####################################################################
47+
48+
cd "${DIR_REPO}"
49+
50+
check_docker
1651

17-
printf "\e[1m\e[7m %-80s\e[0m\n" 'isort'
52+
print_header 'isort'
53+
# shellcheck disable=SC2068
1854
docker compose exec app isort ${@:-.}
1955
echo
2056

21-
printf "\e[1m\e[7m %-80s\e[0m\n" 'black'
57+
print_header 'black'
58+
# shellcheck disable=SC2068
2259
docker compose exec app black ${@:-.}
2360
echo
2461

25-
printf "\e[1m\e[7m %-80s\e[0m\n" 'flake8'
62+
print_header 'flake8'
63+
# shellcheck disable=SC2068
2664
docker compose exec app flake8 ${@:-.}
2765
echo

0 commit comments

Comments
 (0)