99import typer
1010from pathlib import Path
1111from core .utils import current_version , console
12+ from core .config import get_config_value
1213
1314GITHUB_REPO = "Portabase/cli"
14- GITHUB_API_URL = f"https://api.github.com/repos/{ GITHUB_REPO } /releases/latest "
15+ GITHUB_API_BASE_URL = f"https://api.github.com/repos/{ GITHUB_REPO } /releases"
1516CACHE_FILE = Path .home () / ".portabase" / "update_cache.json"
1617
18+ def is_prerelease (version : str ) -> bool :
19+ v = version .lower ()
20+ return any (x in v for x in ['a' , 'b' , 'rc' , 'alpha' , 'beta' ])
21+
1722def get_platform_info ():
1823 system = platform .system ().lower ()
1924 if system == "darwin" :
@@ -28,11 +33,17 @@ def get_platform_info():
2833
2934 return system , arch
3035
31- def get_latest_release_data ():
36+ def get_latest_release_data (pre = False ):
3237 try :
33- response = requests .get (GITHUB_API_URL , timeout = 5 )
34- response .raise_for_status ()
35- return response .json ()
38+ if not pre :
39+ response = requests .get (f"{ GITHUB_API_BASE_URL } /latest" , timeout = 5 )
40+ response .raise_for_status ()
41+ return response .json ()
42+ else :
43+ response = requests .get (GITHUB_API_BASE_URL , timeout = 5 )
44+ response .raise_for_status ()
45+ releases = response .json ()
46+ return releases [0 ] if releases else None
3647 except Exception :
3748 return None
3849
@@ -41,6 +52,15 @@ def check_for_updates(force=False):
4152 return None
4253
4354 current = current_version ()
55+ if current == "unknown" :
56+ return None
57+
58+ channel = get_config_value ("update_channel" )
59+ if channel :
60+ include_pre = (channel == "beta" )
61+ else :
62+ include_pre = is_prerelease (current )
63+
4464 latest_tag = None
4565
4666 try :
@@ -54,7 +74,7 @@ def check_for_updates(force=False):
5474 pass
5575
5676 if latest_tag is None :
57- data = get_latest_release_data ()
77+ data = get_latest_release_data (pre = include_pre )
5878 if data :
5979 latest_tag = data .get ("tag_name" , "" ).lstrip ('v' )
6080 try :
@@ -63,7 +83,7 @@ def check_for_updates(force=False):
6383 except Exception :
6484 pass
6585
66- if not latest_tag or current == "unknown" :
86+ if not latest_tag :
6787 return None
6888
6989 if latest_tag != current :
@@ -78,28 +98,35 @@ def update_cli():
7898 console .print ("[info]If you installed via source, please use [bold]git pull[/bold] to update.[/info]" )
7999 return
80100
81- data = get_latest_release_data ()
101+ current = current_version ()
102+
103+ channel = get_config_value ("update_channel" )
104+ if channel :
105+ pre = (channel == "beta" )
106+ else :
107+ pre = is_prerelease (current ) if current != "unknown" else False
108+
109+ data = get_latest_release_data (pre = pre )
82110 if not data :
83111 console .print ("[danger]✖ Could not fetch latest release data from GitHub.[/danger]" )
84112 return
85113
86114 latest_tag = data .get ("tag_name" , "" ).lstrip ('v' )
87- current = current_version ()
88115
89116 if latest_tag == current :
90117 console .print (f"[success]✔ Portabase CLI is already up to date ({ current } ).[/success]" )
91118 return
92119
93120 try :
94- if latest_tag < current and not (".rc" in current and not ".rc" in latest_tag ):
121+ if latest_tag < current and not (is_prerelease ( current ) and not is_prerelease ( latest_tag ) ):
95122 console .print (f"[warning]⚠ Latest remote version ({ latest_tag } ) appears to be older than current ({ current } ).[/warning]" )
96123 if not typer .confirm ("Do you want to continue with the update (downgrade)?" ):
97124 return
98125 except Exception :
99126 pass
100127
101128 system , arch = get_platform_info ()
102- asset_name = f"portabase_{ system } _ { arch } "
129+ asset_name = f"portabase_{{ system}}_{{ arch} }"
103130 if system == "windows" :
104131 asset_name += ".exe"
105132
@@ -162,4 +189,4 @@ def update_cli():
162189 except Exception as e :
163190 console .print (f"[danger]✖ An error occurred during update: { e } [/danger]" )
164191 if 'temp_file' in locals () and temp_file .exists ():
165- temp_file .unlink ()
192+ temp_file .unlink ()
0 commit comments