mirror of
https://github.com/netinvent/npbackup.git
synced 2025-09-13 00:16:21 +08:00
Fix config loader when empty / wrong file is given
This commit is contained in:
parent
c550e741a6
commit
213e9bc4cf
1 changed files with 16 additions and 9 deletions
|
@ -7,10 +7,11 @@ __intname__ = "npbackup.configuration"
|
||||||
__author__ = "Orsiris de Jong"
|
__author__ = "Orsiris de Jong"
|
||||||
__copyright__ = "Copyright (C) 2022-2023 NetInvent"
|
__copyright__ = "Copyright (C) 2022-2023 NetInvent"
|
||||||
__license__ = "GPL-3.0-only"
|
__license__ = "GPL-3.0-only"
|
||||||
__build__ = "2023121501"
|
__build__ = "2023122901"
|
||||||
__version__ = "2.0.0 for npbackup 2.3.0+"
|
__version__ = "2.0.0 for npbackup 2.3.0+"
|
||||||
|
|
||||||
CONF_VERSION = 2.3
|
MIN_CONF_VERSION = 2.3
|
||||||
|
MAX_CONF_VERSION = 2.3
|
||||||
|
|
||||||
from typing import Tuple, Optional, List, Any, Union
|
from typing import Tuple, Optional, List, Any, Union
|
||||||
import sys
|
import sys
|
||||||
|
@ -116,7 +117,7 @@ ENCRYPTED_OPTIONS = [
|
||||||
|
|
||||||
# This is what a config file looks like
|
# This is what a config file looks like
|
||||||
empty_config_dict = {
|
empty_config_dict = {
|
||||||
"conf_version": CONF_VERSION,
|
"conf_version": MAX_CONF_VERSION,
|
||||||
"repos": {
|
"repos": {
|
||||||
"default": {
|
"default": {
|
||||||
"repo_uri": "",
|
"repo_uri": "",
|
||||||
|
@ -481,12 +482,18 @@ def _load_config_file(config_file: Path) -> Union[bool, dict]:
|
||||||
with open(config_file, "r", encoding="utf-8") as file_handle:
|
with open(config_file, "r", encoding="utf-8") as file_handle:
|
||||||
yaml = YAML(typ="rt")
|
yaml = YAML(typ="rt")
|
||||||
full_config = yaml.load(file_handle)
|
full_config = yaml.load(file_handle)
|
||||||
|
if not full_config:
|
||||||
conf_version = full_config.g("conf_version")
|
logger.critical("Config file seems empty !")
|
||||||
if conf_version != CONF_VERSION:
|
return False
|
||||||
logger.critical(
|
try:
|
||||||
f"Config file version {conf_version} is not required version {CONF_VERSION}"
|
conf_version = float(full_config.g("conf_version"))
|
||||||
)
|
if conf_version < MIN_CONF_VERSION or conf_version > MAX_CONF_VERSION:
|
||||||
|
logger.critical(
|
||||||
|
f"Config file version {conf_version} is not required version min={MIN_CONF_VERSION}, max={MAX_CONF_VERSION}"
|
||||||
|
)
|
||||||
|
return False
|
||||||
|
except AttributeError:
|
||||||
|
logger.critical("Cannot read conf version from config file, which seems bogus")
|
||||||
return False
|
return False
|
||||||
return full_config
|
return full_config
|
||||||
except OSError:
|
except OSError:
|
||||||
|
|
Loading…
Add table
Reference in a new issue