Skip to content
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
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ repos:
rev: v6.0.0
hooks:
- id: end-of-file-fixer
exclude: ^docs/cli-commands\.md$
- id: trailing-whitespace
args:
- --markdown-linebreak-ext=md
Expand Down
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ ulid = "1.1"
unicode-icons = "2.0"
url = { version = "2.5", features = ["serde"] }
urlencoding = "2.1"
self-replace = "1"
tempfile = "3.8"
walkdir = "2.5"
webbrowser = "1.0"

[dev-dependencies]
mockito = "1.5"
serial_test = "3"
tempfile = "3.8"
tokio-test = "0.4"

# Optimize dependencies even in dev mode for better test performance
Expand Down
14 changes: 14 additions & 0 deletions docs/cli-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ This document contains the help content for the `ricochet` command-line program.
* [`ricochet servers add`↴](#ricochet-servers-add)
* [`ricochet servers remove`↴](#ricochet-servers-remove)
* [`ricochet servers set-default`↴](#ricochet-servers-set-default)
* [`ricochet self-update`↴](#ricochet-self-update)

## `ricochet`

Expand All @@ -36,6 +37,7 @@ Ricochet CLI
* `config` — Show configuration
* `init` — Initialize a new Ricochet deployment
* `servers` — Manage configured Ricochet servers
* `self-update` — Update the ricochet CLI to the latest version

###### **Options:**

Expand Down Expand Up @@ -231,6 +233,18 @@ Set the default server



## `ricochet self-update`

Update the ricochet CLI to the latest version

**Usage:** `ricochet self-update [OPTIONS]`

###### **Options:**

* `-f`, `--force` — Force reinstall even if already on the latest version



<hr/>

<small><i>
Expand Down
1 change: 1 addition & 0 deletions src/commands/auth/auth_unit_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ mod tests {
servers,
default_server: Some("prod".to_string()),
default_format: Some("table".to_string()),
skip_update_check: None,
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/commands/auth/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ pub async fn login(
"{} Headless environment detected (no display server). Using manual key entry.",
"ℹ".bright_cyan()
);
println!(
"Create an API key in your server's web UI and paste it below.\n"
);
println!("Create an API key in your server's web UI and paste it below.\n");

let key = Password::new()
.with_prompt("Enter API key (starts with 'rico_')")
Expand Down
1 change: 1 addition & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pub mod init;
pub mod invoke;
pub mod list;
pub mod servers;
pub mod update;
231 changes: 231 additions & 0 deletions src/commands/update.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
use crate::update;
use anyhow::{Context, Result};
use colored::Colorize;
use flate2::read::GzDecoder;
use indicatif::{ProgressBar, ProgressStyle};
use std::io::Read;

const CURRENT_VERSION: &str = env!("CARGO_PKG_VERSION");

/// Determine the download URL for the current platform.
fn download_url(version: &str) -> Result<String> {
#[cfg(any(
all(target_os = "linux", target_arch = "x86_64"),
all(target_os = "linux", target_arch = "aarch64"),
all(target_os = "windows", target_arch = "x86_64"),
))]
let base = format!(
"https://github.com/ricochet-rs/cli/releases/download/v{}",
version
);

#[cfg(target_os = "macos")]
let base = format!(
"https://hel1.your-objectstorage.com/ricochet-cli/v{}",
version
);

#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
return Ok(format!("{}/ricochet-{}-linux-x86_64.tar.gz", base, version));

#[cfg(all(target_os = "linux", target_arch = "aarch64"))]
return Ok(format!(
"{}/ricochet-{}-linux-aarch64.tar.gz",
base, version
));

#[cfg(all(target_os = "windows", target_arch = "x86_64"))]
return Ok(format!(
"{}/ricochet-{}-windows-x86_64.exe.tar.gz",
base, version
));

#[cfg(all(target_os = "macos", target_arch = "aarch64"))]
return Ok(format!("{}/ricochet-{}-darwin-arm64.tar.gz", base, version));

#[cfg(all(target_os = "macos", target_arch = "x86_64"))]
return Ok(format!(
"{}/ricochet-{}-darwin-x86_64.tar.gz",
base, version
));

#[cfg(not(any(
all(target_os = "linux", target_arch = "x86_64"),
all(target_os = "linux", target_arch = "aarch64"),
all(target_os = "windows", target_arch = "x86_64"),
all(target_os = "macos", target_arch = "aarch64"),
all(target_os = "macos", target_arch = "x86_64"),
)))]
anyhow::bail!(
"self-update is not supported on this platform.\n Install manually: https://github.com/ricochet-rs/cli/releases"
);
}

