diff --git a/dev-packages/src/lib.rs b/dev-packages/src/lib.rs index 348deb0..6f957f9 100644 --- a/dev-packages/src/lib.rs +++ b/dev-packages/src/lib.rs @@ -1,4 +1,8 @@ -use bindings::exports::component::churn_tasks::task::Guest; +use std::io::{Read, Write}; + +use bindings::{ + component::churn_tasks::process::Process, exports::component::churn_tasks::task::Guest, +}; #[allow(warnings)] mod bindings; @@ -11,10 +15,61 @@ impl Guest for Component { } fn should_run() -> bool { - true + if !std::path::PathBuf::from("/usr/local/bin/starship").exists() { + return true; + }; + + if !std::path::PathBuf::from("/root/.bashrc").exists() { + return true; + }; + + if let Ok(content) = std::fs::read_to_string("/root/.bashrc") { + if !content.contains("starship") { + return true; + } + } + + false } fn execute() { println!("running dev-packages installation"); + + Process::new().run_process( + &[ + "bash", + "-c", + "curl -sS https://starship.rs/install.sh | sh -s -- --force", + ] + .into_iter() + .map(|i| i.to_string()) + .collect::>(), + ); + + if !std::path::PathBuf::from("/root/.bashrc").exists() { + if let Ok(content) = std::fs::read_to_string("/root/.bashrc") { + if !content.contains("starship") { + let mut bashrc = std::fs::File::options() + .append(true) + .open("/root/.bashrc") + .expect("failed to open bash"); + + bashrc + .write_all(r#"eval "$(starship init bash)"#.as_bytes()) + .expect("failed to write to bashrc file"); + } + } + } else { + let mut bashrc = std::fs::File::options() + .write(true) + .create(true) + .truncate(true) + .open("/root/.bashrc") + .expect("failed to open bash"); + + bashrc + .write_all(r#"eval "$(starship init bash)""#.as_bytes()) + .expect("failed to write to bashrc file"); + } } }