11#!/usr/bin/env python3
2- import platform
3- import shutil
42import sys
53
6- import click
7- import click_log
8- import cloudinary
4+ from click import ClickException
95
106import cloudinary_cli .core
117import cloudinary_cli .modules
128import cloudinary_cli .samples
13- from cloudinary_cli .defaults import logger
14- from cloudinary_cli .utils .config_utils import initialize , load_config , refresh_cloudinary_config , \
15- is_valid_cloudinary_config
9+ from cloudinary_cli .cli_group import cli
10+ from cloudinary_cli .utils .config_utils import initialize
1611from cloudinary_cli .utils .utils import log_exception , ConfigurationError
17- from cloudinary_cli .version import __version__ as cli_version
18-
19- CONTEXT_SETTINGS = dict (max_content_width = shutil .get_terminal_size ()[0 ], terminal_width = shutil .get_terminal_size ()[0 ])
20-
21-
22- @click .group (context_settings = CONTEXT_SETTINGS )
23- @click .help_option ()
24- @click .version_option (cli_version , prog_name = "Cloudinary CLI" ,
25- message = f"%(prog)s, version %(version)s\n "
26- f"Cloudinary SDK, version { cloudinary .VERSION } \n "
27- f"Python, version { platform .python_version ()} " )
28- @click .option ("-c" , "--config" ,
29- help = """Tell the CLI which account to run the command on by specifying an account environment variable."""
30- )
31- @click .option ("-C" , "--config_saved" ,
32- help = """Tell the CLI which account to run the command on by specifying a saved configuration - see
33- `config` command.""" )
34- @click_log .simple_verbosity_option (logger )
35- def cli (config , config_saved ):
36- if config :
37- refresh_cloudinary_config (config )
38- elif config_saved :
39- config = load_config ()
40- if config_saved not in config :
41- raise Exception (f"Config { config_saved } does not exist" )
42-
43- refresh_cloudinary_config (config [config_saved ])
44-
45- if not is_valid_cloudinary_config ():
46- logger .warning ("No Cloudinary configuration found." )
47-
48- return 0
4912
5013
5114def import_commands (* command_modules ):
@@ -62,21 +25,26 @@ def import_commands(*command_modules):
6225
6326
6427def main ():
28+ exit_status = 1 # very optimistic :)
29+
6530 initialize ()
31+
6632 try :
67- exit_status = cli ()
33+ # we don't use standalone mode to get the return value from the command execution
34+ exit_status = cli .main (standalone_mode = False )
35+ except ClickException as e :
36+ # show usage with error message
37+ e .show ()
6838 except ConfigurationError as e :
6939 log_exception (e )
70- exit_status = 1
7140 except Exception as e :
7241 # Improve configuration error handling
7342 if "Must supply cloud_name" in str (e ):
7443 log_exception ("No Cloudinary configuration found." )
7544 else :
7645 log_exception (e , "Command execution failed" )
77- exit_status = 1
7846
79- return exit_status
47+ return 0 if exit_status or exit_status is None else 1
8048
8149
8250if __name__ == "__main__" :
0 commit comments