|
3 | 3 | import warnings |
4 | 4 | from collections import OrderedDict |
5 | 5 |
|
| 6 | +import click |
| 7 | + |
6 | 8 |
|
7 | 9 | def load_dotenv(dotenv_path): |
8 | 10 | """ |
@@ -110,54 +112,58 @@ def flatten_and_write(dotenv_path, dotenv_as_dict): |
110 | 112 | f.write('%s="%s"\r\n' % (k, v)) |
111 | 113 | return True |
112 | 114 |
|
113 | | -if __name__ == "__main__": |
114 | | - import argparse |
115 | | - parser = argparse.ArgumentParser() |
116 | | - parser.add_argument("file_path", help="the absolute path of the .env file you want to use") |
117 | | - parser.add_argument("action", help="what you want to do with the .env file (get, set, unset)", nargs='?') |
118 | | - parser.add_argument("key", help="the environment key you want to set", nargs='?') |
119 | | - parser.add_argument("value", help="the value you want to set 'key' to", nargs='?') |
120 | | - parser.add_argument("--force", help="force writing even if the file at the given path doesn't end in .env") |
121 | | - args = parser.parse_args() |
122 | | - |
123 | | - if not os.path.exists(args.file_path): |
124 | | - warnings.warn("there doesn't appear to be a file at %s" % args.file_path) |
125 | | - exit(1) |
126 | | - if not args.force: |
127 | | - if not args.file_path.endswith(".env"): |
128 | | - warnings.warn("the file %s doesn't appear to be a .env file, use --force to proceed" % args.file_path) |
129 | | - exit(1) |
130 | 115 |
|
131 | | - if not args.action: |
132 | | - with open(args.file_path) as f: |
133 | | - print(f.read()) |
134 | | - exit(0) |
135 | | - elif args.action == "get": |
136 | | - stored_value = get_key(args.file_path, args.key) |
137 | | - if stored_value is not None: |
138 | | - print(args.key) |
139 | | - print(stored_value) |
140 | | - else: |
| 116 | +@click.command() |
| 117 | +@click.argument('action', type=click.Choice(['get', 'set', 'unset']), required=False) |
| 118 | +@click.argument('key', required=False) |
| 119 | +@click.argument('value', required=False) |
| 120 | +@click.option('--force', is_flag=True) |
| 121 | +@click.option('-f', '--file', default='.env', type=click.Path(exists=True)) |
| 122 | +def cli(file, action, key, value, force): |
| 123 | + |
| 124 | + if not action: |
| 125 | + dotenv_as_dict = parse_dotenv(file) |
| 126 | + for k, v in dotenv_as_dict: |
| 127 | + click.echo("%s=%s" % (k, v)) |
| 128 | + |
| 129 | + if action == 'get': |
| 130 | + stored_value = get_key(file, key) |
| 131 | + if stored_value: |
| 132 | + click.echo("%s=%s" % (key, stored_value)) |
141 | 133 | exit(1) |
142 | | - elif args.action == "set": |
143 | | - success, key, value = set_key(args.file_path, args.key, args.value) |
144 | | - if success is not None: |
145 | | - print("%s: %s" % (key, value)) |
146 | 134 | else: |
| 135 | + click.echo("%s doesn't seems to have been set yet.") |
| 136 | + exit(0) |
| 137 | + |
| 138 | + elif action == 'set': |
| 139 | + if not value: |
| 140 | + click.echo("Error: value is missing.") |
| 141 | + exit(0) |
| 142 | + success, key, value = set_key(file, key, value) |
| 143 | + if success: |
| 144 | + click.echo("%s=%s" % (key, value)) |
147 | 145 | exit(1) |
148 | | - elif args.action == "unset": |
149 | | - success, key = unset_key(args.file_path, args.key) |
150 | | - if success is not None: |
151 | | - print("unset %s" % key) |
152 | 146 | else: |
| 147 | + exit(0) |
| 148 | + |
| 149 | + elif action == 'unset': |
| 150 | + success, key = unset_key(file, key) |
| 151 | + if success: |
| 152 | + click.echo("Successfully removed %s" % key) |
153 | 153 | exit(1) |
| 154 | + else: |
| 155 | + exit(0) |
| 156 | + |
154 | 157 | # Need to investigate if this can actually work or if the scope of the new environ variables |
155 | 158 | # Expires when python exits |
156 | | - # |
157 | | - # elif args.action == "load": |
158 | | - # success = load_dotenv(args.file_path) |
| 159 | + |
| 160 | + # elif action == "load": |
| 161 | + # success = load_dotenv(file) |
159 | 162 | # if success != None: |
160 | | - # print("loaded %s into environment" % args.file_path) |
| 163 | + # click.echo("loaded %s into environment" % file) |
161 | 164 | # else: |
162 | 165 | # exit(1) |
163 | | - exit(0) |
| 166 | + |
| 167 | + |
| 168 | +if __name__ == "__main__": |
| 169 | + cli() |
0 commit comments