|
3 | 3 | from pathlib import Path |
4 | 4 |
|
5 | 5 | from commitizen import defaults, git |
| 6 | +from commitizen.exceptions import ConfigFileNotFound |
6 | 7 |
|
7 | 8 | from .base_config import BaseConfig |
8 | 9 | from .json_config import JsonConfig |
9 | 10 | from .toml_config import TomlConfig |
10 | 11 | from .yaml_config import YAMLConfig |
11 | 12 |
|
12 | 13 |
|
13 | | -def read_cfg() -> BaseConfig: |
| 14 | +def read_cfg(filepath: str | None = None) -> BaseConfig: |
14 | 15 | conf = BaseConfig() |
15 | 16 |
|
16 | 17 | git_project_root = git.find_git_project_root() |
| 18 | + |
| 19 | + if filepath is not None: |
| 20 | + given_cfg_path = Path(filepath) |
| 21 | + |
| 22 | + if not given_cfg_path.exists(): |
| 23 | + raise ConfigFileNotFound() |
| 24 | + |
| 25 | + with open(given_cfg_path, "rb") as f: |
| 26 | + given_cfg_data: bytes = f.read() |
| 27 | + |
| 28 | + given_cfg: TomlConfig | JsonConfig | YAMLConfig |
| 29 | + |
| 30 | + if "toml" in given_cfg_path.suffix: |
| 31 | + given_cfg = TomlConfig(data=given_cfg_data, path=given_cfg_path) |
| 32 | + elif "json" in given_cfg_path.suffix: |
| 33 | + given_cfg = JsonConfig(data=given_cfg_data, path=given_cfg_path) |
| 34 | + elif "yaml" in given_cfg_path.suffix: |
| 35 | + given_cfg = YAMLConfig(data=given_cfg_data, path=given_cfg_path) |
| 36 | + |
| 37 | + return given_cfg |
| 38 | + |
17 | 39 | cfg_search_paths = [Path(".")] |
18 | 40 | if git_project_root: |
19 | 41 | cfg_search_paths.append(git_project_root) |
|
0 commit comments