Skip to content

Commit 579aeb4

Browse files
author
Saurabh Kumar
committed
freeze the cli interface
1 parent be6329e commit 579aeb4

3 files changed

Lines changed: 93 additions & 48 deletions

File tree

README.md

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,60 @@ doesn't. Let's fix that.
99

1010
[heroku config](https://devcenter.heroku.com/articles/config-vars) Lets you add/delete env variables on your remote server from your local command line. django-dotenv-rw when used with fabric lets you do the same ```heroku config:set DJANGO_ENV="PRODUCTION" ``` becomes ```fab config:set,DJANGO_ENV,PRODUCTION```
1111

12-
<!-- MarkdownTOC -->
12+
<!-- MarkdownTOC depth=3-->
1313

1414
- [Installation](#installation)
1515
- [Usage](#usage)
16+
- [Command-line interface](#command-line-interface)
1617
- [Loading settings from a `.env` file into your django environment](#loading-settings-from-a-.env-file-into-your-django-environment)
1718
- [Setting remote config](#setting-remote-config)
19+
- [Contributing](#contributing)
1820

1921
<!-- /MarkdownTOC -->
2022

2123

22-
## Installation
24+
# Installation
2325

2426
```
2527
pip install git+ssh://[email protected]/theskumar/python-dotenv.git
2628
```
2729

2830
# Usage
2931

32+
## Command-line interface
33+
34+
<pre>
35+
$ dotenv
36+
Usage: dotenv [OPTIONS] COMMAND [ARGS]...
37+
38+
This script is used to set, get or unset values from a .env file.
39+
40+
Options:
41+
-f, --file PATH Location of the .env file, defaults to .env file in current
42+
working directory.
43+
--help Show this message and exit.
44+
45+
Commands:
46+
get Retrive the value for the given key.
47+
list Display all the stored key/value.
48+
set Store the given key/value.
49+
unset Removes the given key.
50+
</pre>
51+
3052
## Loading settings from a `.env` file into your django environment
3153

3254
Option 1 (suggested): Near the top of `settings.py`. Add:
3355

34-
```
56+
```python
3557
import os
3658
import dotenv
3759
PROJECT_PATH = os.path.dirname(os.path.dirname(__file__))
3860
dotenv.load_dotenv(os.path.join(PROJECT_PATH, ".env"))
3961
```
4062

4163
Option 2: If you want your server to set the env variables and only use `dotenv` when you're using `manage.py`: in `manage.py` add:
42-
```
64+
65+
```python
4366
import dotenv
4467
dotenv.load_dotenv(os.path.join(os.path.dirname(__file__), ".env"))
4568
```
@@ -68,9 +91,9 @@ Usage is designed to mirror the heroku config api very closely.
6891

6992
Get all your remote config info with `fab config`
7093
```
71-
$ fab config
94+
$ fab config:list
7295
[...example.com] Executing task 'config'
73-
[...example.com] run: dotenv -f /home/me/webapps/myapp/myapp/.env
96+
[...example.com] run: dotenv -f /home/me/webapps/myapp/myapp/.env list
7497
[...example.com] out: DJANGO_DEBUG="true"
7598
[...example.com] out: DJANGO_ENV="test"
7699
```
@@ -114,3 +137,7 @@ $ fab config:set,hello,world config:set,foo,bar config:set,fizz,buzz
114137
```
115138

116139
That's it. example.com, or whoever your non-paas host is, is now 1 facor closer to an easy 12 factor app.
140+
141+
# Contributing
142+
143+
You can either open an issue or send a pull request.

dotenv.py

Lines changed: 58 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -93,47 +93,15 @@ def flatten_and_write(dotenv_path, dotenv_as_dict):
9393
return True
9494

9595

96-
@click.command()
97-
@click.argument('action', type=click.Choice(['get', 'set', 'unset']), required=False)
98-
@click.argument('key', required=False)
99-
@click.argument('value', required=False)
100-
@click.option('--force', is_flag=True)
96+
@click.group()
10197
@click.option('-f', '--file', default=os.path.join(os.getcwd(), '.env'),
102-
type=click.Path(exists=True))
103-
def cli(file, action, key, value, force):
104-
105-
if not action:
106-
dotenv_as_dict = parse_dotenv(file)
107-
for k, v in dotenv_as_dict:
108-
click.echo("%s=%s" % (k, v))
109-
110-
if action == 'get':
111-
stored_value = get_key(file, key)
112-
if stored_value:
113-
click.echo('%s="%s"' % (key, stored_value))
114-
exit(0)
115-
else:
116-
click.echo("%s doesn't seems to have been set yet.")
117-
exit(1)
118-
119-
elif action == 'set':
120-
if not value:
121-
click.echo("Error: value is missing.")
122-
exit(1)
123-
success, key, value = set_key(file, key, value)
124-
if success:
125-
click.echo('%s="%s"' % (key, value))
126-
exit(0)
127-
else:
128-
exit(1)
129-
130-
elif action == 'unset':
131-
success, key = unset_key(file, key)
132-
if success:
133-
click.echo("Successfully removed %s" % key)
134-
exit(1)
135-
else:
136-
exit(0)
98+
type=click.Path(exists=True),
99+
help="Location of the .env file, defaults to .env file in current working directory.")
100+
@click.pass_context
101+
def cli(ctx, file):
102+
'''This script is used to set, get or unset values from a .env file.'''
103+
ctx.obj = {}
104+
ctx.obj['FILE'] = file
137105

138106
# Need to investigate if this can actually work or if the scope of the new environ variables
139107
# Expires when python exits
@@ -146,5 +114,55 @@ def cli(file, action, key, value, force):
146114
# exit(1)
147115

148116

117+
@cli.command()
118+
@click.pass_context
119+
def list(ctx):
120+
'''Display all the stored key/value.'''
121+
file = ctx.obj['FILE']
122+
dotenv_as_dict = parse_dotenv(file)
123+
for k, v in dotenv_as_dict:
124+
click.echo('%s="%s"' % (k, v))
125+
126+
127+
@cli.command()
128+
@click.pass_context
129+
@click.argument('key', required=True)
130+
@click.argument('value', required=True)
131+
def set(ctx, key, value):
132+
'''Store the given key/value.'''
133+
file = ctx.obj['FILE']
134+
success, key, value = set_key(file, key, value)
135+
if success:
136+
click.echo('%s="%s"' % (key, value))
137+
else:
138+
exit(1)
139+
140+
141+
@cli.command()
142+
@click.pass_context
143+
@click.argument('key', required=True)
144+
def get(ctx, key):
145+
'''Retrive the value for the given key.'''
146+
file = ctx.obj['FILE']
147+
stored_value = get_key(file, key)
148+
if stored_value:
149+
click.echo('%s="%s"' % (key, stored_value))
150+
else:
151+
exit(1)
152+
153+
154+
@cli.command()
155+
@click.pass_context
156+
@click.argument('key', required=True)
157+
def unset(ctx, key):
158+
'''Removes the given key.'''
159+
file = ctx.obj['FILE']
160+
success, key = unset_key(file, key)
161+
if success:
162+
click.echo("Successfully removed %s" % key)
163+
else:
164+
exit(1)
165+
166+
149167
if __name__ == "__main__":
150168
cli()

tests/test_cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ def test_read_write():
1717

1818
def test_console_script():
1919
sh.touch(dotenv_path)
20-
sh.dotenv('set', 'HELLO', 'WORLD', '-f', dotenv_path)
21-
output = sh.dotenv('get', 'HELLO', '-f', dotenv_path)
20+
sh.dotenv('-f', dotenv_path, 'set', 'HELLO', 'WORLD')
21+
output = sh.dotenv('-f', dotenv_path, 'get', 'HELLO', )
2222
assert output == 'HELLO="WORLD"\n'
2323
sh.rm(dotenv_path)
2424

0 commit comments

Comments
 (0)