Skip to content
This repository was archived by the owner on May 9, 2020. It is now read-only.

Commit 95c57b8

Browse files
committed
support "#{ENV['FOO']}" expressions in knife.rb
1 parent bcf1a8b commit 95c57b8

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

chef/api.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class ChefAPI(object):
6767
"""
6868

6969
ruby_value_re = re.compile(r'#\{([^}]+)\}')
70+
env_value_re = re.compile(r'ENV\[(.+)\]')
7071

7172
def __init__(self, url, key, client, version='0.10.8', headers={}):
7273
self.url = url.rstrip('/')
@@ -105,6 +106,10 @@ def _ruby_value(match):
105106
expr = match.group(1).strip()
106107
if expr == 'current_dir':
107108
return os.path.dirname(path)
109+
envmatch = cls.env_value_re.match(expr)
110+
if envmatch:
111+
envmatch = envmatch.group(1).strip('"').strip("'")
112+
return os.environ.get(envmatch) or ''
108113
log.debug('Unknown ruby expression in line "%s"', line)
109114
raise UnknownRubyExpression
110115
try:

chef/tests/test_api.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,10 @@ def test_current_dir(self):
1818
api = self.load('current_dir.rb')
1919
path = os.path.join(os.path.dirname(__file__), 'configs', 'test_1')
2020
self.assertEqual(api.client, path)
21+
22+
def test_env_variables(self):
23+
username = os.environ.get('LOGNAME')
24+
if username is None:
25+
self.fail('could not read $LOGNAME from environment')
26+
api = self.load('env_values.rb')
27+
self.assertEqual(api.client, username)

0 commit comments

Comments
 (0)