summaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
authorCyborus <cyborus@cyborus.xyz>2024-06-25 01:35:54 +0200
committerCyborus <cyborus@cyborus.xyz>2024-06-25 01:35:54 +0200
commit1eaeec784f6747d2529951faf4ba98c04383cd4c (patch)
treef9c637451679c25093bc6864091511f2373401ef /src/main.rs
parentMerge pull request 'fix trailing paragraph newlines in markdown rendering' (#... (diff)
downloadforgejo-cli-1eaeec784f6747d2529951faf4ba98c04383cd4c.tar.xz
forgejo-cli-1eaeec784f6747d2529951faf4ba98c04383cd4c.zip
feat: version command and update checker
Diffstat (limited to '')
-rw-r--r--src/main.rs56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs
index f33d71b..4b3f852 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -37,6 +37,12 @@ pub enum Command {
#[clap(subcommand)]
Auth(auth::AuthCommand),
Release(release::ReleaseCommand),
+ Version {
+ /// Checks for updates
+ #[clap(long)]
+ #[cfg(feature = "update-check")]
+ check: bool,
+ },
}
#[tokio::main]
@@ -70,12 +76,62 @@ async fn main() -> eyre::Result<()> {
}
Command::Auth(subcommand) => subcommand.run(&mut keys, host_name).await?,
Command::Release(subcommand) => subcommand.run(&mut keys, host_name).await?,
+ Command::Version {
+ #[cfg(feature = "update-check")]
+ check,
+ } => {
+ println!("{}", env!("CARGO_PKG_VERSION"));
+ #[cfg(feature = "update-check")]
+ update_msg(check).await?;
+ }
}
keys.save().await?;
Ok(())
}
+#[cfg(feature = "update-check")]
+async fn update_msg(check: bool) -> eyre::Result<()> {
+ use std::cmp::Ordering;
+
+ if check {
+ let url = url::Url::parse("https://codeberg.org/")?;
+ let api = forgejo_api::Forgejo::new(forgejo_api::Auth::None, url)?;
+
+ let latest = api
+ .repo_get_latest_release("Cyborus", "forgejo-cli")
+ .await?;
+ let latest_tag = latest
+ .tag_name
+ .ok_or_eyre("latest release does not have name")?;
+ let latest_ver = latest_tag
+ .strip_prefix("v")
+ .unwrap_or(&latest_tag)
+ .parse::<semver::Version>()?;
+
+ let current_ver = env!("CARGO_PKG_VERSION").parse::<semver::Version>()?;
+
+ match current_ver.cmp(&latest_ver) {
+ Ordering::Less => {
+ let latest_url = latest
+ .html_url
+ .ok_or_eyre("latest release does not have url")?;
+ println!("New version available: {latest_ver}");
+ println!("Get it at {}", latest_url);
+ }
+ Ordering::Equal => {
+ println!("Up to date!");
+ }
+ Ordering::Greater => {
+ println!("You are ahead of the latest published version");
+ }
+ }
+ } else {
+ println!("Check for a new version with `fj version --check`");
+ }
+ Ok(())
+}
+
async fn readline(msg: &str) -> eyre::Result<String> {
print!("{msg}");
tokio::io::stdout().flush().await?;