1use crate::download::get_archive_file;
2use crate::GoLanguage;
3use proto_core::{
4 async_trait, get_sha256_hash_of_file, Describable, ProtoError, Resolvable, Verifiable,
5};
6use starbase_utils::fs;
7use std::io::{BufRead, BufReader};
8use std::path::{Path, PathBuf};
9use tracing::debug;
10
11#[async_trait]
12impl Verifiable<'_> for GoLanguage {
13 fn get_checksum_path(&self) -> Result<PathBuf, ProtoError> {
14 Ok(self
15 .temp_dir
16 .join(format!("v{}-SHASUMS256.txt", self.get_resolved_version())))
17 }
18
19 fn get_checksum_url(&self) -> Result<Option<String>, ProtoError> {
20 let version = self.get_resolved_version();
21
22 let version = match version.strip_suffix(".0") {
23 Some(s) => s,
24 None => version,
25 };
26
27 Ok(Some(format!(
28 "https://dl.google.com/go/{}.sha256",
29 get_archive_file(version)?
30 )))
31 }
32
33 async fn verify_checksum(
34 &self,
35 checksum_file: &Path,
36 download_file: &Path,
37 ) -> Result<bool, ProtoError> {
38 debug!(
39 tool = self.get_id(),
40 download_file = ?download_file,
41 checksum_file = ?checksum_file,
42 "Verifiying checksum of downloaded file"
43 );
44
45 let checksum = get_sha256_hash_of_file(download_file)?;
46 let file = fs::open_file(checksum_file)?;
47
48 for line in BufReader::new(file).lines().flatten() {
49 if line.starts_with(&checksum) {
50 debug!(
51 tool = self.get_id(),
52 "Successfully verified, checksum matches"
53 );
54
55 return Ok(true);
56 }
57 }
58
59 Err(ProtoError::VerifyInvalidChecksum(
60 download_file.to_path_buf(),
61 checksum_file.to_path_buf(),
62 ))
63 }
64}