From 7f1ae584f54150a967067b37a1209aaacb57a8de Mon Sep 17 00:00:00 2001 From: taleksovska Date: Tue, 14 May 2024 13:21:23 +0200 Subject: [PATCH] Add semantic versioning --- pyproject.toml | 2 +- setup.py | 2 +- .../commands/cmd_version.py | 5 ++++- src/enabler_keitaro_inc/type/semver.py | 22 ++++++++++--------- tests/version_unittests.py | 4 ++-- 5 files changed, 20 insertions(+), 15 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 56d90f9..ba23b11 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "Enabler" # noqa -version = "0.1" # noqa +version = "0.1.0" # noqa description = "Enabler is a CLI application built for making life easier when working on microservice-based applications. Through this package we can create, edit and execute custom commands to configure microservices." # noqa authors = ["Your Name "] # noqa diff --git a/setup.py b/setup.py index c9c6466..7d9cf8b 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name="enabler", - version="0.1", + version="0.1.0", packages=["enabler", "src.enabler_keitaro_inc.commands", "src.enabler_keitaro_inc.helpers"], # noqa include_package_data=True, install_requires=["click==7.1.1", diff --git a/src/enabler_keitaro_inc/commands/cmd_version.py b/src/enabler_keitaro_inc/commands/cmd_version.py index 7f5c176..836809d 100644 --- a/src/enabler_keitaro_inc/commands/cmd_version.py +++ b/src/enabler_keitaro_inc/commands/cmd_version.py @@ -1,6 +1,7 @@ from src.enabler_keitaro_inc.enabler import pass_environment, logger import pkg_resources import click +from src.enabler_keitaro_inc.type.semver import BasedVersionParamType # Command to get the current Enabler version @click.group('version', short_help='Get current version of Enabler', invoke_without_command=True) # noqa @@ -10,4 +11,6 @@ def cli(ctx, kube_context_cli): """Get current version of Enabler""" distribution = pkg_resources.get_distribution("enabler") version = distribution.version - logger.info("Enabler "+version) + # Ensure the version string has three parts (major, minor, patch) + formatted_version = BasedVersionParamType().convert(version, None, None) + logger.info(f"Enabler {formatted_version}") diff --git a/src/enabler_keitaro_inc/type/semver.py b/src/enabler_keitaro_inc/type/semver.py index 617edd3..7486cda 100644 --- a/src/enabler_keitaro_inc/type/semver.py +++ b/src/enabler_keitaro_inc/type/semver.py @@ -8,13 +8,15 @@ class BasedVersionParamType(click.ParamType): def convert(self, value, param, ctx): try: parse(value) - return (value) - except TypeError: - self.fail( - '{value!r} is not a valid version, please use semver', - param, - ctx, - ) - except ValueError: - self.fail(f'{value!r} is not a valid version, please use semver', - param, ctx) + return value + except (TypeError, ValueError): + parts = value.split('.') + if len(parts) == 2: + parts.append('0') # Default patch version to 0 if not provided + elif len(parts) == 3: + # Increment patch version for bug fixes + parts[2] = str(int(parts[2]) + 1) + else: + self.fail(f'{value!r} is not a valid version, please use semver', param, ctx) # noqa + + return '.'.join(parts) diff --git a/tests/version_unittests.py b/tests/version_unittests.py index f717490..eb305e9 100644 --- a/tests/version_unittests.py +++ b/tests/version_unittests.py @@ -9,7 +9,7 @@ def setUp(self): @patch('src.enabler_keitaro_inc.enabler.subprocess.run') def test_version_command(self, mock_subprocess_run): - mock_subprocess_run.return_value.stdout = 'Enabler 0.1' + mock_subprocess_run.return_value.stdout = 'Enabler 0.1.0' version = self.cli.version_command() - expected_version = 'Enabler 0.1' + expected_version = 'Enabler 0.1.0' self.assertEqual(version, expected_version)