Skip to content

Commit

Permalink
feat(cli): add config option to specify config file path
Browse files Browse the repository at this point in the history
Issue #656
  • Loading branch information
rockleona authored and Lee-W committed Apr 11, 2024
1 parent c183460 commit 5e2cc6c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
11 changes: 9 additions & 2 deletions commitizen/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ def __call__(
),
"formatter_class": argparse.RawDescriptionHelpFormatter,
"arguments": [
{
"name": "--config",
"help": "specify file path if config file is not in root folder",
},
{"name": "--debug", "action": "store_true", "help": "use debug mode"},
{
"name": ["-n", "--name"],
Expand Down Expand Up @@ -534,9 +538,7 @@ def parse_no_raise(comma_separated_no_raise: str) -> list[int]:


def main():
conf = config.read_cfg()
parser = cli(data)

argcomplete.autocomplete(parser)
# Show help if no arg provided
if len(sys.argv) == 1:
Expand Down Expand Up @@ -576,6 +578,11 @@ def main():
extra_args = " ".join(unknown_args[1:])
arguments["extra_cli_args"] = extra_args

if args.config:
conf = config.read_cfg(args.config)
else:
conf = config.read_cfg()

if args.name:
conf.update({"name": args.name})
elif not args.name and not conf.path:
Expand Down
24 changes: 23 additions & 1 deletion commitizen/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,39 @@
from pathlib import Path

from commitizen import defaults, git
from commitizen.exceptions import ConfigFileNotFound

from .base_config import BaseConfig
from .json_config import JsonConfig
from .toml_config import TomlConfig
from .yaml_config import YAMLConfig


def read_cfg() -> BaseConfig:
def read_cfg(filepath: str | None = None) -> BaseConfig:
conf = BaseConfig()

git_project_root = git.find_git_project_root()

if filepath is not None:
given_cfg_path = Path(filepath)

if not given_cfg_path.exists():
raise ConfigFileNotFound()

with open(given_cfg_path, "rb") as f:
given_cfg_data: bytes = f.read()

given_cfg: TomlConfig | JsonConfig | YAMLConfig

if "toml" in given_cfg_path.suffix:
given_cfg = TomlConfig(data=given_cfg_data, path=given_cfg_path)
elif "json" in given_cfg_path.suffix:
given_cfg = JsonConfig(data=given_cfg_data, path=given_cfg_path)
elif "yaml" in given_cfg_path.suffix:
given_cfg = YAMLConfig(data=given_cfg_data, path=given_cfg_path)

return given_cfg

cfg_search_paths = [Path(".")]
if git_project_root:
cfg_search_paths.append(git_project_root)
Expand Down
6 changes: 6 additions & 0 deletions commitizen/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class ExitCode(enum.IntEnum):
VERSION_PROVIDER_UNKNOWN = 27
VERSION_SCHEME_UNKNOWN = 28
CHANGELOG_FORMAT_UNKNOWN = 29
CONFIG_FILE_NOT_FOUND = 30


class CommitizenException(Exception):
Expand Down Expand Up @@ -189,3 +190,8 @@ class VersionSchemeUnknown(CommitizenException):
class ChangelogFormatUnknown(CommitizenException):
exit_code = ExitCode.CHANGELOG_FORMAT_UNKNOWN
message = "Unknown changelog format identifier"


class ConfigFileNotFound(CommitizenException):
exit_code = ExitCode.CONFIG_FILE_NOT_FOUND
message = "Cannot found the config file, please check your file path again."
10 changes: 10 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
NoCommandFoundError,
NotAGitProjectError,
InvalidCommandArgumentError,
ConfigFileNotFound,
)


Expand All @@ -25,6 +26,15 @@ def test_sysexit_no_argv(mocker: MockFixture, capsys):
assert out.startswith("usage")


def test_cz_config_file_without_correct_file_path(mocker: MockFixture, capsys):
testargs = ["cz", "--config", "./config/pyproject.toml", "example"]
mocker.patch.object(sys, "argv", testargs)

with pytest.raises(ConfigFileNotFound) as excinfo:
cli.main()
assert "Cannot found the config file" in str(excinfo.value)


def test_cz_with_arg_but_without_command(mocker: MockFixture):
testargs = ["cz", "--name", "cz_jira"]
mocker.patch.object(sys, "argv", testargs)
Expand Down

0 comments on commit 5e2cc6c

Please sign in to comment.