summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWilly Tarreau <w@1wt.eu>2021-11-18 17:42:50 +0100
committerWilly Tarreau <w@1wt.eu>2021-11-18 17:54:49 +0100
commitec347b1239202dafbccf89a06a39fb5986400e19 (patch)
tree7bb59d00ff1f675ed2c06b5d123f4d715317df9b
parent002e2068cc3429ba259ffc20b4ef77d774a065df (diff)
downloadhaproxy-ec347b1239202dafbccf89a06a39fb5986400e19.tar.gz
MINOR: config: support default values for environment variables
Sometimes it is really useful to be able to specify a default value for an optional environment variable, like the ${name-value} construct in shell. In fact we're really missing this for a number of settings in reg tests, starting with timeouts. This commit simply adds support for the common syntax above. Other common forms like '+' to replace existing variables, or ':-' and ':+' to act on empty variables, were not implemented at this stage, as they are less commonly needed.
-rw-r--r--doc/configuration.txt7
-rw-r--r--src/tools.c16
2 files changed, 20 insertions, 3 deletions
diff --git a/doc/configuration.txt b/doc/configuration.txt
index 4f306d73b..f597a8c60 100644
--- a/doc/configuration.txt
+++ b/doc/configuration.txt
@@ -727,13 +727,16 @@ shell. Variable names can contain alphanumerical characters or the character
underscore ("_") but should not start with a digit. If the variable contains a
list of several values separated by spaces, it can be expanded as individual
arguments by enclosing the variable with braces and appending the suffix '[*]'
-before the closing brace.
+before the closing brace. It is also possible to specify a default value to
+use when the variable is not set, by appending that value after a dash '-'
+next to the variable name. Note that the default value only replaces non
+existing variables, not empty ones.
Example:
bind "fd@${FD_APP1}"
- log "${LOCAL_SYSLOG}:514" local0 notice # send to local server
+ log "${LOCAL_SYSLOG-127.0.0.1}:514" local0 notice # send to local server
user "$HAPROXY_USER"
diff --git a/src/tools.c b/src/tools.c
index 8fc67163a..5d8483d4e 100644
--- a/src/tools.c
+++ b/src/tools.c
@@ -5440,13 +5440,27 @@ uint32_t parse_line(char *in, char *out, size_t *outlen, char **args, int *nbarg
}
if (brace) {
- if (*in != '}') {
+ if (*in == '-') {
+ /* default value starts just after the '-' */
+ if (!value)
+ value = in + 1;
+
+ while (*in && *in != '}')
+ in++;
+ if (!*in)
+ goto no_brace;
+ *in = 0; // terminate the default value
+ }
+ else if (*in != '}') {
+ no_brace:
/* unmatched brace */
err |= PARSE_ERR_BRACE;
if (errptr)
*errptr = brace;
goto leave;
}
+
+ /* brace found, skip it */
in++;
brace = NULL;
}