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