Skip to content

Commit

Permalink
fixup example.sh like 2.3.0 main.sh
Browse files Browse the repository at this point in the history
__i_am_main_script logic fixed for example.sh
added double-sourced test
  • Loading branch information
mstreuhofer committed Feb 10, 2017
1 parent 382217e commit d879ae1
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 13 deletions.
29 changes: 23 additions & 6 deletions example.sh
Expand Up @@ -7,7 +7,7 @@
#
# LOG_LEVEL=7 ./example.sh -f /tmp/x -d (change this for your script)
#
# Based on a template by BASH3 Boilerplate v2.2.0
# Based on a template by BASH3 Boilerplate v2.3.0
# http://bash3boilerplate.sh/#authors
#
# The MIT License (MIT)
Expand Down Expand Up @@ -47,13 +47,34 @@ EOF
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/main.sh"


### Signal trapping and backtracing
##############################################################################

function __b3bp_cleanup_before_exit () {
info "Cleaning up. Done"
}
trap __b3bp_cleanup_before_exit EXIT

# requires `set -o errtrace`
__b3bp_err_report() {
local error_code
error_code=${?}
error "Error in ${__file} in function ${1} on line ${2}"
exit ${error_code}
}
# Uncomment the following line for always providing an error backtrace
# trap '__b3bp_err_report "${FUNCNAME:-.}" ${LINENO}' ERR


### Command-line argument switches (like -d for debugmode, -h for showing helppage)
##############################################################################

# debug mode
if [[ "${arg_d:?}" = "1" ]]; then
set -o xtrace
LOG_LEVEL="7"
# Enable error backtracing
trap '__b3bp_err_report "${FUNCNAME:-.}" ${LINENO}' ERR
fi

# verbose mode
Expand Down Expand Up @@ -83,11 +104,7 @@ fi
### Runtime
##############################################################################

function cleanup_before_exit () {
info "Cleaning up. Done"
}
trap cleanup_before_exit EXIT

info "__i_am_main_script: ${__i_am_main_script}"
info "__file: ${__file}"
info "__dir: ${__dir}"
info "__base: ${__base}"
Expand Down
16 changes: 11 additions & 5 deletions main.sh
Expand Up @@ -27,11 +27,18 @@ set -o pipefail
# set -o xtrace

if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
__i_am_main_script="0" # false

if [[ "${__usage+x}" ]]; then
if [[ "${BASH_SOURCE[1]}" = "${0}" ]]; then
__i_am_main_script="1" # true
fi

__b3bp_external_usage="true"
__b3bp_tmp_source_idx=1
fi
else
__i_am_main_script="1" # true
[[ "${__usage+x}" ]] && unset -v __usage
[[ "${__helptext+x}" ]] && unset -v __helptext
fi
Expand All @@ -40,11 +47,7 @@ fi
__dir="$(cd "$(dirname "${BASH_SOURCE[${__b3bp_tmp_source_idx:-0}]}")" && pwd)"
__file="${__dir}/$(basename "${BASH_SOURCE[${__b3bp_tmp_source_idx:-0}]}")"
__base="$(basename "${__file}" .sh)"
if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
__i_am_main_script="0" # false
else
__i_am_main_script="1" # true
fi


# Define the environment variables (and their defaults) that this script depends on
LOG_LEVEL="${LOG_LEVEL:-6}" # 7 = debug -> 0 = emergency
Expand Down Expand Up @@ -308,6 +311,7 @@ if [[ "${__b3bp_external_usage:-}" = "true" ]]; then
return
fi


### Signal trapping and backtracing
##############################################################################

