Skip to content

Commit 03098f1

Browse files
authored
feat(cli): hint users for newer version (#166)
1 parent 04b4e8e commit 03098f1

File tree

6 files changed

+319
-2
lines changed

6 files changed

+319
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
cargo/sampo: minor
3+
---
4+
5+
Sampo now checks for updates once per day, and displays a hint when a newer version is available on crates.io. The check is non-blocking and fails silently if offline.

Cargo.lock

Lines changed: 49 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/sampo/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ rustc-hash = "2.0"
2121
semver = "1.0.26"
2222
serde_json = "1.0"
2323
toml = "0.8"
24-
reqwest = { version = "0.12", features = ["json"] }
24+
reqwest = { version = "0.12", features = ["json", "blocking"] }
2525
sampo-core = { version = "0.10.0", path = "../sampo-core" }
2626
clap = { version = "4.5", features = ["derive"] }
2727
dialoguer = { version = "0.11", default-features = true }
28+
dirs = "6.0.0"
2829

2930
[dev-dependencies]
3031
tempfile = "3"

crates/sampo/src/main.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@ mod prerelease;
66
mod publish;
77
mod release;
88
mod ui;
9+
mod version_check;
910

1011
use clap::Parser;
1112
use cli::{Cli, Commands};
1213
use std::process::ExitCode;
14+
use version_check::VersionCheckResult;
1315

1416
fn main() -> ExitCode {
1517
let cli = Cli::parse();
1618

19+
check_and_notify_update();
20+
1721
match cli.command {
1822
Commands::Init => {
1923
let cwd = match std::env::current_dir() {
@@ -70,3 +74,14 @@ fn main() -> ExitCode {
7074
}
7175
ExitCode::SUCCESS
7276
}
77+
78+
/// Checks for CLI updates and prints a hint if a newer version is available. Non-blocking, best-effort.
79+
fn check_and_notify_update() {
80+
if let VersionCheckResult::UpdateAvailable { current, latest } =
81+
version_check::check_for_updates()
82+
{
83+
ui::log_hint(&format!(
84+
"A new version of Sampo is available: {current} → {latest}. Run `cargo install sampo` to update."
85+
));
86+
}
87+
}

crates/sampo/src/ui.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ use sampo_core::{
1010
use std::io;
1111

1212
pub const SUCCESS_PREFIX: &str = "✔";
13-
pub const WARNING_PREFIX: &str = "⚠️";
13+
pub const WARNING_PREFIX: &str = "⚠";
14+
pub const HINT_PREFIX: &str = "💡";
1415
const EMPTY_SELECTION_PLACEHOLDER: &str = "(none)";
1516

1617
pub fn log_success_value(label: &str, value: &str) {
@@ -55,6 +56,17 @@ pub fn log_warning(message: &str) {
5556
eprintln!("{line}");
5657
}
5758

59+
/// Prints a hint message to stderr with a distinct visual style.
60+
///
61+
/// Used for non-critical suggestions like update notifications.
62+
pub fn log_hint(message: &str) {
63+
let prefix = style(HINT_PREFIX.to_string()).for_stderr().yellow();
64+
let message_style = Style::new().for_stderr().yellow();
65+
66+
let line = format!("{} {}", prefix, message_style.apply_to(message));
67+
eprintln!("{line}");
68+
}
69+
5870
pub fn prompt_theme() -> ColorfulTheme {
5971
ColorfulTheme {
6072
prompt_prefix: style("🧭".to_string()).cyan(),

0 commit comments

Comments
 (0)