From 3453f6431291ba0f76f488713b4b8fb273d40e52 Mon Sep 17 00:00:00 2001 From: Robert Garrett Date: Sun, 20 Jul 2025 12:33:38 -0500 Subject: [PATCH] Wire in the conf file loading, assume PWD project Load the configuration for the current directory. The project guessing mechanism isn't here, yet, so this will have to do. First take the properties set via Args. This will also capture the values set through environment variables. For anything that's missing, try to fill it with the info from the configuration files. In the event that there isn't enough information, new error types have been added to signal mis-use. --- src/lib.rs | 9 +++++++++ src/main.rs | 38 +++++++++++++++++++++++++++++++++----- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 580c45d..d14ed47 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,6 +21,9 @@ pub(crate) async fn decode_client_error(response: reqwest::Response) -> Result for crate::Error { } } +impl From for crate::Error { + fn from(value: crate::config::Error) -> Self { + Self::WrappedConfigErr(value) + } +} + type Result = core::result::Result; diff --git a/src/main.rs b/src/main.rs index 1c98e8b..711ef20 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,34 @@ use reqwest::header::ACCEPT; async fn main() -> Result<(), gt_tool::Error> { let args = Args::parse(); + // TODO: Heuristics to guess project path + // See issue #8: https://git.gelvin.dev/robert/gt-tool/issues/8 + let pwd = std::env::current_dir() + .map_err(|_e| gt_tool::Error::WrappedConfigErr( + gt_tool::config::Error::CouldntReadFile + ))?; + let config = gt_tool::config::get_config( + pwd.to_str().expect("I assumed the path can be UTF-8, but that didn't work out..."), + gt_tool::config::default_paths() + )?; + println!("->> Loaded Config: {config:?}"); + // arg parser also checks the environment. Prefer CLI/env, then config file. + let gitea_url = args.gitea_url.or(config.gitea_url).ok_or(gt_tool::Error::MissingGiteaUrl)?; + // Config files split the repo FQRN into "owner" and "repo" (confusing naming, sorry) + // These must be merged back together and passed along. + let conf_fqrn = config.owner + .ok_or(gt_tool::Error::MissingRepoFRQN) + .and_then(| mut own| { + let repo = config.repo.ok_or(gt_tool::Error::MissingRepoFRQN)?; + own.push_str("/"); + own.push_str(&repo); + Ok(own) + }); + let repo_fqrn = args.repo + .ok_or(gt_tool::Error::MissingRepoFRQN) + .or(conf_fqrn)?; + + let mut headers = reqwest::header::HeaderMap::new(); headers.append(ACCEPT, header::HeaderValue::from_static("application/json")); @@ -28,7 +56,7 @@ async fn main() -> Result<(), gt_tool::Error> { match args.command { gt_tool::cli::Commands::ListReleases => { let releases = - gt_tool::api::release::list_releases(&client, &args.gitea_url, &args.repo).await?; + gt_tool::api::release::list_releases(&client, &gitea_url, &repo_fqrn).await?; // Print in reverse order so the newest items are closest to the // user's command prompt. Otherwise the newest item scrolls off the // screen and can't be seen. @@ -54,7 +82,7 @@ async fn main() -> Result<(), gt_tool::Error> { tag_name, target_commitish, }; - gt_tool::api::release::create_release(&client, &args.gitea_url, &args.repo, submission) + gt_tool::api::release::create_release(&client, &gitea_url, &repo_fqrn, submission) .await?; } gt_tool::cli::Commands::UploadRelease { @@ -75,7 +103,7 @@ async fn main() -> Result<(), gt_tool::Error> { // Grab all, find the one that matches the input tag. // Scream if there are multiple matches. let release_candidates = - gt_tool::api::release::list_releases(&client, &args.gitea_url, &args.repo).await?; + gt_tool::api::release::list_releases(&client, &gitea_url, &repo_fqrn).await?; if let Some(release) = match_release_by_tag(&tag_name, release_candidates) { for file in &files { @@ -94,8 +122,8 @@ async fn main() -> Result<(), gt_tool::Error> { for file in files { let _attach_desc = gt_tool::api::release_attachment::create_release_attachment( &client, - &args.gitea_url, - &args.repo, + &gitea_url, + &repo_fqrn, release.id, file, )