|
1 | | -import json |
2 | | - |
3 | 1 | import cloudinary |
4 | | -from click import command, option |
5 | | -from cloudinary import api |
| 2 | +from click import command, option, echo |
6 | 3 |
|
7 | | -from cloudinary_cli.utils import CLOUDINARY_CLI_CONFIG_FILE, refresh_config, logger |
| 4 | +from cloudinary_cli.defaults import logger |
| 5 | +from cloudinary_cli.utils.config_utils import load_config, verify_cloudinary_url, update_config, remove_config_keys |
8 | 6 |
|
9 | 7 |
|
10 | 8 | @command("config", help="Display the current configuration, and manage additional configurations.") |
11 | 9 | @option("-n", "--new", help="""\b Create and name a configuration from a Cloudinary account environment variable. |
12 | 10 | e.g. cld config -n <NAME> <CLOUDINARY_URL>""", nargs=2) |
13 | 11 | @option("-ls", "--ls", help="List all saved configurations.", is_flag=True) |
14 | 12 | @option("-rm", "--rm", help="Delete a specified configuration.", nargs=1) |
15 | | -@option("-url", "--from_url", help="Create a configuration from a Cloudinary account environment variable. The configuration name is the cloud name.", nargs=1) |
| 13 | +@option("-url", "--from_url", |
| 14 | + help="Create a configuration from a Cloudinary account environment variable. " |
| 15 | + "The configuration name is the cloud name.", |
| 16 | + nargs=1) |
16 | 17 | def config(new, ls, rm, from_url): |
17 | | - if not (new or ls or rm or from_url): |
18 | | - logger.info('\n'.join(["{}:\t{}".format(k, v if k != "api_secret" |
19 | | - else "***************{}".format(v[-4:])) |
20 | | - for k, v in cloudinary.config().__dict__.items()])) |
21 | | - return |
| 18 | + if new or from_url: |
| 19 | + config_name, cloudinary_url = new or [None, from_url] |
22 | 20 |
|
23 | | - with open(CLOUDINARY_CLI_CONFIG_FILE, "r+") as f: |
24 | | - fi = f.read() |
25 | | - cfg = json.loads(fi) if fi != "" else {} |
26 | | - f.close() |
27 | | - if new: |
28 | | - try: |
29 | | - refresh_config(new[1]) |
30 | | - cfg[new[0]] = new[1] |
31 | | - api.ping() |
32 | | - with open(CLOUDINARY_CLI_CONFIG_FILE, "w") as f: |
33 | | - f.write(json.dumps(cfg)) |
34 | | - f.close() |
35 | | - logger.info("Config '{}' saved!".format(new[0])) |
36 | | - except Exception as e: |
37 | | - logger.error("Invalid Cloudinary URL: {}".format(new[1])) |
38 | | - raise e |
39 | | - return |
40 | | - if ls: |
41 | | - logger.info("\n".join(cfg.keys())) |
42 | | - if rm: |
43 | | - if rm not in cfg.keys(): |
44 | | - logger.warn("Configuration '{}' not found.".format(rm)) |
| 21 | + if not verify_cloudinary_url(cloudinary_url): |
45 | 22 | return |
46 | | - del cfg[rm] |
47 | | - open(CLOUDINARY_CLI_CONFIG_FILE, "w").write(json.dumps(cfg)) |
48 | | - logger.info("Configuration '{}' deleted".format(rm)) |
49 | | - return |
50 | | - if from_url: |
51 | | - if "CLOUDINARY_URL=" in from_url: |
52 | | - from_url = from_url[15:] |
53 | | - try: |
54 | | - refresh_config(from_url) |
55 | | - cfg[cloudinary.config().cloud_name] = from_url |
56 | | - api.ping() |
57 | | - with open(CLOUDINARY_CLI_CONFIG_FILE, "w") as f: |
58 | | - f.write(json.dumps(cfg)) |
59 | | - f.close() |
60 | | - logger.info("Config '{}' saved!".format(cloudinary.config().cloud_name)) |
61 | | - logger.info("Example usage: cld -C {} <command>".format(cloudinary.config().cloud_name)) |
62 | | - except Exception as e: |
63 | | - logger.error("Invalid Cloudinary URL: {}".format(from_url)) |
64 | | - raise e |
| 23 | + |
| 24 | + config_name = config_name or cloudinary.config().cloud_name |
| 25 | + |
| 26 | + update_config({config_name: cloudinary_url}) |
| 27 | + |
| 28 | + logger.info("Config '{}' saved!".format(config_name)) |
| 29 | + logger.info("Example usage: cld -C {} <command>".format(config_name)) |
| 30 | + elif rm: |
| 31 | + if remove_config_keys(rm): |
| 32 | + logger.warn(f"Configuration '{rm}' not found.") |
| 33 | + else: |
| 34 | + logger.info(f"Configuration '{rm}' deleted") |
| 35 | + elif ls: |
| 36 | + echo("\n".join(load_config().keys())) |
| 37 | + else: |
| 38 | + obfuscated_config = {k: v if k != "api_secret" else "***************{}".format(v[-4:]) |
| 39 | + for k, v in cloudinary.config().__dict__.items()} |
| 40 | + echo('\n'.join(["{}:\t{}".format(k, v) for k, v in obfuscated_config.items()])) |
0 commit comments