/// The binary name inside the tarball for the current platform.
/// macOS bottles have a different structure: ricochet/{version}/bin/ricochet
fn binary_name_in_tarball(version: &str) -> Result<String> {
#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
return Ok(format!("ricochet-{}-linux-x86_64", version));

#[cfg(all(target_os = "linux", target_arch = "aarch64"))]
return Ok(format!("ricochet-{}-linux-aarch64", version));

#[cfg(all(target_os = "windows", target_arch = "x86_64"))]
return Ok(format!("ricochet-{}-windows-x86_64.exe", version));

// macOS bottles: the binary is at ricochet/{version}/bin/ricochet inside the tarball
#[cfg(target_os = "macos")]
return Ok(format!("ricochet/{}/bin/ricochet", version));

#[cfg(not(any(
all(target_os = "linux", target_arch = "x86_64"),
all(target_os = "linux", target_arch = "aarch64"),
all(target_os = "windows", target_arch = "x86_64"),
target_os = "macos",
)))]
{
let _ = version;
anyhow::bail!("self-update is not supported on this platform");
}
}

pub async fn self_update(force: bool) -> Result<()> {
println!("Checking for updates...");

let latest = update::fetch_latest_version()
.await
.context("Failed to fetch latest version from GitHub")?;

if !update::is_newer(CURRENT_VERSION, &latest) && !force {
println!(
"{} Already on the latest version: {}",
"✓".green().bold(),
CURRENT_VERSION.bright_cyan()
);
return Ok(());
}

if !update::is_newer(CURRENT_VERSION, &latest) {
println!(
"Reinstalling current version {} (--force)",
latest.bright_cyan()
);
} else {
println!(
"Updating {} -> {}",
CURRENT_VERSION.dimmed(),
latest.green().bold()
);
}

let url = download_url(&latest)?;

let client = reqwest::Client::builder()
.user_agent(concat!("ricochet-cli/", env!("CARGO_PKG_VERSION")))
.timeout(std::time::Duration::from_secs(120))
.build()?;

let spinner = ProgressBar::new_spinner();
spinner.set_style(
ProgressStyle::default_spinner()
.template("{spinner:.green} {msg}")
.unwrap(),
);
spinner.set_message(format!("Downloading v{}...", latest));
spinner.enable_steady_tick(std::time::Duration::from_millis(80));

let tarball_bytes = client
.get(&url)
.send()
.await
.context("Failed to download release tarball")?
.error_for_status()
.context("Download failed (server returned error)")?
.bytes()
.await
.context("Failed to read download response")?;

spinner.finish_and_clear();
println!("{} Downloaded ({} bytes)", "✓".green(), tarball_bytes.len());

let binary_path = binary_name_in_tarball(&latest)?;
let extracted_bytes = extract_binary_from_tarball(&tarball_bytes, &binary_path)
.with_context(|| format!("Failed to extract '{}' from tarball", binary_path))?;

// Write extracted binary to a temp file, then use self_replace to atomically swap it in.
// self_replace handles platform quirks like Windows locking the running executable.
let tmp_dir = tempfile::tempdir().context("Failed to create temp directory")?;
let tmp_path = tmp_dir.path().join("ricochet-new");
std::fs::write(&tmp_path, &extracted_bytes)
.context("Failed to write new binary to temp file")?;

#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(&tmp_path, std::fs::Permissions::from_mode(0o755))
.context("Failed to set executable permissions")?;
}

self_replace::self_replace(&tmp_path)
.context("Failed to replace the ricochet binary. You may need elevated permissions.")?;

// Update the cache: reset failure counter and record the new version
let _ = update::save_cache(&update::UpdateCache {
last_checked: chrono::Utc::now(),
latest_version: latest.clone(),
consecutive_failures: 0,
});

// Re-enable update checks if they were auto-disabled due to previous failures
if let Ok(mut config) = crate::config::Config::load()
&& config.skip_update_check == Some(true)
{
config.skip_update_check = None;
let _ = config.save();
}

println!(
"\n{} Successfully updated to v{}",
"✓".green().bold(),
latest.bright_cyan()
);
println!(
"Release notes: {}",
update::release_notes_url(&latest).bright_cyan()
);

Ok(())
}

fn extract_binary_from_tarball(tarball: &[u8], binary_path: &str) -> Result<Vec<u8>> {
use std::io::Cursor;

let cursor = Cursor::new(tarball);
let gz = GzDecoder::new(cursor);
let mut archive = tar::Archive::new(gz);

for entry in archive.entries().context("Failed to read tar entries")? {
let mut entry = entry.context("Failed to read tar entry")?;
let path = entry.path().context("Failed to read entry path")?;
let path_str = path.to_string_lossy();

// Match against full path (for macOS bottles: ricochet/0.4.0/bin/ricochet)
// or just the filename (for Linux/Windows flat tarballs)
let matches = path_str == binary_path
|| path
.file_name()
.and_then(|n| n.to_str())
.map(|n| n == binary_path)
.unwrap_or(false);

if matches {
let mut bytes = Vec::new();
entry
.read_to_end(&mut bytes)
.context("Failed to read binary from tar")?;
return Ok(bytes);
}
}

anyhow::bail!("Binary '{}' not found in tarball", binary_path)
}
Loading