]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: config: add predicate "defined()" to conditional expression blocks
authorWilly Tarreau <w@1wt.eu>
Thu, 6 May 2021 13:55:14 +0000 (15:55 +0200)
committerWilly Tarreau <w@1wt.eu>
Thu, 6 May 2021 15:02:36 +0000 (17:02 +0200)
"defined(name)" will return true if <name> is a defined environment variable
otherwise false, regardless of its contents.

doc/configuration.txt
src/cfgparse.c

index a6af2f267df9c4a54ab82436bb7e91b8bf0b45ef..ff90526d868fcd653c7b3d050c7c4ab20d0a6696 100644 (file)
@@ -807,9 +807,20 @@ The conditions are currently limited to:
   - an empty string, always returns "false"
   - the integer zero ('0'), always returns "false"
   - a non-nul integer (e.g. '1'), always returns "true".
+  - a predicate optionally followed by argument(s) in parenthesis.
 
-Other patterns are not supported yet but the purpose is to bring a few
-functions to test for certain build options and supported features.
+The list of currently supported predicates is the following:
+
+  - defined(<name>)       : returns true if an environment variable <name>
+                            exists, regardless of its contents
+
+Example:
+
+   .if defined(HAPROXY_MWORKER)
+       listen mwcli_px
+          bind :1111
+          ...
+   .endif
 
 Three other directives are provided to report some status:
 
index c7a746fe63434480420397f6a07cccf0a5f0a086..d8ba76a8b460c442e1ff3f92959567d951292084 100644 (file)
@@ -136,6 +136,7 @@ enum nested_cond_state {
 /* supported conditional predicates for .if/.elif */
 enum cond_predicate {
        CFG_PRED_NONE,            // none
+       CFG_PRED_DEFINED,         // "defined"
 };
 
 struct cond_pred_kw {
@@ -146,6 +147,7 @@ struct cond_pred_kw {
 
 /* supported condition predicates */
 const struct cond_pred_kw cond_predicates[] = {
+       { "defined",          CFG_PRED_DEFINED,         ARG1(1, STR)         },
        { NULL, CFG_PRED_NONE, 0 }
 };
 
@@ -1712,6 +1714,10 @@ static int cfg_eval_condition(char **args, char **err, const char **errptr)
                 * arguments, placed in <argp> (which we'll need to free).
                 */
                switch (cond_pred->prd) {
+               case CFG_PRED_DEFINED:  // checks if arg exists as an environment variable
+                       ret = getenv(argp[0].data.str.area) != NULL;
+                       goto done;
+
                default:
                        memprintf(err, "internal error: unhandled conditional expression predicate '%s'", cond_pred->word);
                        if (errptr)