Skip to content

Commit 505b8f4

Browse files
committed
Merge branch 'wt/self-update' — add gitpane update command
2 parents e392daf + 7d432d8 commit 505b8f4

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

src/main.rs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ mod tui;
99
mod update_checker;
1010
mod watcher;
1111

12-
use clap::Parser;
12+
use clap::{Parser, Subcommand};
1313
use color_eyre::Result;
1414
use std::path::PathBuf;
1515

@@ -23,12 +23,27 @@ struct Cli {
2323
/// UI frame rate (deprecated — rendering is now on-demand)
2424
#[arg(long, default_value_t = 10, hide = true)]
2525
frame_rate: u16,
26+
27+
#[command(subcommand)]
28+
command: Option<Command>,
29+
}
30+
31+
#[derive(Subcommand, Debug)]
32+
enum Command {
33+
/// Update gitpane to the latest version via cargo install
34+
Update,
2635
}
2736

2837
#[tokio::main]
2938
async fn main() -> Result<()> {
3039
color_eyre::install()?;
3140

41+
let cli = Cli::parse();
42+
43+
if let Some(Command::Update) = cli.command {
44+
return self_update();
45+
}
46+
3247
tracing_subscriber::fmt()
3348
.with_env_filter(
3449
tracing_subscriber::EnvFilter::from_default_env()
@@ -37,7 +52,6 @@ async fn main() -> Result<()> {
3752
.with_writer(std::io::stderr)
3853
.init();
3954

40-
let cli = Cli::parse();
4155
let mut config = config::Config::load()?;
4256

4357
if let Some(root) = cli.root {
@@ -50,3 +64,35 @@ async fn main() -> Result<()> {
5064

5165
Ok(())
5266
}
67+
68+
fn self_update() -> Result<()> {
69+
let current = env!("CARGO_PKG_VERSION");
70+
println!("gitpane v{current} — checking for updates...");
71+
72+
if let Some(latest) = update_checker::check_latest() {
73+
println!("New version available: v{latest}");
74+
} else {
75+
println!("Already up to date.");
76+
return Ok(());
77+
}
78+
79+
println!("Running: cargo install gitpane");
80+
let status = std::process::Command::new("cargo")
81+
.args(["install", "gitpane"])
82+
.status();
83+
84+
match status {
85+
Ok(s) if s.success() => println!("Updated successfully."),
86+
Ok(s) => {
87+
eprintln!("cargo install exited with {s}");
88+
std::process::exit(1);
89+
}
90+
Err(e) => {
91+
eprintln!("Failed to run cargo: {e}");
92+
eprintln!("Make sure cargo is installed (https://rustup.rs)");
93+
std::process::exit(1);
94+
}
95+
}
96+
97+
Ok(())
98+
}

0 commit comments

Comments
 (0)