config: fix configure() method to validate configuration only if -V/--validate parameter is provided

This commit is contained in:
Benjamin Renard 2023-12-15 13:15:07 +01:00
parent 371d194728
commit 4962b16099
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC

View file

@ -798,7 +798,7 @@ class Config: # pylint: disable=too-many-instance-attributes
self._loaded_callbacks_executed.append(callback) self._loaded_callbacks_executed.append(callback)
return not error return not error
def save(self, filepath=None): def save(self, filepath=None, reload=True):
"""Save configuration file""" """Save configuration file"""
filepath = filepath if filepath else self._filepath filepath = filepath if filepath else self._filepath
assert filepath, "Configuration filepath is not set or provided" assert filepath, "Configuration filepath is not set or provided"
@ -828,7 +828,8 @@ class Config: # pylint: disable=too-many-instance-attributes
except Exception: # pylint: disable=broad-except except Exception: # pylint: disable=broad-except
log.exception("Failed to write generated configuration file %s", filepath) log.exception("Failed to write generated configuration file %s", filepath)
return False return False
self.load_file(filepath) if reload:
return self.load_file(filepath)
return True return True
@property @property
@ -1158,24 +1159,14 @@ class Config: # pylint: disable=too-many-instance-attributes
options = self.parse_arguments_options(argv, create=False, execute_callback=False) options = self.parse_arguments_options(argv, create=False, execute_callback=False)
if os.path.exists(options.config) and not options.overwrite: def validate():
print(f"Configuration file {options.config} already exists") """Validate configuration file"""
sys.exit(1)
if options.interactive:
self.ask_values(set_it=True)
if self.save(options.config):
print(f"Configuration file {options.config} created.")
if options.validate:
print("Validate your configuration...") print("Validate your configuration...")
try: try:
if self._loaded(): if self.load_file(options.config):
print("Your configuration seem valid.") print("Your configuration seem valid.")
else: else:
print( print("Error(s) occurred validating your configuration. See logs for details.")
"Error(s) occurred validating your configuration. See logs for details."
)
sys.exit(1) sys.exit(1)
except Exception: # pylint: disable=broad-except except Exception: # pylint: disable=broad-except
print( print(
@ -1184,6 +1175,24 @@ class Config: # pylint: disable=too-many-instance-attributes
"\n\nSee logs for details." "\n\nSee logs for details."
) )
sys.exit(2) sys.exit(2)
if os.path.exists(options.config) and not options.overwrite:
print(
f"Configuration file {options.config} already exists. "
"Use -O/--overwrite parameter to overwrite it."
)
if options.validate:
validate()
sys.exit(0)
sys.exit(1)
if options.interactive:
self.ask_values(set_it=True)
if self.save(options.config, reload=False):
print(f"Configuration file {options.config} created.")
if options.validate:
validate()
else: else:
print(f"Error occured creating configuration file {options.config}") print(f"Error occured creating configuration file {options.config}")
sys.exit(1) sys.exit(1)