|
2 | 2 |
|
3 | 3 | import os |
4 | 4 | import warnings |
5 | | -try: |
6 | | - from collections import OrderedDict |
7 | | -except ImportError: |
8 | | - from ordereddict import OrderedDict |
9 | 5 |
|
10 | | -import click |
| 6 | +from .compat import OrderedDict |
11 | 7 |
|
12 | 8 |
|
13 | 9 | def load_dotenv(dotenv_path): |
@@ -95,99 +91,3 @@ def flatten_and_write(dotenv_path, dotenv_as_dict): |
95 | 91 | for k, v in dotenv_as_dict.items(): |
96 | 92 | f.write('%s="%s"\n' % (k, v)) |
97 | 93 | return True |
98 | | - |
99 | | - |
100 | | -@click.group() |
101 | | -@click.option('-f', '--file', default=os.path.join(os.getcwd(), '.env'), |
102 | | - type=click.Path(exists=True), |
103 | | - help="Location of the .env file, defaults to .env file in current working directory.") |
104 | | -@click.pass_context |
105 | | -def cli(ctx, file): |
106 | | - '''This script is used to set, get or unset values from a .env file.''' |
107 | | - ctx.obj = {} |
108 | | - ctx.obj['FILE'] = file |
109 | | - |
110 | | - # Need to investigate if this can actually work or if the scope of the new environ variables |
111 | | - # Expires when python exits |
112 | | - |
113 | | - # elif action == "load": |
114 | | - # success = load_dotenv(file) |
115 | | - # if success != None: |
116 | | - # click.echo("loaded %s into environment" % file) |
117 | | - # else: |
118 | | - # exit(1) |
119 | | - |
120 | | - |
121 | | -@cli.command() |
122 | | -@click.pass_context |
123 | | -def list(ctx): |
124 | | - '''Display all the stored key/value.''' |
125 | | - file = ctx.obj['FILE'] |
126 | | - dotenv_as_dict = parse_dotenv(file) |
127 | | - for k, v in dotenv_as_dict: |
128 | | - click.echo('%s="%s"' % (k, v)) |
129 | | - |
130 | | - |
131 | | -@cli.command() |
132 | | -@click.pass_context |
133 | | -@click.argument('key', required=True) |
134 | | -@click.argument('value', required=True) |
135 | | -def set(ctx, key, value): |
136 | | - '''Store the given key/value.''' |
137 | | - file = ctx.obj['FILE'] |
138 | | - success, key, value = set_key(file, key, value) |
139 | | - if success: |
140 | | - click.echo('%s="%s"' % (key, value)) |
141 | | - else: |
142 | | - exit(1) |
143 | | - |
144 | | - |
145 | | -@cli.command() |
146 | | -@click.pass_context |
147 | | -@click.argument('key', required=True) |
148 | | -def get(ctx, key): |
149 | | - '''Retrive the value for the given key.''' |
150 | | - file = ctx.obj['FILE'] |
151 | | - stored_value = get_key(file, key) |
152 | | - if stored_value: |
153 | | - click.echo('%s="%s"' % (key, stored_value)) |
154 | | - else: |
155 | | - exit(1) |
156 | | - |
157 | | - |
158 | | -@cli.command() |
159 | | -@click.pass_context |
160 | | -@click.argument('key', required=True) |
161 | | -def unset(ctx, key): |
162 | | - '''Removes the given key.''' |
163 | | - file = ctx.obj['FILE'] |
164 | | - success, key = unset_key(file, key) |
165 | | - if success: |
166 | | - click.echo("Successfully removed %s" % key) |
167 | | - else: |
168 | | - exit(1) |
169 | | - |
170 | | - |
171 | | -def get_cli_string(path=None, action=None, key=None, 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: |
183 | | - command.append(key) |
184 | | - if value: |
185 | | - if ' ' in value: |
186 | | - command.append('"%s"' % value) |
187 | | - else: |
188 | | - command.append(value) |
189 | | - |
190 | | - return ' '.join(command).strip() |
191 | | - |
192 | | -if __name__ == "__main__": |
193 | | - cli() |
0 commit comments