Expand All @@ -326,6 +330,7 @@ __b3bp_err_report() {
# Uncomment the following line for always providing an error backtrace
# trap '__b3bp_err_report "${FUNCNAME:-.}" ${LINENO}' ERR


### Command-line argument switches (like -d for debugmode, -h for showing helppage)
##############################################################################

Expand Down Expand Up @@ -364,6 +369,7 @@ fi
### Runtime
##############################################################################

info "__i_am_main_script: ${__i_am_main_script}"
info "__file: ${__file}"
info "__dir: ${__dir}"
info "__base: ${__base}"
Expand Down
1 change: 1 addition & 0 deletions test/fixture/double-source.exitcode
@@ -0,0 +1 @@
1
23 changes: 23 additions & 0 deletions test/fixture/double-source.stdio
@@ -0,0 +1,23 @@
ACCPTST:STDIO_REPLACE_DATETIMES
{datetime} UTC [ debug] cli arg arg_f = () -> {tmpdir}/x
{datetime} UTC [ info] __i_am_main_script: 0
{datetime} UTC [ info] __file: {root}/example.sh
{datetime} UTC [ info] __dir: {root}
{datetime} UTC [ info] __base: example
{datetime} UTC [ info] OSTYPE: {OSTYPE}
{datetime} UTC [ info] arg_f: {tmpdir}/x
{datetime} UTC [ info] arg_d: 0
{datetime} UTC [ info] arg_v: 0
{datetime} UTC [ info] arg_h: 0
{datetime} UTC [ info] multiple lines example - line #1
{datetime} UTC [ info] multiple lines example - line #2
{datetime} UTC [ info] imagine logging the output of 'ls -al /path/'
{datetime} UTC [ debug] Info useful to developers for debugging the application, not useful during operations.
{datetime} UTC [ info] Normal operational messages - may be harvested for reporting, measuring throughput, etc. - no action required.
{datetime} UTC [ notice] Events that are unusual but not error conditions - might be summarized in an email to developers or admins to spot potential problems - no immediate action required.
{datetime} UTC [ warning] Warning messages, not an error, but indication that an error will occur if action is not taken, e.g. file system 85% full - each item must be resolved within a given time. This is a debug message
{datetime} UTC [ error] Non-urgent failures, these should be relayed to developers or admins; each item must be resolved within a given time.
{datetime} UTC [ critical] Should be corrected immediately, but indicates failure in a primary system, an example is a loss of a backup ISP connection.
{datetime} UTC [ alert] Should be corrected immediately, therefore notify staff who can fix the problem. An example would be the loss of a primary ISP connection.
{datetime} UTC [emergency] A "panic" condition usually affecting multiple apps/servers/sites. At this level it would usually notify all tech staff on call.
{datetime} UTC [ info] Cleaning up. Done
1 change: 1 addition & 0 deletions test/fixture/main-debug.stdio
@@ -1,5 +1,6 @@
ACCPTST:STDIO_REPLACE_DATETIMES
{datetime} UTC [ debug] cli arg arg_f = () -> {tmpdir}/x
{datetime} UTC [ info] __i_am_main_script: 1
{datetime} UTC [ info] __file: {root}/main.sh
{datetime} UTC [ info] __dir: {root}
{datetime} UTC [ info] __base: main
Expand Down
1 change: 1 addition & 0 deletions test/fixture/main-nocolor.stdio
@@ -1,5 +1,6 @@
ACCPTST:STDIO_REPLACE_DATETIMES
{datetime} UTC [ debug] cli arg arg_f = () -> {tmpdir}/x
{datetime} UTC [ info] __i_am_main_script: 1
{datetime} UTC [ info] __file: {root}/main.sh
{datetime} UTC [ info] __dir: {root}
{datetime} UTC [ info] __base: main
Expand Down
15 changes: 15 additions & 0 deletions test/scenario/double-source/run.sh
@@ -0,0 +1,15 @@
#!/usr/bin/env bash
set -o pipefail
set -o errexit
set -o nounset
# set -o xtrace

__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
__root="$(cd "$(dirname "$(dirname "$(dirname "${__dir}")")")" && pwd)"

export LOG_LEVEL=7

echo "ACCPTST:STDIO_REPLACE_DATETIMES"

# shellcheck source=example.sh
source "${__root}/example.sh" -f /tmp/x
2 changes: 1 addition & 1 deletion test/scenario/main-debug/run.sh
Expand Up @@ -9,4 +9,4 @@ __root="$(cd "$(dirname "$(dirname "$(dirname "${__dir}")")")" && pwd)"

echo "ACCPTST:STDIO_REPLACE_DATETIMES"

env LOG_LEVEL=8 bash "${__root}/main.sh" -f /tmp/x
env LOG_LEVEL=7 bash "${__root}/main.sh" -f /tmp/x
2 changes: 1 addition & 1 deletion test/scenario/main-nocolor/run.sh
Expand Up @@ -9,4 +9,4 @@ __root="$(cd "$(dirname "$(dirname "$(dirname "${__dir}")")")" && pwd)"

echo "ACCPTST:STDIO_REPLACE_DATETIMES"

env LOG_LEVEL=8 NO_COLOR=true bash "${__root}/main.sh" -f /tmp/x
env LOG_LEVEL=7 NO_COLOR=true bash "${__root}/main.sh" -f /tmp/x

2 comments on commit d879ae1

@zbeekman
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mstreuhofer I'm fixing some more stuff as well... if you like I can push my branch to kvz/bash3boilerplate:fix-lint so that we can collaborate better... or I can rebase ontop of your changes then pull everything in with one PR... just let me know when you're done and what you would like to do.

@mstreuhofer
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm done in this PR.
if everybody agrees to the changes you can rebase your branch on mine and just merge yours after. this PR could then be closed.

Please sign in to comment.