Skip to content

Commit 2e80a61

Browse files
authored
Merge pull request #18 from cyberark/conjurinc-ops-423
Use logging functions in helpers lib
2 parents 24f1b27 + cc1390b commit 2e80a61

File tree

7 files changed

+57
-22
lines changed

7 files changed

+57
-22
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ files within it's directory.
129129
<li><b>bl_remote_sha_for_ref</b>: Returns the SHA for a given ref from a named remote.</li>
130130
<li><b>bl_remote_tag_for_sha</b>: Returns the tag corresponding to a SHA from a named remote - if there is one.</li>
131131
<li><b>bl_tracked_files_excluding_subtrees</b>: List files tracked by git, but excluding any files that are in paths listed in <code>.gittrees</code>.</li>
132+
<li><b>bl_gittrees_present</b>: Succeeds if .gittrees is present in the root of the repo, otherwise fails.</li>
132133
<li><b>bl_cat_gittrees</b>: Returns the contents of .gittrees from the top level of the repo, excluding any comments. Fails if .gittrees is not present.</li>
133134
</ol>
134135
</td>

git/lib

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,25 @@ function bl_remote_tag_for_sha(){
6464

6565
## Minimal git subtree functionality required for tests to pass
6666
# full subtree functionality is not ready for merge.
67+
function bl_gittrees_present(){
68+
local -r git_trees="$(bl_repo_root)/.gittrees"
69+
[[ -e "${git_trees}" ]]
70+
}
71+
6772
function bl_cat_gittrees(){
6873
local -r git_trees="$(bl_repo_root)/.gittrees"
6974
local -r subtrees_file_format=".gittrees should contain one subtree per line,\
7075
space seperated with three fields: subtree_path renmote_url remote_name"
71-
[[ -e "${git_trees}" ]] || bl_die ".gittrees file ${git_trees} not found. ${subtrees_file_format}"
72-
grep -E -v '^\s*$|^\s*#' "$(bl_repo_root)/.gittrees"
76+
bl_gittrees_present || bl_die ".gittrees file ${git_trees} not found. ${subtrees_file_format}"
77+
grep -E -v '^\s*$|^\s*#' "$(bl_repo_root)/.gittrees" || true
7378
}
7479

7580
function bl_tracked_files_excluding_subtrees(){
76-
subtrees="$(bl_cat_gittrees | awk '{print $1}' | paste -sd '|' -)"
77-
bl_all_files_in_repo | grep -E -v "${subtrees}"
81+
if bl_gittrees_present; then
82+
subtrees="$(bl_cat_gittrees | awk '{print $1}' | paste -sd '|' -)"
83+
bl_all_files_in_repo | grep -E -v "${subtrees}"
84+
else
85+
bl_log debug ".gittrees not found subtress not excluded" stderr
86+
bl_all_files_in_repo
87+
fi
7888
}

helpers/lib

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
: "${BASH_LIB_DIR:?BASH_LIB_DIR must be set. Please source bash-lib/init before other scripts from bash-lib.}"
44

55
function bl_die(){
6-
echo "${@}"
6+
bl_fatal "${@}"
77
exit 1
88
}
99

1010
#safe pushd
1111
function bl_spushd(){
1212
if ! pushd "${1}" >/dev/null; then
13-
die "pushd ${1} failed :("
13+
bl_die "pushd ${1} failed :("
1414
fi
1515
}
1616

@@ -38,16 +38,14 @@ function bl_retry {
3838
local -r MAX_BACKOFF=30
3939

4040
if [[ ${#} -lt 2 ]]; then
41-
echo "retry usage: retry <retries> <command>"
42-
exit 1
41+
bl_die "retry usage: retry <retries> <command>"
4342
fi
4443

4544
local retries=$1
4645
shift
4746

4847
if ! bl_is_num "${retries}"; then
49-
echo "Invalid number of retries: ${retries} for command '${*}'".
50-
exit 1
48+
bl_die "Invalid number of retries: ${retries} for command '${*}'".
5149
fi
5250

5351
local count=0
@@ -67,11 +65,11 @@ function bl_retry {
6765
# Add a random amount to the delay to prevent competing processes
6866
# from re-colliding.
6967
wait=$(( backoff + (RANDOM % count) ))
70-
echo "'${*}' Retry $count/$retries exited $exit, retrying in $wait seconds..."
68+
bl_info "'${*}' Retry $count/$retries exited $exit, retrying in $wait seconds..."
7169
sleep $wait
7270
else
7371
# Out of retries :(
74-
echo "Retry $count/$retries exited $exit, no more retries left."
72+
bl_error "Retry $count/$retries exited $exit, no more retries left."
7573
return $exit
7674
fi
7775
done
@@ -81,21 +79,18 @@ function bl_retry {
8179
# retry function that waits a constant number of seconds between attempts.
8280
function bl_retry_constant {
8381
if [[ ${#} -lt 3 ]]; then
84-
echo "retry usage: retry <retries> <interval (seconds)> <command>"
85-
exit 1
82+
bl_die "retry usage: retry <retries> <interval (seconds)> <command>"
8683
fi
8784

8885
local retries=$1; shift
8986
local interval=$1; shift
9087

9188
if ! bl_is_num "${retries}"; then
92-
echo "Invalid number of retries: ${retries} for command '${*}'".
93-
exit 1
89+
bl_die "Invalid number of retries: ${retries} for command '${*}'"
9490
fi
9591

9692
if ! bl_is_num "${interval}"; then
97-
echo "Invalid interval in seconds: ${retries} for command '${*}'".
98-
exit 1
93+
bl_die "Invalid interval in seconds: ${retries} for command '${*}'".
9994
fi
10095

10196
local count=0
@@ -106,11 +101,11 @@ function bl_retry_constant {
106101
exit=$?
107102
count=$((count + 1))
108103
if [ "${count}" -lt "${retries}" ]; then
109-
echo "'${*}' Retry $count/$retries exited $exit, retrying in $interval seconds..."
104+
bl_info "'${*}' Retry $count/$retries exited $exit, retrying in $interval seconds..."
110105
sleep "${interval}"
111106
else
112107
# Out of retries :(
113-
echo "Retry $count/$retries exited $exit, no more retries left."
108+
bl_error "Retry $count/$retries exited $exit, no more retries left."
114109
return $exit
115110
fi
116111
done

logging/lib

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,21 @@ function bl_log {
3030
runtime_log_level="${BASH_LIB_LOG_LEVEL}"
3131
write_log_level="${1}"
3232
msg="${2}"
33+
out="${3:-stdout}"
3334

3435
bl_check_log_level "${runtime_log_level}"
3536
bl_check_log_level "${write_log_level}"
3637

37-
if (( BASH_LIB_LOG_LEVELS[write_log_level] >= BASH_LIB_LOG_LEVELS[runtime_log_level] )); then
38+
runtime_level_num="${BASH_LIB_LOG_LEVELS[${runtime_log_level}]}"
39+
write_level_num="${BASH_LIB_LOG_LEVELS[${write_log_level}]}"
40+
41+
if (( write_level_num < runtime_level_num )); then
42+
return
43+
fi
44+
45+
if [[ "${out}" == "stderr" ]]; then
46+
echo -e "\e[${BASH_LIB_LOG_COLOURS[${write_log_level}]}m${msg}\e[0m" 1>&2
47+
else
3848
echo -e "\e[${BASH_LIB_LOG_COLOURS[${write_log_level}]}m${msg}\e[0m"
3949
fi
4050
}

tests-for-this-repo/git.bats

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,19 @@ teardown(){
100100
assert_success
101101
}
102102

103+
@test "bl_gittrees_present succeeds when .gittrees file is present" {
104+
touch .gittrees
105+
run bl_gittrees_present
106+
assert_success
107+
assert_output ""
108+
}
109+
110+
@test "bl_gittrees_present fails when .gittrees file is not present" {
111+
run bl_gittrees_present
112+
assert_failure
113+
assert_output ""
114+
}
115+
103116
@test "bl_cat_gittrees dies when gittrees doesn't exist" {
104117
run bl_cat_gittrees
105118
assert_failure

tests-for-this-repo/helpers.bats

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ teardown(){
1717

1818
@test "bl_die exits and prints message" {
1919
run bash -c ". ${BASH_LIB_DIR}/init; bl_die msg"
20-
assert_output msg
20+
assert_output --partial msg
2121
assert_failure
2222
}
2323

tests-for-this-repo/logging.bats

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ teardown() {
3030
assert_output --partial test
3131
}
3232

33+
@test "bl_log outputs mesage when stderr is selected. Note: bats combines stdout and stderr" {
34+
run bl_log info test stderr
35+
assert_success
36+
assert_output --partial "test"
37+
}
38+
3339
@test "bl_debug doesn't output anything using the default info level" {
3440
run bl_debug foo
3541
assert_success

0 commit comments

Comments
 (0)