Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(commands): add a repository update command #20

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
1 change: 1 addition & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pub mod debug;
pub mod start;
pub mod stop;
pub mod update;
pub mod repo_update;
68 changes: 68 additions & 0 deletions src/commands/repo_update.rs
Original file line number Diff line number Diff line change
@@ -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));
steveiliop56 marked this conversation as resolved.
Show resolved Hide resolved

spin.set_message("Finding repository from .env");
let repo_id = env::get_env_value("APPS_REPO_ID").unwrap_or("29ca930bfdaffa1dfabf5726336380ede7066bc53297e3c0c868b27c97282903".to_string());
nicotsx marked this conversation as resolved.
Show resolved Hide resolved
let path = format!("repos/{}", repo_id);
let repo_path = Path::new(&path);
set_current_dir(&repo_path).unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace unwrap() with proper error handling to avoid potential panics if changing the current directory fails.

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!")
}
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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();
}
Expand Down
Loading