11import typer
2-
2+ from typing import get_args
33from copia ._version import __version__
4- from .commands import list_app , init_command
5- from .exit_codes import ExitCodes
6- from copia .adapters import get_adapter
7- from ..tui import CopiaApp
8- from copia .cli .console_utils import print_error , info , success , error , echo
9- from .config import (
10- get_profile ,
11- LOCAL_COPIA_FILE ,
12- GLOBAL_COPIA_FILE
4+ from .commands import (
5+ list_command ,
6+ init_command ,
7+ tui_command ,
8+ run_command
9+ )
10+ from .config import Adapter
11+ from .utils import echo
12+
13+
14+ AVAILABLES_ADAPTERS : tuple = get_args (Adapter )
15+
16+ def get_help_msg ():
17+ help_msg = "An Interactive TUI app for Databases seeding"
18+ help_msg += "\n \n Supported databases:"
19+ for value in AVAILABLES_ADAPTERS :
20+ help_msg += f'\n - { value } '
21+ return help_msg
22+
23+ app = typer .Typer (
24+ name = 'copia' ,
25+ help = get_help_msg (),
26+ no_args_is_help = True ,
27+ invoke_without_command = True ,
28+ context_settings = {
29+ 'help_option_names' : ['--help' , '-h' ]
30+ }
1331)
14-
15- app = typer .Typer (invoke_without_command = True )
1632
1733app .add_typer (init_command )
18- app .add_typer (list_app )
34+ app .add_typer (list_command )
35+ app .add_typer (tui_command )
36+ app .add_typer (run_command )
1937
2038@app .callback ()
2139def main (
2240 ctx : typer .Context ,
23- profile_name : str = typer .Option ("default" ,
24- "-p" , "--profile" ,
25- help = "The profile to use for the session" ),
41+
2642 version_flag : bool = typer .Option (False ,
2743 "-v" , "--version" ,
2844 help = "Display the current version and exit" ),
29-
30- help_flag : bool = typer .Option (False ,
31- "-h" , "--help" ,
32- help = "Display this message and exit" ),
33-
34- search_globals_only : bool = typer .Option (False ,
35- "-g" , "--global" ,
36- help = f"Search only in [green]'{ GLOBAL_COPIA_FILE } '" ),
37- search_locals_only : bool = typer .Option (False ,
38- "-l" , "--local" ,
39- help = f"Search only in [green]'{ LOCAL_COPIA_FILE } '" ),
40- ):
41-
42- """An Interactive TUI app for MySQL Database seeding"""
43-
45+ ):
4446 if ctx .invoked_subcommand is not None :
4547 return
46-
47- if help_flag :
48- echo (ctx .get_help ())
49- raise typer .Exit ()
50-
48+
5149 if version_flag :
5250 echo (f"[blue]copia { __version__ } " )
53- raise typer .Exit ()
54-
55- profile = None
56- try :
57- if search_globals_only and search_locals_only :
58- error ("Cannot use --global | -g and --local | -l at the same time" )
59- raise typer .Exit ()
60-
61- if search_globals_only :
62- profile = get_profile (profile_name , "global" )
63- elif search_locals_only :
64- profile = get_profile (profile_name , "local" )
65- else :
66- profile = get_any_profile (profile_name )
67- except Exception as err :
68- print_error (err )
69- raise typer .Exit ()
70-
71-
72- info (f"Found profile: { profile } " )
73- info ("Connecting to db..." )
74-
75- try :
76- adapter = get_adapter (profile )
77- success ("Connection to db successfull" )
78- except ImportError :
79- print_error (f"Missing dependencies for adapter { profile .adapter !r} " ,
80- f'Try "pip install copia-seed\\ [{ profile .adapter } ]"' )
81- raise typer .Exit (ExitCodes .RESOURCE_ERROR )
82- except Exception as connection_err :
83- print_error (connection_err )
84- raise typer .Exit (ExitCodes .CONNEXION_TO_DB_FAILED )
85-
86- CopiaApp (adapter ).run ()
87- success ("Bye." )
88-
89-
90- def get_any_profile (profile_name : str ):
91- """
92- Try to fetch the profile from the local config file,
93- and fall back to global on any fail.
94- Any exception from local fetching get silently swallowed
95-
96- All the exceptions raised by this function are always in the global fetch state
97-
98- Args:
99- profile_name (str): the name by which the profile is identified
100-
101- Raises:
102- FileNotFoundError: when the file couldn't be found.
103- PermissionError: when permission to read the file had been denied by the os.
104- InvalidConfigError: when the config isn't valid TOML or the value of the "profiles" key is not a dict/TOML Table
105- ProfileNotFoundError: when the "profiles.profile_name" key don't exist, this also includes missing "profiles" key
106- FoundProfileIsNotATableError: when the "profiles.profile_name" key exist but is not a dict
107- InvalidProfileError: when the profile isn't valid
108-
109- Returns:
110- Profile: a valid Profile object
111- """
112- try :
113- info (f"Fetching profiles.\" { profile_name } \" from local config..." )
114- return get_profile (profile_name , "local" )
115- except Exception as err :
116- error (f"{ err } " )
117- info ("Falling back to global config..." )
118- info (f"Fetching profiles.\" { profile_name } \" from global config..." )
119- return get_profile (profile_name , "global" )
51+ raise typer .Exit ()
0 commit comments