Skip to content

Commit 3c6bade

Browse files
committed
refactor(cli): load configs via ConfigReader
1 parent 368f4c3 commit 3c6bade

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

src/vcspull/cli/add.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
import traceback
88
import typing as t
99

10-
import yaml
1110
from colorama import Fore, Style
1211

12+
from vcspull._internal.config_reader import ConfigReader
1313
from vcspull.config import find_home_config_files, save_config_yaml
1414

1515
if t.TYPE_CHECKING:
@@ -95,19 +95,23 @@ def add_repo(
9595
raw_config: dict[str, t.Any] = {}
9696
if config_file_path.exists() and config_file_path.is_file():
9797
try:
98-
with config_file_path.open(encoding="utf-8") as f:
99-
raw_config = yaml.safe_load(f) or {}
100-
if not isinstance(raw_config, dict):
101-
log.error(
102-
"Config file %s is not a valid YAML dictionary. ",
103-
config_file_path,
104-
)
105-
return
98+
loaded_config = ConfigReader._from_file(config_file_path)
10699
except Exception:
107100
log.exception("Error loading YAML from %s. Aborting.", config_file_path)
108101
if log.isEnabledFor(logging.DEBUG):
109102
traceback.print_exc()
110103
return
104+
105+
if loaded_config is None:
106+
raw_config = {}
107+
elif isinstance(loaded_config, dict):
108+
raw_config = loaded_config
109+
else:
110+
log.error(
111+
"Config file %s is not a valid YAML dictionary.",
112+
config_file_path,
113+
)
114+
return
111115
else:
112116
log.info(
113117
"Config file %s not found. A new one will be created.",

src/vcspull/cli/add_from_fs.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
import traceback
1010
import typing as t
1111

12-
import yaml
1312
from colorama import Fore, Style
1413

14+
from vcspull._internal.config_reader import ConfigReader
1515
from vcspull.config import expand_dir, find_home_config_files, save_config_yaml
1616

1717
if t.TYPE_CHECKING:
@@ -134,19 +134,23 @@ def add_from_filesystem(
134134
raw_config: dict[str, t.Any] = {}
135135
if config_file_path.exists() and config_file_path.is_file():
136136
try:
137-
with config_file_path.open(encoding="utf-8") as f:
138-
raw_config = yaml.safe_load(f) or {}
139-
if not isinstance(raw_config, dict):
140-
log.error(
141-
"Config file %s is not a valid YAML dictionary. ",
142-
config_file_path,
143-
)
144-
return
137+
loaded_config = ConfigReader._from_file(config_file_path)
145138
except Exception:
146139
log.exception("Error loading YAML from %s. Aborting.", config_file_path)
147140
if log.isEnabledFor(logging.DEBUG):
148141
traceback.print_exc()
149142
return
143+
144+
if loaded_config is None:
145+
raw_config = {}
146+
elif isinstance(loaded_config, dict):
147+
raw_config = loaded_config
148+
else:
149+
log.error(
150+
"Config file %s is not a valid YAML dictionary.",
151+
config_file_path,
152+
)
153+
return
150154
else:
151155
log.info(
152156
"%si%s Config file %s%s%s not found. A new one will be created.",

0 commit comments

Comments
 (0)