changeset 1116:40228511d600

chef: Add new `chef/env` config section.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 25 Feb 2018 21:48:55 -0800
parents 11b9d0c8bd62
children e8511fed42a3
files docs/config.yml docs/docs/99_reference/01_website-configuration.md piecrust/main.py
diffstat 3 files changed, 48 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/docs/config.yml	Sat Feb 24 21:13:54 2018 -0800
+++ b/docs/config.yml	Sun Feb 25 21:48:55 2018 -0800
@@ -24,6 +24,10 @@
             source: api
             func: apiurl
 
+chef:
+    env:
+        PATH: node_modules/.bin
+
 baker:
     assets_dirs:
         assets:
--- a/docs/docs/99_reference/01_website-configuration.md	Sat Feb 24 21:13:54 2018 -0800
+++ b/docs/docs/99_reference/01_website-configuration.md	Sun Feb 25 21:48:55 2018 -0800
@@ -136,6 +136,26 @@
 [tplengref]: {{docurl('reference/template-engines')}}
 
 
+## Chef Command Line
+
+The following settings are unde the `chef` section, and all valid for _all_ chef
+commands.
+
+* `env`: A mapping of environment variables and values to set before running the
+  current command. By default, variables are set to the given value. If a `+` is
+  added to the variable name, the value will be _appended_ instead. The `PATH`
+  variable's value is _always_ appended with either `:` or `;` depending on the
+  OS.
+    
+    For example:
+
+        chef:
+          env:
+            PATH: node_modules/.bin
+            SOME_VAR: value
+            "OTHER_VAR+": ",append this"
+
+
 ## Preparation
 
 The following settings are under the `prepare` section, and are used by the
--- a/piecrust/main.py	Sat Feb 24 21:13:54 2018 -0800
+++ b/piecrust/main.py	Sun Feb 25 21:48:55 2018 -0800
@@ -246,6 +246,25 @@
     return cache_key
 
 
+def _setup_app_environment(env):
+    for k, v in env.items():
+        varname = k
+        append = False
+        if k.lower() == 'path':
+            append = True
+            v = os.pathsep + v
+        elif k.endswith('+'):
+            varname = k[:-1]
+            append = True
+
+        if append:
+            logger.debug("Env: $%s += %s" % (varname, v))
+            os.environ[varname] += v
+        else:
+            logger.debug("Env: $%s = %s" % (varname, v))
+            os.environ[varname] = v
+
+
 def _run_chef(pre_args, argv):
     # Setup the app.
     root = None
@@ -315,6 +334,11 @@
         parser.print_help()
         return 0
 
+    # Do any custom setup the user wants.
+    custom_env = app.config.get('chef/env')
+    if custom_env:
+        _setup_app_environment(custom_env)
+
     # Add some timing information.
     if app.env:
         app.env.stats.registerTimer('ChefStartup')