Repository: comtrya/comtrya (language: rust)

Issue:
Print a message if a newer version is available
Hi 👋

I found this feature very useful in the [GitHub CLI application](https://github.com/cli/cli/blob/trunk/internal/update/update.go) and [npm](https://github.com/npm/cli/blob/latest/lib/utils/update-notifier.js).

I think it'll be useful for users who install [comtrya](https://github.com/comtrya/comtrya) using the installation script.
And this will let them know that a new version is available and it's time to update.

Ready-made crates to do that:
* https://github.com/mgrachev/update-informer
* https://github.com/tarikeshaq/update-notifier


Discussion / hints:

Fix patch:
diff --git a/app/Cargo.toml b/app/Cargo.toml
index 39bf8ba9..66598e87 100644
--- a/app/Cargo.toml
+++ b/app/Cargo.toml
@@ -37,6 +37,8 @@ walkdir = "2"
 which = "4.2.5"
 whoami = "1.2.1"
 sha256 = "1.0.3"
+update-informer = "0.5.0"
+colored = "2.0.0"
 
 [target.'cfg(unix)'.dependencies]
 users = "0.11.0"
diff --git a/app/src/main.rs b/app/src/main.rs
index eeb3c49b..3735dcd7 100644
--- a/app/src/main.rs
+++ b/app/src/main.rs
@@ -78,6 +78,10 @@ fn main(args: GlobalArgs) -> anyhow::Result<()> {
         }
     };
 
+    if !config.disable_update_check {
+        check_for_updates(args.no_color);
+    }
+
     // Run Context Providers
     let contexts = build_contexts(&config);
 
@@ -87,5 +91,39 @@ fn main(args: GlobalArgs) -> anyhow::Result<()> {
         contexts,
     };
 
-    execute(runtime)
+    execute(runtime)?;
+
+    Ok(())
+}
+
+fn check_for_updates(no_color: bool) {
+    use colored::*;
+    use update_informer::{registry, Check};
+
+    if no_color {
+        control::set_override(false);
+    }
+
+    let pkg_name = env!("CARGO_PKG_NAME");
+    let pkg_version = env!("CARGO_PKG_VERSION");
+    let informer = update_informer::new(registry::Crates, pkg_name, pkg_version);
+
+    if let Some(new_version) = informer.check_version().ok().flatten() {
+        let msg = format!(
+            "A new version of {pkg_name} is available: v{pkg_version} -> {new_version}",
+            pkg_name = pkg_name.italic().cyan(),
+            new_version = new_version.to_string().green()
+        );
+
+        let release_url =
+            format!("https://github.com/{pkg_name}/{pkg_name}/releases/tag/{new_version}").blue();
+        let changelog = format!("Changelog: {release_url}",);
+
+        let cmd = format!(
+            "Run to update: {cmd}",
+            cmd = "curl -fsSL https://get.comtrya.dev | sh".green()
+        );
+
+        println!("\n{msg}\n{changelog}\n{cmd}");
+    }
 }
diff --git a/lib/src/config/mod.rs b/lib/src/config/mod.rs
index 14dbf23b..ae4c4a9f 100644
--- a/lib/src/config/mod.rs
+++ b/lib/src/config/mod.rs
@@ -13,6 +13,9 @@ pub struct Config {
 
     #[serde(default)]
     pub include_variables: Option<Vec<String>>,
+
+    #[serde(default)]
+    pub disable_update_check: bool,
 }
 
 /// Check the current working directory for a `Comtrya.yaml` file


Test patch: