Skip to content

Commit

Permalink
fix/handle-invalid-version
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanpulver committed Sep 27, 2024
1 parent 53c2de8 commit 083c921
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions safety/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from functools import wraps
from typing import Dict, Optional
from packaging import version as packaging_version
from packaging.version import InvalidVersion

import click
import typer
Expand Down Expand Up @@ -777,20 +778,25 @@ def check_updates(ctx: typer.Context,
console.print()

if latest_available_version:
# Compare the current version and the latest available version
if packaging_version.parse(latest_available_version) > packaging_version.parse(VERSION):
console.print(f"Update available: Safety version {latest_available_version}")
console.print()
console.print(
f"If Safety was installed from a requirements file, update Safety to version {latest_available_version} in that requirements file."
)
console.print()
console.print(f"Pip: To install the updated version of Safety directly via pip, run: pip install safety=={latest_available_version}")
elif packaging_version.parse(latest_available_version) < packaging_version.parse(VERSION):
# Notify user about downgrading
console.print(f"Latest stable version is {latest_available_version}. If you want to downgrade to this version, you can run: pip install safety=={latest_available_version}")
else:
console.print("You are already using the latest stable version of Safety.")
try:
# Compare the current version and the latest available version using packaging.version
if packaging_version.parse(latest_available_version) > packaging_version.parse(VERSION):
console.print(f"Update available: Safety version {latest_available_version}")
console.print()
console.print(
f"If Safety was installed from a requirements file, update Safety to version {latest_available_version} in that requirements file."
)
console.print()
console.print(f"Pip: To install the updated version of Safety directly via pip, run: pip install safety=={latest_available_version}")
elif packaging_version.parse(latest_available_version) < packaging_version.parse(VERSION):
# Notify user about downgrading
console.print(f"Latest stable version is {latest_available_version}. If you want to downgrade to this version, you can run: pip install safety=={latest_available_version}")
else:
console.print("You are already using the latest stable version of Safety.")
except InvalidVersion as invalid_version:
LOG.exception(f'Invalid version format encountered: {invalid_version}')
console.print(f"Error: Invalid version format encountered for the latest available version: {latest_available_version}")
console.print(f"Please report this issue or try again later.")

if console.quiet:
console.quiet = False
Expand Down

0 comments on commit 083c921

Please sign in to comment.