summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordataCobra <datacobra@thinkbot.de>2024-10-26 13:03:30 +0200
committerdataCobra <datacobra@thinkbot.de>2024-10-26 13:03:30 +0200
commit68ec6a0440b5900591b04848e22d33f48ce005de (patch)
tree787082ae42b8280ba8b17f1e1df44beda0cb61e0
parentmove `whoami` into a module (diff)
downloadforgejo-cli-68ec6a0440b5900591b04848e22d33f48ce005de.tar.xz
forgejo-cli-68ec6a0440b5900591b04848e22d33f48ce005de.zip
move `version` into a module
-rw-r--r--src/main.rs59
-rw-r--r--src/version.rs62
2 files changed, 65 insertions, 56 deletions
diff --git a/src/main.rs b/src/main.rs
index 4b69010..b388fab 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -13,6 +13,7 @@ mod prs;
mod release;
mod repo;
mod user;
+mod version;
mod whoami;
mod wiki;
@@ -39,12 +40,7 @@ pub enum Command {
Auth(auth::AuthCommand),
Release(release::ReleaseCommand),
User(user::UserCommand),
- Version {
- /// Checks for updates
- #[clap(long)]
- #[cfg(feature = "update-check")]
- check: bool,
- },
+ Version(version::VersionCommand),
}
#[tokio::main]
@@ -66,62 +62,13 @@ 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::User(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?;
- }
+ Command::Version(command) => command.run().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> {
use std::io::Write;
print!("{msg}");
diff --git a/src/version.rs b/src/version.rs
new file mode 100644
index 0000000..70cae8d
--- /dev/null
+++ b/src/version.rs
@@ -0,0 +1,62 @@
+use clap::Args;
+#[cfg(feature = "update-check")]
+use eyre::OptionExt;
+
+#[derive(Args, Clone, Debug)]
+pub struct VersionCommand {
+ /// Checks for updates
+ #[clap(long)]
+ #[cfg(feature = "update-check")]
+ check: bool,
+}
+
+impl VersionCommand {
+ pub async fn run(self) -> eyre::Result<()> {
+ println!("{}", env!("CARGO_PKG_VERSION"));
+ #[cfg(feature = "update-check")]
+ self.update_msg().await?;
+ Ok(())
+ }
+
+ #[cfg(feature = "update-check")]
+ pub async fn update_msg(self) -> eyre::Result<()> {
+ use std::cmp::Ordering;
+
+ if self.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(())
+ }
+}