mirror of
https://codeberg.org/Cyborus/forgejo-cli.git
synced 2024-11-27 04:03:48 +01:00
praise rustfmt
This commit is contained in:
parent
9a37f3bcd1
commit
b5693293e4
2 changed files with 27 additions and 37 deletions
36
src/keys.rs
36
src/keys.rs
|
@ -1,5 +1,5 @@
|
|||
use std::{collections::BTreeMap, io::ErrorKind};
|
||||
use eyre::eyre;
|
||||
use std::{collections::BTreeMap, io::ErrorKind};
|
||||
use tokio::io::AsyncWriteExt;
|
||||
use url::Url;
|
||||
|
||||
|
@ -47,17 +47,23 @@ impl KeyInfo {
|
|||
let remote_url = get_remote(&repo)?;
|
||||
let login_info = self.get_login(&remote_url)?;
|
||||
|
||||
let mut path = remote_url.path_segments().ok_or_else(|| eyre!("bad path"))?.collect::<Vec<_>>();
|
||||
let repo_name = path.pop().ok_or_else(|| eyre!("path does not have repo name"))?.to_string();
|
||||
let owner = path.pop().ok_or_else(|| eyre!("path does not have owner name"))?.to_string();
|
||||
let mut path = remote_url
|
||||
.path_segments()
|
||||
.ok_or_else(|| eyre!("bad path"))?
|
||||
.collect::<Vec<_>>();
|
||||
let repo_name = path
|
||||
.pop()
|
||||
.ok_or_else(|| eyre!("path does not have repo name"))?
|
||||
.to_string();
|
||||
let owner = path
|
||||
.pop()
|
||||
.ok_or_else(|| eyre!("path does not have owner name"))?
|
||||
.to_string();
|
||||
let base_path = path.join("/");
|
||||
|
||||
let mut url = remote_url;
|
||||
url.set_path(&base_path);
|
||||
let host_info = HostInfo {
|
||||
url,
|
||||
login_info,
|
||||
};
|
||||
let host_info = HostInfo { url, login_info };
|
||||
let repo_info = RepoInfo {
|
||||
owner,
|
||||
name: repo_name,
|
||||
|
@ -111,7 +117,7 @@ impl RepoInfo {
|
|||
pub fn owner(&self) -> &str {
|
||||
&self.owner
|
||||
}
|
||||
|
||||
|
||||
pub fn name(&self) -> &str {
|
||||
&self.name
|
||||
}
|
||||
|
@ -144,10 +150,7 @@ pub struct LoginInfo {
|
|||
|
||||
impl LoginInfo {
|
||||
pub fn new(name: String, key: String) -> Self {
|
||||
Self {
|
||||
name,
|
||||
key,
|
||||
}
|
||||
Self { name, key }
|
||||
}
|
||||
|
||||
pub fn username(&self) -> &str {
|
||||
|
@ -162,10 +165,11 @@ impl LoginInfo {
|
|||
fn get_remote(repo: &git2::Repository) -> eyre::Result<Url> {
|
||||
let head = repo.head()?;
|
||||
let branch_name = head.name().ok_or_else(|| eyre!("branch name not UTF-8"))?;
|
||||
let remote_name= repo.branch_upstream_remote(branch_name)?;
|
||||
let remote_name = remote_name.as_str().ok_or_else(|| eyre!("remote name not UTF-8"))?;
|
||||
let remote_name = repo.branch_upstream_remote(branch_name)?;
|
||||
let remote_name = remote_name
|
||||
.as_str()
|
||||
.ok_or_else(|| eyre!("remote name not UTF-8"))?;
|
||||
let remote = repo.find_remote(remote_name)?;
|
||||
let url = Url::parse(std::str::from_utf8(remote.url_bytes())?)?;
|
||||
Ok(url)
|
||||
}
|
||||
|
||||
|
|
28
src/main.rs
28
src/main.rs
|
@ -132,10 +132,9 @@ async fn main() -> eyre::Result<()> {
|
|||
RepoCommand::Browse => {
|
||||
let (host, repo) = keys.get_current()?;
|
||||
let mut url = host.url().clone();
|
||||
let new_path = format!("{}/{}/{}",
|
||||
url.path()
|
||||
.strip_suffix("/")
|
||||
.unwrap_or(url.path()),
|
||||
let new_path = format!(
|
||||
"{}/{}/{}",
|
||||
url.path().strip_suffix("/").unwrap_or(url.path()),
|
||||
repo.owner(),
|
||||
repo.name(),
|
||||
);
|
||||
|
@ -146,10 +145,7 @@ async fn main() -> eyre::Result<()> {
|
|||
Command::User { host } => {
|
||||
let host = host.map(|host| Url::parse(&host)).transpose()?;
|
||||
let (url, name) = match host {
|
||||
Some(url) => (
|
||||
keys.get_login(&url)?.username(),
|
||||
url,
|
||||
),
|
||||
Some(url) => (keys.get_login(&url)?.username(), url),
|
||||
None => {
|
||||
let (host, _) = keys.get_current()?;
|
||||
(host.username(), host.url().clone())
|
||||
|
@ -164,20 +160,14 @@ async fn main() -> eyre::Result<()> {
|
|||
// let pass = readline("password: ").await?;
|
||||
}
|
||||
AuthCommand::Logout { host } => {
|
||||
let info_opt = keys
|
||||
.hosts
|
||||
.remove(&host);
|
||||
let info_opt = keys.hosts.remove(&host);
|
||||
if let Some(info) = info_opt {
|
||||
eprintln!("signed out of {}@{}", &info.username(), host);
|
||||
} else {
|
||||
eprintln!("already not signed in to {host}");
|
||||
}
|
||||
}
|
||||
AuthCommand::AddKey {
|
||||
host,
|
||||
user,
|
||||
key,
|
||||
} => {
|
||||
AuthCommand::AddKey { host, user, key } => {
|
||||
let key = match key {
|
||||
Some(key) => key,
|
||||
None => readline("new key: ").await?,
|
||||
|
@ -185,10 +175,7 @@ async fn main() -> eyre::Result<()> {
|
|||
if keys.hosts.get(&user).is_none() {
|
||||
keys.hosts.insert(host, LoginInfo::new(user, key));
|
||||
} else {
|
||||
println!(
|
||||
"key for {} already exists",
|
||||
host
|
||||
);
|
||||
println!("key for {} already exists", host);
|
||||
}
|
||||
}
|
||||
AuthCommand::List => {
|
||||
|
@ -216,4 +203,3 @@ async fn readline(msg: &str) -> eyre::Result<String> {
|
|||
})
|
||||
.await?
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue