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 5a108739af
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)
return not error
def save(self, filepath=None):
def save(self, filepath=None, reload=True):
"""Save configuration file"""
filepath = filepath if filepath else self._filepath
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
log.exception("Failed to write generated configuration file %s", filepath)
return False
self.load_file(filepath)
if reload:
return self.load_file(filepath)
return True
@property
@ -1158,24 +1159,14 @@ class Config: # pylint: disable=too-many-instance-attributes
options = self.parse_arguments_options(argv, create=False, execute_callback=False)
if os.path.exists(options.config) and not options.overwrite:
print(f"Configuration file {options.config} already exists")
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:
def validate():
"""Validate configuration file"""
print("Validate your configuration...")
try:
if self._loaded():
if self.load_file(options.config):
print("Your configuration seem valid.")
else:
print(
"Error(s) occurred validating your configuration. See logs for details."
)
print("Error(s) occurred validating your configuration. See logs for details.")
sys.exit(1)
except Exception: # pylint: disable=broad-except
print(
@ -1184,6 +1175,23 @@ class Config: # pylint: disable=too-many-instance-attributes
"\n\nSee logs for details."
)
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)
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:
print(f"Error occured creating configuration file {options.config}")
sys.exit(1)