diff --git a/src/init.rs b/src/init.rs new file mode 100644 index 0000000..97f41ec --- /dev/null +++ b/src/init.rs @@ -0,0 +1,25 @@ +// +use crate::model::Config; +use crate::patterns::{issue_pattern, marker_pattern}; +use anyhow::{Context, Result}; + +pub fn init_config() -> Result { + let config = Config::builder() + .fs(kxio::fs::new( + std::env::var("GITHUB_WORKSPACE") + .context("GITHUB_WORKSPACE")? + .into(), + )) + .repo(std::env::var("GITHUB_REPOSITORY").context("GITHUB_REPOSITORY")?) + .server(std::env::var("GITHUB_SERVER_URL").context("GITHUB_SERVER_URL")?) + .prefix_pattern(marker_pattern()?) + .issue_pattern(issue_pattern()?) + .maybe_auth_token(std::env::var("REPO_TOKEN").ok()) + .build(); + + println!("Repo: {}", config.repo()); + println!("Prefix: {}", config.prefix_pattern()); + println!("Issues: {}", config.issue_pattern()); + + Ok(config) +} diff --git a/src/main.rs b/src/main.rs index 9a339cd..57fd966 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,43 +1,20 @@ // -use std::path::Path; - -use anyhow::{Context, Result}; -use model::{Config, FoundMarkers, Line, Marker}; -use patterns::{issue_pattern, marker_pattern}; +use anyhow::Result; +use init::init_config; +use scanner::find_markers; +mod init; mod model; mod patterns; +mod scanner; fn main() -> Result<()> { println!("Forgejo TODO Checker!"); - let config = Config::builder() - .fs(kxio::fs::new( - std::env::var("GITHUB_WORKSPACE") - .context("GITHUB_WORKSPACE")? - .into(), - )) - .repo(std::env::var("GITHUB_REPOSITORY").context("GITHUB_REPOSITORY")?) - .server(std::env::var("GITHUB_SERVER_URL").context("GITHUB_SERVER_URL")?) - .prefix_pattern(marker_pattern()?) - .issue_pattern(issue_pattern()?) - .maybe_auth_token(std::env::var("REPO_TOKEN").ok()) - .build(); + let config = init_config()?; - println!("Repo: {}", config.repo()); - println!("Prefix: {}", config.prefix_pattern()); - println!("Issues: {}", config.issue_pattern()); - - let mut found_markers = FoundMarkers::default(); - - for file in ignore::Walk::new(config.fs().base()).flatten() { - let path = file.path(); - if config.fs().path_is_file(path)? { - scan_file(path, &config, &mut found_markers)?; - } - } - - println!("{found_markers}"); + let markers = find_markers(config)?; + println!("{markers}"); // TODO: add authentication when provided // let issues = ureq::get(&api).call()?.into_string()?; @@ -51,25 +28,3 @@ fn main() -> Result<()> { Ok(()) } - -fn scan_file(file: &Path, config: &Config, found_markers: &mut FoundMarkers) -> Result<()> { - let relative_path = file.strip_prefix(config.fs().base())?.to_path_buf(); - config - .fs() - .file_read_to_string(file)? - .lines() - .enumerate() - .map(|(n, line)| { - Line::builder() - .file(file.to_path_buf()) - .relative_path(relative_path.clone()) - .num(n) - .value(line.to_owned()) - .build() - }) - .filter_map(|line| line.into_marker().ok()) - .filter(|marker| !matches!(marker, Marker::Unmarked)) - .for_each(|marker| found_markers.add_marker(marker)); - - Ok(()) -} diff --git a/src/scanner.rs b/src/scanner.rs new file mode 100644 index 0000000..a385ee0 --- /dev/null +++ b/src/scanner.rs @@ -0,0 +1,38 @@ +// +use std::path::Path; + +use crate::model::{Config, FoundMarkers, Line, Marker}; +use anyhow::Result; + +pub fn find_markers(config: Config) -> Result { + let mut markers = FoundMarkers::default(); + for file in ignore::Walk::new(config.fs().base()).flatten() { + let path = file.path(); + if config.fs().path_is_file(path)? { + scan_file(path, &config, &mut markers)?; + } + } + Ok(markers) +} + +fn scan_file(file: &Path, config: &Config, found_markers: &mut FoundMarkers) -> Result<()> { + let relative_path = file.strip_prefix(config.fs().base())?.to_path_buf(); + config + .fs() + .file_read_to_string(file)? + .lines() + .enumerate() + .map(|(n, line)| { + Line::builder() + .file(file.to_path_buf()) + .relative_path(relative_path.clone()) + .num(n) + .value(line.to_owned()) + .build() + }) + .filter_map(|line| line.into_marker().ok()) + .filter(|marker| !matches!(marker, Marker::Unmarked)) + .for_each(|marker| found_markers.add_marker(marker)); + + Ok(()) +}