mbarkhau / bumpver

BumpVer: Automatic Versioning
https://github.com/mbarkhau/bumpver
MIT License
199 stars 36 forks source link

CLI option to print only version to stdout with or without new line for scripts #167

Closed ianjosephwilson closed 3 years ago

ianjosephwilson commented 3 years ago

I saw that there is a way to print environment variables right now but is there a way to just print the version to stdout?

For example (using version format, using pep440 format, using version format but not including newline):

$ ve/bin/bumpver version
v202110.0005-alpha
$ ve/bin/bumpver version --pep440
202110.5a0
$ echo "My version is `ve/bin/bumpver version -n`."
My version is v202110.0005-alpha.

I do something like this now to parse the version from the output and then pass it into ansible.

    res = check_output(['./ve/bin/bumpver', 'show'], text=True)
    version = [line.split(":")[1].strip() for line in res.splitlines() if "PEP440" in line][0].strip()
mbarkhau commented 3 years ago

I'm guessing you mean bumpver show (as is shown in your ansible script).

You might do something like this

$ eval $(bumpver show -n --env); echo $CURRENT_VERSION
2021.1009
ianjosephwilson commented 3 years ago

I guess I meant more to just make a parallel command since show behaves so much differently. Sorry its a feature request. I run python scripts for deployment, building and other tasks. I'm not sure how I would eval the output without just parsing it in Python. Maybe I should call into the library directly instead?

mbarkhau commented 3 years ago

I wouldn't bother trying to call the library directly.

Instead of a new subcommand, how about a new parameter for show like this:

$ bumpver show --var CURRENT_VERSION
2021.1009
ianjosephwilson commented 3 years ago

Just to clarify, from an API perspective, this specifies a variable/param that is available via bumpver show -n --env. In this example that variable/param is CURRENT_VERSION. In my specific case my CURRENT_VERSION is v202110.0005-alpha based on my toml file version specs. I would want to access bumpver show --var PEP440_VERSION, ie. 202110.5a0.

I'm not super familiar with command line scripts because I do mostly webdev but it seems that when calling a script like this the newline would be dropped so the version could be accessed directly but when calling interactively we'd want the newline otherwise the output seems garbled in the shell. So like echo we'd want a -n option.

mbarkhau commented 3 years ago

If you're invoking a subcommand anyway, I think all of this is not worth the overkill.

Something like this should work.

>>> from subprocess import check_output
>>> check_output(['bash', '-c', 'eval $(./ve/bin/bumpver show --no-fetch --env); echo -n $PEP440_VERSION'], text=True)
'2021.1002b0'