1010import aperturedb .cli .keys as keys
1111from aperturedb .Configuration import Configuration
1212from aperturedb .CommonLibrary import _create_configuration_from_json , __create_connector
13+ import re
14+
15+
16+ # alnum for first character, then anum with - or _ for rest. Max Length 64
17+ CONFIG_NAME_RE = re .compile (
18+ r'^([a-zA-Z0-9]([a-zA-Z0-9-_]){0,63})'
19+ )
1320
1421
1522class ObjEncoder (json .JSONEncoder ):
@@ -85,11 +92,7 @@ def get_configurations(file: str):
8592 return configs , active
8693
8794
88- @app .command ()
89- def ls (log_to_console : bool = True ):
90- """
91- List the configurations with their details.
92- """
95+ def get_all_configs ():
9396 all_configs = {}
9497 for as_global in [True , False ]:
9598 config_path = _config_file_path (as_global )
@@ -114,6 +117,24 @@ def ls(log_to_console: bool = True):
114117 all_configs ["environment" ] = {}
115118 all_configs ["environment" ][env_key ] = config
116119 all_configs ["active" ] = f"env:{ env_key } "
120+ return all_configs
121+
122+
123+ def get_active_config (all_configs ):
124+ active = all_configs ["active" ]
125+ if active .startswith ("env:" ):
126+ return all_configs ["environment" ][active [4 :]]
127+ else :
128+ return all_configs ["local" ][active ] if "locan" in all_configs and active in all_configs ["local" ] \
129+ else all_configs ["global" ][active ]
130+
131+
132+ @app .command ()
133+ def ls (log_to_console : bool = True ):
134+ """
135+ List the configurations with their details.
136+ """
137+ all_configs = get_all_configs ()
117138
118139 if "global" in all_configs or "local" in all_configs :
119140 if "global" in all_configs and len (all_configs ["global" ]) == 0 \
@@ -214,6 +235,10 @@ def check_for_overwrite(name):
214235 name = name if name is not None else gen_config .name
215236
216237 else :
238+ if not CONFIG_NAME_RE .match (name ):
239+ console .log (
240+ f"Configuration name { name } must be alphanumerical with dashes of 1-64 characters in length" , style = "bold yellow" )
241+ raise typer .Exit (code = 2 )
217242 if interactive :
218243 if name is None :
219244 name = typer .prompt (
@@ -395,3 +420,75 @@ def get_key(name: Annotated[str, typer.Argument(
395420 check_configured (as_global = True , show_error = True )
396421
397422 print (f"{ user_key } " )
423+
424+
425+ @app .command ()
426+ def get (
427+ tag : Annotated [str , typer .Argument (help = "Tag of information to get" )],
428+ ignore_unset : Annotated [bool , typer .Option (help = "ignore unsetl" )] = False ):
429+ """
430+ Retrieve detail of a config.
431+ """
432+
433+ all_configs = get_all_configs ()
434+ used_config = None
435+ remaining = ""
436+ if tag [0 ] == "." :
437+ used_config = get_active_config (all_configs )
438+ remaining = tag
439+ else :
440+ # match config name to config element seperator: .
441+ m = re .match (f"{ CONFIG_NAME_RE .pattern } \\ ." , tag )
442+ if len (m .groups ()) < 1 or len (m .groups (0 )) == 0 :
443+ console .log (f"Configuration name { tag } is invalid" )
444+ raise typer .Exit (code = 2 )
445+ else :
446+ name = m .group (1 )
447+ has_local = "local" in all_configs
448+ if (has_local and not name in all_configs ["local" ]) or \
449+ not name in all_configs ["global" ]:
450+ console .log (f"No Configuration { name } " )
451+ raise typer .Exit (code = 2 )
452+ else :
453+ used_config = all_configs ["local" ][name ] \
454+ if has_local and name in all_configs ["local" ] \
455+ else all_configs ["global" ][name ]
456+ remaining = tag [len (name ):]
457+
458+ if remaining [0 ] != "." :
459+ console .log (
460+ f"Cannot create configuration data to retrieve from { remaining } " )
461+ raise typer .Exit (code = 2 )
462+ else :
463+ # match config name to end of string
464+ m = re .match (f"{ CONFIG_NAME_RE .pattern } $" , remaining [1 :])
465+ if len (m .groups ()) < 1 or len (m .groups (0 )) == 0 :
466+ console .log (f"Configuration item { remaining [1 :]} is invalid" )
467+ raise typer .Exit (code = 2 )
468+ else :
469+ config_item = m .group (0 )
470+ print_ok = True
471+
472+ # check if attribut exists or is valid to print.
473+ if not config_item in dir (used_config ):
474+ print_ok = False
475+ else :
476+ attrib = getattr (used_config , config_item )
477+ # we allow only retreiving string, int or bool values from the
478+ # Configuration.
479+ allowed_types = [str , int , bool , type (None )]
480+ allowed_commands = ["auth_mode" ]
481+ if config_item in allowed_commands :
482+ attrib = attrib ()
483+ elif not any ([isinstance (attrib , allowed_type ) for allowed_type in allowed_types ]):
484+ print_ok = False
485+
486+ if print_ok :
487+ if attrib is None and not ignore_unset :
488+ console .log (f"Configuration Item { config_item } is unset" )
489+ raise typer .Exit (code = 2 )
490+ else :
491+ print (attrib )
492+ else :
493+ console .log (f"No Configuration Item { config_item } " )
494+ raise typer .Exit (code = 2 )
0 commit comments