@@ -804,61 +804,78 @@ def update_sb(sb_repo_path):
804804 print (f"Error: { sb_sh_path } does not exist or is not a file." )
805805 sys .exit (1 )
806806
807- # Hardcoded paths
808- release_file_path = os .path .join (sb_repo_path , 'release.txt' )
809- target_binary_path = os .path .join (sb_repo_path , 'sb' )
807+ # Target binary path
808+ target_binary_path = "/usr/local/bin/sb"
810809
811- # Read the release.txt file to get the GitHub tag
812- if not os .path .isfile (release_file_path ):
813- print (f"Error: { release_file_path } does not exist." )
814- sys .exit (1 )
810+ # Fetch the latest release from sb-go repository
811+ api_url = "https://api.github.com/repos/saltyorg/sb-go/releases/latest"
815812
816- with open (release_file_path , 'r' ) as release_file :
817- github_tag = release_file .readline ().strip ()
813+ try :
814+ response = requests .get (api_url )
815+ response .raise_for_status ()
816+ latest_release = response .json ()
818817
819- # Extract the version number from the tag
820- if not github_tag .startswith ('refs/tags/' ):
821- print (f"Error: Invalid tag format in { release_file_path } ." )
822- sys .exit (1 )
818+ # Extract the version tag
819+ version = latest_release .get ('tag_name' )
820+ if not version :
821+ print ("Error: Could not determine latest version from GitHub API." )
822+ sys .exit (1 )
823823
824- version = github_tag [len ('refs/tags/' ):]
825- if not version :
826- print (f"Error: No version found in tag { github_tag } ." )
827- sys .exit (1 )
824+ print (f"Latest sb-go version: { version } " )
828825
829- # Form the URL for the binary download
830- download_url = f"https://github.com/saltyorg/sb/releases/download/{ version } /sb"
826+ # Find the appropriate asset for the current platform
827+ # Currently sb-go only has sb_linux_amd64
828+ asset_name = "sb_linux_amd64"
831829
832- # Download the binary file
833- response = requests .get (download_url )
834- if response .status_code != 200 :
835- print (f"Error: Failed to download the binary from { download_url } ." )
836- sys .exit (1 )
830+ assets = latest_release .get ('assets' , [])
831+ download_url = None
837832
838- # Save the downloaded binary to a temporary file
839- temp_binary_path = target_binary_path + '.tmp'
840- with open (temp_binary_path , 'wb' ) as temp_binary_file :
841- temp_binary_file .write (response .content )
842-
843- # Check if the downloaded file is a binary
844- mime = magic .Magic (mime = True )
845- file_type = mime .from_file (temp_binary_path )
846- if not file_type .startswith ('application/' ):
847- print (f"Error: Downloaded file is not a binary. "
848- f"Detected type: { file_type } " )
849- os .remove (temp_binary_path )
850- sys .exit (1 )
833+ for asset in assets :
834+ if asset .get ('name' ) == asset_name :
835+ download_url = asset .get ('browser_download_url' )
836+ break
851837
852- # Replace the old binary with the new one
853- if os .path .isfile (temp_binary_path ):
854- os .replace (temp_binary_path , target_binary_path )
855- print (f"Updated binary at { target_binary_path } ." )
838+ if not download_url :
839+ print (f"Error: Could not find asset '{ asset_name } ' in release { version } ." )
840+ sys .exit (1 )
856841
857- # Ensure the new binary is executable
858- os .chmod (target_binary_path , 0o755 )
859- print (f"Permissions changed for { target_binary_path } to be executable." )
860- else :
861- print (f"Error: Failed to write the new binary to { temp_binary_path } ." )
842+ print (f"Downloading { asset_name } from { download_url } " )
843+
844+ # Download the binary file
845+ response = requests .get (download_url )
846+ response .raise_for_status ()
847+
848+ # Save the downloaded binary to a temporary file
849+ temp_binary_path = target_binary_path + '.tmp'
850+ with open (temp_binary_path , 'wb' ) as temp_binary_file :
851+ temp_binary_file .write (response .content )
852+
853+ # Check if the downloaded file is a binary
854+ mime = magic .Magic (mime = True )
855+ file_type = mime .from_file (temp_binary_path )
856+ if not file_type .startswith ('application/' ):
857+ print (f"Error: Downloaded file is not a binary. "
858+ f"Detected type: { file_type } " )
859+ os .remove (temp_binary_path )
860+ sys .exit (1 )
861+
862+ # Replace the old binary with the new one
863+ if os .path .isfile (temp_binary_path ):
864+ os .replace (temp_binary_path , target_binary_path )
865+ print (f"Updated binary at { target_binary_path } to version { version } ." )
866+
867+ # Ensure the new binary is executable
868+ os .chmod (target_binary_path , 0o755 )
869+ print (f"Permissions changed for { target_binary_path } to be executable." )
870+ else :
871+ print (f"Error: Failed to write the new binary to { temp_binary_path } ." )
872+ sys .exit (1 )
873+
874+ except requests .RequestException as e :
875+ print (f"Error downloading sb-go binary: { e } " )
876+ sys .exit (1 )
877+ except Exception as e :
878+ print (f"Unexpected error updating sb-go binary: { e } " )
862879 sys .exit (1 )
863880
864881
0 commit comments