Skip to content

Commit 9a52e3d

Browse files
author
Saurabh Kumar
committed
feat(get_cli_string): Abstact our cli string creation logic
- Simplifies the fabric command. - More validation logic can put in the `get_cli_sting` without breaking the public api.
1 parent d8c5e7b commit 9a52e3d

4 files changed

Lines changed: 39 additions & 12 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ __pycache__
77
.coverage
88
.DS_Store
99
htmlcov/
10+
.cache/

README.rst

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,23 +133,20 @@ server.
133133
# fabfile.py
134134
135135
from fabric.api import task, run, env
136+
import dotenv
136137
137-
# absolute path to the location of .env on remote server
138+
# absolute path to the location of .env on remote server.
138139
env.dotenv_path = '/home/me/webapps/myapp/myapp/.env'
139140
140141
@task
141-
def config(action=None, key=None, value=None):
142+
def config(action=None, key_value=None):
142143
'''Manage project configuration via .env
143144
144145
see: https://github.com/theskumar/python-dotenv
145-
e.g: fab config:set,[key],[value]
146+
e.g: fab config:set[,key][=value]
146147
'''
147148
run('touch %(dotenv_path)s' % env)
148-
command = 'dotenv'
149-
command += ' -f %s ' % env.dotenv_path
150-
command += action + " " if action else " "
151-
command += key + " " if key else " "
152-
command += value if value else ""
149+
command = dotenv.get_cli_string(env.dotenv_path, action, key_value)
153150
run(command)
154151
155152
Usage is designed to mirror the heroku config api very closely.
@@ -168,7 +165,7 @@ Set remote config variables with ``fab config:set,[key],[value]``
168165

169166
::
170167

171-
$ fab config:set,hello,world
168+
$ fab config:set,hello=world
172169
[...example.com] Executing task 'config'
173170
[...example.com] run: dotenv -f /home/me/webapps/myapp/myapp/.env set hello world
174171
[...example.com] out: hello="world"
@@ -192,12 +189,11 @@ Delete a remote config variables with ``fab config:unset,[key]``
192189
[...example.com] out: unset hello
193190

194191
Thanks entirely to fabric and not one bit to this project, you can chain
195-
commands like
196-
so\ ``fab config:set,[key1],[value1] config:set,[key2],[value2]``
192+
commands like so ``fab config:set,[key1][=value1] config:set,[key2][=value2]``
197193

198194
::
199195

200-
$ fab config:set,hello,world config:set,foo,bar config:set,fizz,buzz
196+
$ fab config:set,hello=world config:set,foo=bar config:set,fizz=buzz
201197
[...example.com] Executing task 'config'
202198
[...example.com] run: dotenv -f /home/me/webapps/myapp/myapp/.env set hello world
203199
[...example.com] out: hello="world"

dotenv.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,5 +168,24 @@ def unset(ctx, key):
168168
exit(1)
169169

170170

171+
def get_cli_string(path=None, action=None, key_value=None):
172+
"""Returns a string suitable for running as a shell script.
173+
174+
Useful for converting a arguments passed to a fabric task
175+
to be passed to a `local` or `run` command.
176+
"""
177+
command = ['dotenv']
178+
if path:
179+
command.append('-f %s' % path)
180+
if action:
181+
command.append(action)
182+
if key_value:
183+
if '=' in key_value:
184+
command += key_value.split('=', 1)
185+
else:
186+
command.append(key_value)
187+
188+
return ' '.join(command).strip()
189+
171190
if __name__ == "__main__":
172191
cli()

tests/test_utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from dotenv import get_cli_string as c
2+
3+
4+
def test_to_cli_string():
5+
assert c() == 'dotenv'
6+
assert c(path='/etc/.env') == 'dotenv -f /etc/.env'
7+
assert c(path='/etc/.env', action='list') == 'dotenv -f /etc/.env list'
8+
assert c(action='list') == 'dotenv list'
9+
assert c(action='get', key_value='DEBUG') == 'dotenv get DEBUG'
10+
assert c(action='set', key_value='DEBUG=True') == 'dotenv set DEBUG True'
11+
assert c(action='set', key_value='SECRET==@asdfasf') == 'dotenv set SECRET =@asdfasf'

0 commit comments

Comments
 (0)