Skip to main content

idt/installers/
shellcheck.rs

1use std::path::Path;
2
3use rootcause::prelude::ResultExt as _;
4use ytil_sys::Arch;
5use ytil_sys::Os;
6use ytil_sys::SysInfo;
7
8use crate::downloaders::http::deflate::HttpDeflateOption;
9use crate::installers::Installer;
10use crate::installers::SystemDependent;
11
12pub struct Shellcheck<'a> {
13    pub bin_dir: &'a Path,
14    pub sys_info: &'a SysInfo,
15}
16
17impl SystemDependent for Shellcheck<'_> {
18    fn target_arch_and_os(&self) -> (&str, &str) {
19        let SysInfo { os, arch } = self.sys_info;
20        let os = match os {
21            Os::MacOs => "darwin",
22            Os::Linux => "linux",
23        };
24        let arch = match arch {
25            Arch::Arm => "aarch64",
26            Arch::X86 => "x86_64",
27        };
28        (arch, os)
29    }
30}
31
32impl Installer for Shellcheck<'_> {
33    fn bin_name(&self) -> &'static str {
34        "shellcheck"
35    }
36
37    fn should_verify_checksum(&self) -> bool {
38        false
39    }
40
41    fn install(&self) -> rootcause::Result<()> {
42        let (arch, os) = self.target_arch_and_os();
43
44        let repo = format!("koalaman/{}", self.bin_name());
45        let latest_release = crate::downloaders::http::github::get_latest_release_tag(&repo)?;
46        let dest_dir = Path::new("/tmp");
47
48        crate::downloaders::http::run(
49            &format!(
50                "https://github.com/{repo}/releases/download/{latest_release}/{}-{latest_release}.{os}.{arch}.tar.xz",
51                self.bin_name()
52            ),
53            &HttpDeflateOption::ExtractTarXz {
54                dest_dir,
55                dest_name: None,
56            },
57            None,
58        )?;
59
60        let target = self.bin_dir.join(self.bin_name());
61        let extracted = dest_dir
62            .join(format!("{0}-{latest_release}", self.bin_name()))
63            .join(self.bin_name());
64        std::fs::rename(&extracted, &target)
65            .context("error moving extracted shellcheck binary")
66            .attach_with(|| format!("from={}", extracted.display()))
67            .attach_with(|| format!("to={}", target.display()))?;
68        ytil_sys::file::chmod_x(target)?;
69
70        Ok(())
71    }
72}