diff --git a/src/args.rs b/src/args.rs index 6f0f983..f8c3de8 100644 --- a/src/args.rs +++ b/src/args.rs @@ -50,6 +50,8 @@ pub enum RuntipiMainCommand { Update(UpdateCommand), /// Manage your apps App(AppCommand), + /// Update the app store repository + RepoUpdate, /// Debug your runtipi instance Debug, } diff --git a/src/commands/mod.rs b/src/commands/mod.rs index c8dfe94..201f658 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -3,3 +3,4 @@ pub mod debug; pub mod start; pub mod stop; pub mod update; +pub mod repo_update; \ No newline at end of file diff --git a/src/commands/repo_update.rs b/src/commands/repo_update.rs new file mode 100644 index 0000000..1e1a068 --- /dev/null +++ b/src/commands/repo_update.rs @@ -0,0 +1,68 @@ +use crate::components::spinner; +use crate::utils::env; +use std::env::set_current_dir; +use std::path::Path; + +pub fn run() { + let spin = spinner::new(""); + + spin.set_message("Updating app store repository..."); + std::thread::sleep(std::time::Duration::from_millis(500)); + + spin.set_message("Finding repository from .env"); + let repo_id = env::get_env_value("APPS_REPO_ID").unwrap_or("29ca930bfdaffa1dfabf5726336380ede7066bc53297e3c0c868b27c97282903".to_string()); + let path = format!("repos/{}", repo_id); + let repo_path = Path::new(&path); + set_current_dir(&repo_path).unwrap(); + spin.succeed("Repository found"); + + spin.set_message("Resetting git repository..."); + let reset_command = std::process::Command::new("git").arg("reset").args(["--hard"]).output().map_err(|e| e.to_string()); + + match reset_command { + Ok(output) => { + if !output.status.success() { + spin.fail("Failed to reset repository. Please check the permissions."); + spin.finish(); + + let stderr = String::from_utf8_lossy(&output.stderr); + println!("\nDebug: {}", stderr); + return; + } + } + Err(e) => { + spin.fail("Failed to reset repository. Please check the permissions."); + spin.finish(); + + println!("\nDebug: {}", e); + return; + } + } + spin.succeed("Repository reset"); + + spin.set_message("Pulling updates..."); + let pull_command = std::process::Command::new("git").arg("pull").output().map_err(|e| e.to_string()); + + match pull_command { + Ok(output) => { + if !output.status.success() { + spin.fail("Failed to git pull. Please check the permissions."); + spin.finish(); + + let stderr = String::from_utf8_lossy(&output.stderr); + println!("\nDebug: {}", stderr); + return; + } + } + Err(e) => { + spin.fail("Failed to git pull. Please check the permissions."); + spin.finish(); + + println!("\nDebug: {}", e); + return; + } + } + spin.succeed("Updates pulled"); + + spin.succeed("Successfully updated app store repository!") +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 80e3c5c..29d018f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ use crate::commands::update::UpdateArgs; fn main() { let args = RuntipiArgs::parse(); - println!("{}", "Welcome to Runtipi CLI ✨\n".green()); + println!("{}", "Welcome to Runtipi CLI ✨".green()); match args.command { args::RuntipiMainCommand::Start(args) => { @@ -38,6 +38,9 @@ fn main() { args::RuntipiMainCommand::App(app_command) => { commands::app::run(app_command); } + args::RuntipiMainCommand::RepoUpdate => { + commands::repo_update::run(); + } args::RuntipiMainCommand::Debug => { commands::debug::run(); }