|
1 |
| -#!/bin/bash |
| 1 | +#!/usr/bin/env bash |
2 | 2 | #
|
3 | 3 | # Run Python code tools (isort, black, flake8)
|
4 | 4 | #
|
| 5 | +#### SETUP #################################################################### |
| 6 | + |
| 7 | +set -o errexit |
5 | 8 | set -o errtrace
|
6 | 9 | set -o nounset
|
7 | 10 |
|
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 | +} |
10 | 34 |
|
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 |
14 | 38 | 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 |
16 | 51 |
|
17 |
| -printf "\e[1m\e[7m %-80s\e[0m\n" 'isort' |
| 52 | +print_header 'isort' |
| 53 | +# shellcheck disable=SC2068 |
18 | 54 | docker compose exec app isort ${@:-.}
|
19 | 55 | echo
|
20 | 56 |
|
21 |
| -printf "\e[1m\e[7m %-80s\e[0m\n" 'black' |
| 57 | +print_header 'black' |
| 58 | +# shellcheck disable=SC2068 |
22 | 59 | docker compose exec app black ${@:-.}
|
23 | 60 | echo
|
24 | 61 |
|
25 |
| -printf "\e[1m\e[7m %-80s\e[0m\n" 'flake8' |
| 62 | +print_header 'flake8' |
| 63 | +# shellcheck disable=SC2068 |
26 | 64 | docker compose exec app flake8 ${@:-.}
|
27 | 65 | echo
|
0 commit comments