Skip to content
This repository has been archived by the owner on Dec 6, 2022. It is now read-only.

Commit

Permalink
Merge pull request #5 from kronostechnologies/magic-star
Browse files Browse the repository at this point in the history
add the posibility to use star as patch to get the latest one.
  • Loading branch information
Antoine Deschênes authored Apr 26, 2019
2 parents 3fba7bf + aead1a4 commit fca32de
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,16 @@ usage: kpin set [-h] ENVIRONMENT PROJECT [PROJECT ...]
positional arguments:
ENVIRONMENT environment pin to add
PROJECT project to pin: project@semver
PROJECT project to pin: project@x.x.x-x
You can use * as patch to get the latest patch. Can't be used with a pre release
optional arguments:
-h, --help show this help message and exit
```

Example:
```
$ kpin set my-prod a-project@1.0.0 another-project@2.0.3
$ kpin set my-prod a-project@1.0.0 another-project@2.0.3 another-other-project@2.1.*
```

### Show pins for one or many environments
Expand Down
49 changes: 41 additions & 8 deletions kpin
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ import semver

ProjectVersionArg = Tuple[str, str]

_VERSION_REGEX = re.compile(
r"""
^
(?P<major>(?:0|[1-9][0-9]*))
\.
(?P<minor>(?:0|[1-9][0-9]*))
\.
(?P<patch>(?:0|[1-9][0-9]*|\*))
(\-(?P<prerelease>
(?:0|[1-9][0-9]*)
))?
$
""", re.VERBOSE)

class Image:
__pin_prefix = 'pin-'
Expand Down Expand Up @@ -241,17 +254,36 @@ class Commands:
versions = repository.get_images_by_property('versions')
pins = repository.get_images_by_property('pins')

match = _VERSION_REGEX.match(version)
if match is None:
raise ValueError('%s is not valid version string' % version)

version_parts = match.groupdict()
try:
new_image = versions[version]
if(version_parts['patch'] == '*' and version_parts['prerelease'] is None):
matched_versions = []
for image_version, image in versions.items():
if semver.match(image_version, '>'+str(version_parts['major'])+'.'+str(version_parts['minor'])+'.0'):
if semver.match(image_version, '<'+str(version_parts['major'])+'.'+str((int(version_parts['minor'])+1))+'.0'):
matched_versions.append(image_version)

version_to_pin = '0.0.0'
for matched_version in matched_versions:
version_to_pin = semver.max_ver(version_to_pin,matched_version)
else:
version_to_pin = version

new_image = versions[version_to_pin]
new_image.add_pin(environment)
self.output.print_success('{}@{} set for {}!'.format(repository.name, version, environment))
self.output.print_success('{}@{} set for {}!'.format(repository.name, version_to_pin, environment))
except KeyError:
self.output.print_error("{}@{} skipped: version not found."
.format(repository.name, version))
.format(repository.name, version_to_pin))
continue
except ValueError:
except ValueError as e:
print(e)
self.output.print_error("{}@{} skipped: already set for {}."
.format(repository.name, version, environment))
.format(repository.name, version_to_pin, environment))
continue

try:
Expand All @@ -271,7 +303,7 @@ def project_version(arg: str) -> ProjectVersionArg:
project, version = arg.split('@')
if not re.compile('^[A-Za-z0-9_.\-]+$').fullmatch(project):
raise ValueError("{} is not a valid project name".format(project))
semver.parse(version)

return project, version


Expand Down Expand Up @@ -302,7 +334,7 @@ def main():
set_parser = subparsers.add_parser('set', help='set pins')
set_parser.set_defaults(func=lambda e: commands.set_pins(e.ENVIRONMENT, e.PROJECT))
set_parser.add_argument('ENVIRONMENT', help='environment pin to add')
set_parser.add_argument('PROJECT', nargs='+', type=project_version, help='project to pin: project@semver')
set_parser.add_argument('PROJECT', nargs='+', type=project_version, help='project to pin: project@x.x.x-x (You can use * as patch to get the latest patch. Can\'t be used with a pre release)')

list_parser = subparsers.add_parser('show', help='show pins')
list_parser.set_defaults(func=lambda e: commands.show_pins(e.ENVIRONMENT))
Expand All @@ -311,7 +343,8 @@ def main():
versions_parser = subparsers.add_parser('versions', help='list available versions', aliases=['list-versions'])
versions_parser.set_defaults(func=lambda p: commands.list_versions(p.PROJECT or None, p.latest))
versions_parser.add_argument('PROJECT', nargs='*')
versions_parser.add_argument('-l', '--latest', action='store_true', help='show only the latest open and closed versions')
versions_parser.add_argument('-l', '--latest', action='store_true',
help='show only the latest open and closed versions')

# show help without arguments
if not sys.argv[1:]:
Expand Down

0 comments on commit fca32de

Please sign in to comment.