diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs index e0455ca..84a60a9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ use anyhow::anyhow; +use anyhow::Error; use anyhow::Result; use clap::ArgMatches; use result_inspect::*; @@ -16,6 +17,9 @@ async fn main() -> Result<()> { match app.get_matches().subcommand() { ("problems", Some(mtch)) => problems(mtch).await, + ("projects", Some(mtch)) => projects(mtch).await, + ("project", Some(mtch)) => project(mtch).await, + ("repo", Some(mtch)) => problems(mtch).await, (other, _) => Err(anyhow!("No such subcommand: {}", other)), } } @@ -36,3 +40,82 @@ async fn problems<'a>(matches: &ArgMatches<'a>) -> Result<()> { Ok(()) } + +async fn projects<'a>(matches: &ArgMatches<'a>) -> Result<()> { + trait FromCliArg: Sized { + fn from_cli_arg(arg: &str) -> Result<Self>; + } + + impl FromCliArg for String { + fn from_cli_arg(arg: &str) -> Result<Self> { + Ok(String::from(arg)) + } + } + + impl FromCliArg for usize { + fn from_cli_arg(arg: &str) -> Result<Self> { + use std::str::FromStr; + usize::from_str(arg).map_err(Error::from) + } + } + + impl FromCliArg for bool { + fn from_cli_arg(arg: &str) -> Result<Self> { + use std::str::FromStr; + bool::from_str(arg).map_err(Error::from) + } + } + + impl FromCliArg for librepology::v1::api::NumberOrRange { + fn from_cli_arg(arg: &str) -> Result<Self> { + // TODO: Parse arg to NumberOrRange + unimplemented!() + } + } + + fn cliarg<'a, T: FromCliArg>(matches: &ArgMatches<'a>, s: &'static str) -> Result<Option<T>> { + matches.value_of(s).map(FromCliArg::from_cli_arg).transpose() + } + + ApiClient::new::<DefaultEndpoint>()? + .projects() + .filtered() + .with_search(cliarg(matches, "search")?) + .with_maintainer(cliarg(matches, "maintainer")?) + .with_category(cliarg(matches, "category")?) + .with_in_repo_filter(cliarg(matches, "in_repo_filter")?) + .with_not_in_repo_filter(cliarg(matches, "not_in_repo_filter")?) + .with_repos_filter(cliarg(matches, "repos_filter")?) + .with_families_filter(cliarg(matches, "families_filter")?) + .with_repos_newest(cliarg(matches, "repos_newest")?) + .with_families_newest(cliarg(matches, "families_newest")?) + .with_newest(cliarg(matches, "newest")?) + .with_outdated(cliarg(matches, "outdated")?) + .with_problematic(cliarg(matches, "problematic")?) + .to_request() + .perform() + .await? + .into_iter() + .for_each(|project| { + println!("Project: {:?}", project); + }); + + Ok(()) +} + +async fn project<'a>(matches: &ArgMatches<'a>) -> Result<()> { + let name = matches.value_of("name").map(String::from).unwrap(); // safe by clap + let client = ApiClient::new::<DefaultEndpoint>()?; + client.projects() + .with_name(name) + .to_request() + .perform() + .await + .inspect(|response| log::debug!("Response: {:?}", response))? + .into_iter() + .for_each(|project| { + println!("Project: {:?}", project); + }); + + Ok(()) +} |