Skip to main content

idt/installers/
opencode.rs

1use std::path::Path;
2
3use ytil_sys::Arch;
4use ytil_sys::Os;
5use ytil_sys::SysInfo;
6
7use crate::downloaders::http::deflate::HttpDeflateOption;
8use crate::installers::Installer;
9use crate::installers::SystemDependent;
10
11pub struct Opencode<'a> {
12    pub bin_dir: &'a Path,
13    pub sys_info: &'a SysInfo,
14}
15
16impl SystemDependent for Opencode<'_> {
17    fn target_arch_and_os(&self) -> (&str, &str) {
18        let SysInfo { os, arch } = self.sys_info;
19        let os = match os {
20            Os::MacOs => "darwin",
21            Os::Linux => "linux",
22        };
23        let arch = match arch {
24            Arch::Arm => "arm64",
25            Arch::X86 => "x64",
26        };
27        (arch, os)
28    }
29}
30
31impl Installer for Opencode<'_> {
32    fn bin_name(&self) -> &'static str {
33        "opencode"
34    }
35
36    fn should_verify_checksum(&self) -> bool {
37        false
38    }
39
40    fn install(&self) -> rootcause::Result<()> {
41        let repo = format!("anomalyco/{}", self.bin_name());
42        let latest_release = crate::downloaders::http::github::get_latest_release_tag(&repo)?;
43
44        let (arch, os) = self.target_arch_and_os();
45
46        let (filename, deflate_option) = match self.sys_info.os {
47            Os::MacOs => (
48                format!("{}-{os}-{arch}.zip", self.bin_name()),
49                HttpDeflateOption::ExtractZip {
50                    dest_dir: self.bin_dir,
51                    dest_name: Some(self.bin_name()),
52                },
53            ),
54            Os::Linux => (
55                format!("{}-{os}-{arch}.tar.gz", self.bin_name()),
56                HttpDeflateOption::ExtractTarGz {
57                    dest_dir: self.bin_dir,
58                    dest_name: Some(self.bin_name()),
59                },
60            ),
61        };
62
63        let target = crate::downloaders::http::run(
64            &format!("https://github.com/{repo}/releases/download/{latest_release}/{filename}"),
65            &deflate_option,
66            None,
67        )?;
68
69        ytil_sys::file::chmod_x(target)?;
70
71        Ok(())
72    }
73}