mirror of
https://codeberg.org/Cyborus/forgejo-cli.git
synced 2025-03-14 21:25:54 +01:00
Merge pull request 'move whoami
and version
into separate modules' (#137) from dataCobra/forgejo-cli:whoami into main
Reviewed-on: https://codeberg.org/Cyborus/forgejo-cli/pulls/137
This commit is contained in:
commit
884778f401
3 changed files with 98 additions and 76 deletions
83
src/main.rs
83
src/main.rs
|
@ -1,7 +1,7 @@
|
|||
use std::io::IsTerminal;
|
||||
|
||||
use clap::{Parser, Subcommand};
|
||||
use eyre::{eyre, Context, OptionExt};
|
||||
use eyre::eyre;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
|
||||
mod keys;
|
||||
|
@ -13,6 +13,8 @@ mod prs;
|
|||
mod release;
|
||||
mod repo;
|
||||
mod user;
|
||||
mod version;
|
||||
mod whoami;
|
||||
mod wiki;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
|
@ -33,20 +35,12 @@ pub enum Command {
|
|||
Pr(prs::PrCommand),
|
||||
Wiki(wiki::WikiCommand),
|
||||
#[command(name = "whoami")]
|
||||
WhoAmI {
|
||||
#[clap(long, short)]
|
||||
remote: Option<String>,
|
||||
},
|
||||
WhoAmI(whoami::WhoAmICommand),
|
||||
#[clap(subcommand)]
|
||||
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]
|
||||
|
@ -64,80 +58,17 @@ async fn main() -> eyre::Result<()> {
|
|||
Command::Issue(subcommand) => subcommand.run(&mut keys, host_name).await?,
|
||||
Command::Pr(subcommand) => subcommand.run(&mut keys, host_name).await?,
|
||||
Command::Wiki(subcommand) => subcommand.run(&mut keys, host_name).await?,
|
||||
Command::WhoAmI { remote } => {
|
||||
let url = repo::RepoInfo::get_current(host_name, None, remote.as_deref(), &keys)
|
||||
.wrap_err("could not find host, try specifying with --host")?
|
||||
.host_url()
|
||||
.clone();
|
||||
let name = keys.get_login(&url).ok_or_eyre("not logged in")?.username();
|
||||
let host = url
|
||||
.host_str()
|
||||
.ok_or_eyre("instance url does not have host")?;
|
||||
if url.path() == "/" || url.path().is_empty() {
|
||||
println!("currently signed in to {name}@{host}");
|
||||
} else {
|
||||
println!("currently signed in to {name}@{host}{}", url.path());
|
||||
}
|
||||
}
|
||||
Command::WhoAmI(command) => command.run(&mut keys, host_name).await?,
|
||||
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}");
|
||||
|
|
62
src/version.rs
Normal file
62
src/version.rs
Normal file
|
@ -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(())
|
||||
}
|
||||
}
|
29
src/whoami.rs
Normal file
29
src/whoami.rs
Normal file
|
@ -0,0 +1,29 @@
|
|||
use clap::{self, Args};
|
||||
use eyre::{Context, OptionExt};
|
||||
|
||||
use crate::{repo::RepoInfo, KeyInfo};
|
||||
|
||||
#[derive(Args, Clone, Debug)]
|
||||
pub struct WhoAmICommand {
|
||||
#[clap(long, short)]
|
||||
remote: Option<String>,
|
||||
}
|
||||
|
||||
impl WhoAmICommand {
|
||||
pub async fn run(self, keys: &mut KeyInfo, host_name: Option<&str>) -> eyre::Result<()> {
|
||||
let url = RepoInfo::get_current(host_name, None, self.remote.as_deref(), &keys)
|
||||
.wrap_err("could not find host, try specifying with --host")?
|
||||
.host_url()
|
||||
.clone();
|
||||
let name = keys.get_login(&url).ok_or_eyre("not logged in")?.username();
|
||||
let host = url
|
||||
.host_str()
|
||||
.ok_or_eyre("instance url does not have host")?;
|
||||
if url.path() == "/" || url.path().is_empty() {
|
||||
println!("currently signed in to {name}@{host}");
|
||||
} else {
|
||||
println!("currently signed in to {name}@{host}{}", url.path());
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue