1use std::{
2 env, fs,
3 process::{Command, Stdio},
4};
5
6use anyhow::Error;
7use envhub_types::configuration::Configuration;
8
9use crate::Extension;
10
11pub struct Rtx {}
12
13impl Rtx {
14 pub fn new() -> Self {
15 Self {}
16 }
17
18 pub fn install(&self, package: &str) -> Result<(), Error> {
19 let mut child = Command::new("sh")
20 .arg("-c")
21 .arg(format!("rtx install {}", package))
22 .stdin(Stdio::inherit())
23 .stdout(Stdio::inherit())
24 .stderr(Stdio::inherit())
25 .spawn()?;
26 child.wait()?;
27 Ok(())
28 }
29}
30
31impl Extension for Rtx {
32 fn load(&self, config: &Configuration) -> Result<(), Error> {
33 self.setup()?;
34 match config.rtx {
35 Some(ref rtx) => {
36 for package in &rtx.packages {
37 self.install(package)?;
38 }
39 }
40 None => {}
41 }
42 Ok(())
43 }
44
45 fn setup(&self) -> Result<(), Error> {
46 env::set_var(
47 "PATH",
48 format!(
49 "{}/.local/share/rtx/bin:{}",
50 env::var("HOME")?,
51 env::var("PATH")?
52 ),
53 );
54 env::set_var(
55 "PATH",
56 format!(
57 "{}/.local/share/rtx/shims:{}",
58 env::var("HOME")?,
59 env::var("PATH")?
60 ),
61 );
62 let mut child = Command::new("sh")
63 .arg("-c")
64 .arg("type rtx > /dev/null || curl https://rtx.pub/install.sh | sh")
65 .stdin(Stdio::inherit())
66 .stdout(Stdio::inherit())
67 .stderr(Stdio::inherit())
68 .spawn()?;
69 child.wait()?;
70
71 fs::create_dir_all(format!("{}/.local/share/rtx/shims", env::var("HOME")?))?;
72
73 Ok(())
74 }
75}