Skip to content

Commit

Permalink
feat(3131): show upgrade warning on exit (#3134)
Browse files Browse the repository at this point in the history
Co-authored-by: Tushar Mathur <[email protected]>
  • Loading branch information
dekkku and tusharmath authored Nov 20, 2024
1 parent 4f4336b commit 8ee2046
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 44 deletions.
29 changes: 29 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ opentelemetry-system-metrics = { version = "0.2.0", optional = true }
tailcall-http-cache = { path = "tailcall-http-cache", optional = true }
tailcall-version = { path = "./tailcall-version", optional = true }
genai = { git = "https://github.com/laststylebender14/rust-genai.git", rev = "63a542ce20132503c520f4e07108e0d768f243c3", optional = true }
ctrlc = { version = "3.4.5", optional = true }

# dependencies safe for wasm:

Expand Down Expand Up @@ -236,6 +237,7 @@ cli = [
"dep:tailcall-http-cache",
"dep:tailcall-version",
"dep:genai",
"dep:ctrlc",
]

# Feature flag to enable all default features.
Expand Down
105 changes: 61 additions & 44 deletions src/cli/update_checker.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,77 @@
use colored::Colorize;
use ctrlc::set_handler;
use tailcall_version::VERSION;
use update_informer::{registry, Check};
use update_informer::{registry, Check, Version};
use which::which;

#[derive(Default)]
enum InstallationMethod {
Npm,
Npx,
Brew,
#[default]
Direct,
}

fn get_installation_method() -> InstallationMethod {
if std::env::var("npm_execpath").is_ok() {
return InstallationMethod::Npx;
}
impl InstallationMethod {
/// figure out the installation method is used by user.
pub fn get_installation_method() -> Self {
if std::env::var("npm_execpath").is_ok() {
return InstallationMethod::Npx;
}

if let Ok(output) = std::process::Command::new("npm")
.arg("ls")
.arg("--global")
.output()
{
if String::from_utf8_lossy(&output.stdout).contains("@tailcallhq/tailcall") {
return InstallationMethod::Npm;
if let Ok(output) = std::process::Command::new("npm")
.arg("ls")
.arg("--global")
.output()
{
if String::from_utf8_lossy(&output.stdout).contains("@tailcallhq/tailcall") {
return InstallationMethod::Npm;
}
}

if let Ok(result) = which("tailcall") {
if result.to_str().map_or(false, |s| s.contains("homebrew")) {
return InstallationMethod::Brew;
}
}

InstallationMethod::default()
}

if let Ok(result) = which("tailcall") {
if result.to_str().map_or(false, |s| s.contains("homebrew")) {
return InstallationMethod::Brew;
fn format_upgrade_message(&self, command: &str) -> String {
format!("{} {}", "Please run:".white(), command.yellow())
}

/// displays the message to upgrade the tailcall depending on the
/// installation method used.
pub fn display_message(&self) -> String {
match self {
InstallationMethod::Npx => {
self.format_upgrade_message("npx @tailcallhq/tailcall@latest")
}
InstallationMethod::Npm => {
self.format_upgrade_message("npm update -g @tailcallhq/tailcall")
}
InstallationMethod::Brew => self.format_upgrade_message("brew upgrade tailcall"),
InstallationMethod::Direct => {
"Please update by downloading the latest release from GitHub".to_string()
}
}
}
}

InstallationMethod::Direct
fn show_update_message(name: &str, latest_version: Version) {
let github_release_url = format!("https://github.com/{name}/releases/tag/{latest_version}",);
tracing::warn!(
"{} {} {} {}. {}. Release notes: {}",
"A new release of tailcall is available:",
VERSION.as_str().cyan(),
"➜",
latest_version.to_string().cyan(),
InstallationMethod::get_installation_method().display_message(),
github_release_url.yellow()
);
}

pub async fn check_for_update() {
Expand All @@ -46,34 +86,11 @@ pub async fn check_for_update() {
let informer = update_informer::new(registry::GitHub, name, VERSION.as_str());

if let Some(latest_version) = informer.check_version().ok().flatten() {
let github_release_url =
format!("https://github.com/{name}/releases/tag/{latest_version}",);
let installation_method = get_installation_method();
tracing::warn!(
"{}",
format!(
"A new release of tailcall is available: {} {} {}",
VERSION.as_str().cyan(),
"➜".white(),
latest_version.to_string().cyan()
)
.yellow()
);
match installation_method {
InstallationMethod::Npx => tracing::warn!(
"You're running an outdated version, run: npx @tailcallhq/tailcall@latest"
),
InstallationMethod::Npm => {
tracing::warn!("To upgrade, run: npm update -g @tailcallhq/tailcall")
}
InstallationMethod::Brew => {
tracing::warn!("To upgrade, run: brew upgrade tailcall")
}
InstallationMethod::Direct => {
tracing::warn!("Please update by downloading the latest release from GitHub")
}
}
tracing::warn!("{}", github_release_url.yellow());
// schedules the update message to be shown when the user presses Ctrl+C on cli.
let _ = set_handler(move || {
show_update_message(name, latest_version.clone());
std::process::exit(exitcode::OK);
});
}
});
}

1 comment on commit 8ee2046

@github-actions
Copy link

Choose a reason for hiding this comment

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

Running 30s test @ http://localhost:8000/graphql

4 threads and 100 connections

Thread Stats Avg Stdev Max +/- Stdev
Latency 3.99ms 1.95ms 37.32ms 81.05%
Req/Sec 6.45k 840.21 9.45k 94.00%

769910 requests in 30.02s, 3.86GB read

Requests/sec: 25646.67

Transfer/sec: 131.64MB

Please sign in to comment.