Add environment output option (#152)

Add --env option for environment output
This commit is contained in:
Dave Wapstra 2021-01-17 22:20:14 +01:00 committed by GitHub
parent b337765114
commit 140ac2e79b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 4 deletions

View file

@ -538,7 +538,7 @@ ERROR - No patterns matched for file 'src/mymodule/utils.py'
$ bumpver --help
Usage: bumpver [OPTIONS] COMMAND [ARGS]...
Automatically update CalVer version strings in plaintext files.
Automatically update version strings in plaintext files.
Options:
--version Show the version and exit.
@ -591,6 +591,21 @@ Options:
<!-- END bumpver update --help -->
To help with shell script automation, you can use `bumpver show --env`.
```shell
$ bumpver show -n --env
YEAR_Y=2020
YEAR_G=
...
TAG=final
...
$ eval $(bumpver show -n --env)
$ echo $TAG
final
```
### Part Overview

View file

@ -189,6 +189,15 @@ fetch_option = click.option(
)
env_option = click.option(
"-e",
"--env",
is_flag=True,
default=False,
help="Print version state for use with shell scripts: eval $(bumpver show --env)",
)
def version_options(function: typ.Callable) -> typ.Callable:
decorators = [
click.option("--major", is_flag=True, default=False, help="Increment major component."),
@ -413,7 +422,8 @@ def grep(
@cli.command()
@verbose_option
@fetch_option
def show(verbose: int = 0, fetch: bool = True) -> None:
@env_option
def show(verbose: int = 0, fetch: bool = True, env: bool = False) -> None:
"""Show current version of your project."""
_configure_logging(verbose=max(_VERBOSE, verbose))
@ -424,8 +434,15 @@ def show(verbose: int = 0, fetch: bool = True) -> None:
sys.exit(1)
cfg = _update_cfg_from_vcs(cfg, fetch)
click.echo(f"Current Version: {cfg.current_version}")
click.echo(f"PEP440 : {cfg.pep440_version}")
if env:
version_info = v2version.parse_version_info(cfg.current_version, cfg.version_pattern)
for key, val in version_info._asdict().items():
click.echo(f"{key.upper()}={val if val else ''}")
click.echo(f"CURRENT_VERSION={cfg.current_version}")
click.echo(f"PEP440_VERSION={cfg.pep440_version}")
else:
click.echo(f"Current Version: {cfg.current_version}")
click.echo(f"PEP440 : {cfg.pep440_version}")
def _colored_diff_lines(diff: str) -> typ.Iterable[str]:

View file

@ -116,6 +116,17 @@ def test_version(runner):
assert match
def test_show_env(runner):
_add_project_files("README.md", "setup.cfg")
result = runner.invoke(cli.cli, ['init', "-vv"])
assert result.exit_code == 0
result = runner.invoke(cli.cli, ['show', "-e"])
assert result.exit_code == 0
assert "TAG=alpha" in result.output
def test_incr_default(runner):
old_version = "v201709.1004-alpha"