Skip to content

Commit e31c8ec

Browse files
committed
refactor(setup): Consolidate injection of auth arg in curl cmds
1 parent 4d3afd2 commit e31c8ec

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

setup/lib.sh

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,22 @@ function suberr {
2020
echo "$1" >&2
2121
}
2222

23+
# Inject common arguments to curl commands based on the environment.
24+
function augment_curl_args {
25+
local args_var_name=$1
26+
local -n args_ref="${args_var_name}"
27+
if [[ -n "${ELASTIC_PASSWORD:-}" ]]; then
28+
args_ref+=( '-u' "elastic:${ELASTIC_PASSWORD}" )
29+
fi
30+
}
31+
2332
# Poll the 'elasticsearch' service until it responds with HTTP code 200.
2433
function wait_for_elasticsearch {
2534
local elasticsearch_host="${ELASTICSEARCH_HOST:-elasticsearch}"
2635

2736
local -a args=( '-s' '-D-' '-m15' '-w' '%{http_code}' "http://${elasticsearch_host}:9200/" )
2837

29-
if [[ -n "${ELASTIC_PASSWORD:-}" ]]; then
30-
args+=( '-u' "elastic:${ELASTIC_PASSWORD}" )
31-
fi
38+
augment_curl_args args
3239

3340
local -i result=1
3441
local output
@@ -63,9 +70,7 @@ function wait_for_builtin_users {
6370

6471
local -a args=( '-s' '-D-' '-m15' "http://${elasticsearch_host}:9200/_security/user?pretty" )
6572

66-
if [[ -n "${ELASTIC_PASSWORD:-}" ]]; then
67-
args+=( '-u' "elastic:${ELASTIC_PASSWORD}" )
68-
fi
73+
augment_curl_args args
6974

7075
local -i result=1
7176

@@ -114,9 +119,7 @@ function check_user_exists {
114119
"http://${elasticsearch_host}:9200/_security/user/${username}"
115120
)
116121

117-
if [[ -n "${ELASTIC_PASSWORD:-}" ]]; then
118-
args+=( '-u' "elastic:${ELASTIC_PASSWORD}" )
119-
fi
122+
augment_curl_args args
120123

121124
local -i result=1
122125
local -i exists=0
@@ -153,9 +156,7 @@ function set_user_password {
153156
'-d' "{\"password\" : \"${password}\"}"
154157
)
155158

156-
if [[ -n "${ELASTIC_PASSWORD:-}" ]]; then
157-
args+=( '-u' "elastic:${ELASTIC_PASSWORD}" )
158-
fi
159+
augment_curl_args args
159160

160161
local -i result=1
161162
local output
@@ -187,9 +188,7 @@ function create_user {
187188
'-d' "{\"password\":\"${password}\",\"roles\":[\"${role}\"]}"
188189
)
189190

190-
if [[ -n "${ELASTIC_PASSWORD:-}" ]]; then
191-
args+=( '-u' "elastic:${ELASTIC_PASSWORD}" )
192-
fi
191+
augment_curl_args args
193192

194193
local -i result=1
195194
local output
@@ -220,9 +219,7 @@ function ensure_role {
220219
'-d' "$body"
221220
)
222221

223-
if [[ -n "${ELASTIC_PASSWORD:-}" ]]; then
224-
args+=( '-u' "elastic:${ELASTIC_PASSWORD}" )
225-
fi
222+
augment_curl_args args
226223

227224
local -i result=1
228225
local output

0 commit comments

Comments
 (0)