Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,9 +3,14 @@ name = "codeowners"
version = "0.1.0"
edition = "2021"

[profile.release]
debug = true

[dependencies]
clap = { version = "4.2.1", features = ["derive"] }
clap_derive = "4.2.0"
color-eyre = "0.6.2"

glob-match = "0.2.1"
itertools = "0.10.5"
jwalk = "0.8.1"
Expand Down
31 changes: 14 additions & 17 deletions src/main.rs
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
use color_eyre::{eyre::Context, Result};
use ownership::{Ownership, ValidationErrors};
use tracing::debug;

use crate::project::Project;
use clap::{Parser, Subcommand};
use path_clean::PathClean;
use std::{
error::Error,
fs::File,
path::{Path, PathBuf},
process,
Expand DownExpand Up@@ -47,44 +46,42 @@ struct Args {
}

impl Args {
fn absolute_project_root(&self) -> Result<PathBuf, std::io::Error> {
self.project_root.canonicalize()
fn absolute_project_root(&self) -> Result<PathBuf> {
self.project_root
.canonicalize()
.with_context(|| format!("Can't canonizalize {}", self.project_root.to_string_lossy()))
}

fn absolute_config_path(&self) -> Result<PathBuf, std::io::Error> {
fn absolute_config_path(&self) -> Result<PathBuf> {
Ok(self.absolute_path(&self.config_path)?.clean())
}

fn absolute_codeowners_path(&self) -> Result<PathBuf, std::io::Error> {
fn absolute_codeowners_path(&self) -> Result<PathBuf> {
Ok(self.absolute_path(&self.codeowners_file_path)?.clean())
}

fn absolute_path(&self, path: &Path) -> Result<PathBuf, std::io::Error> {
fn absolute_path(&self, path: &Path) -> Result<PathBuf> {
Ok(self.absolute_project_root()?.join(path))
}
}

fn main() -> Result<(), Box<dyn Error>> {
fn main() -> Result<()> {
color_eyre::install()?;
install_logger();
print_validation_errors_to_stdout(cli())?;

Ok(())
}

fn cli() -> Result<(), Box<dyn Error>> {
fn cli() -> Result<()> {
let args = Args::parse();

let config_path = args.absolute_config_path()?;
let codeowners_file_path = args.absolute_codeowners_path()?;
let project_root = args.absolute_project_root()?;

debug!(
config_path = &config_path.to_str(),
codeowners_file_path = &codeowners_file_path.to_str(),
project_root = &project_root.to_str(),
);

let config = serde_yaml::from_reader(File::open(config_path)?)?;
let config =
serde_yaml::from_reader(File::open(&config_path).with_context(|| format!("Can't open {}", config_path.to_string_lossy()))?)?;
let ownership = Ownership::build(Project::build(&project_root, &codeowners_file_path, &config)?);
let command = args.command;

Expand All@@ -102,7 +99,7 @@ fn cli() -> Result<(), Box<dyn Error>> {
Ok(())
}

fn print_validation_errors_to_stdout(result: Result<(), Box<dyn Error>>) -> Result<(), Box<dyn Error>> {
fn print_validation_errors_to_stdout(result: Result<()>) -> Result<()> {
if let Err(error) = result {
if let Some(validation_errors) = error.downcast_ref::<ValidationErrors>() {
println!("{}", validation_errors);
Expand Down
6 changes: 3 additions & 3 deletions src/project.rs
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
use std::{
collections::HashMap,
error::Error,
fs::File,
io::BufRead,
path::{Path, PathBuf},
};

use color_eyre::Result;
use jwalk::WalkDir;
use rayon::prelude::{IntoParallelIterator, ParallelIterator};
use regex::Regex;
Expand DownExpand Up@@ -111,7 +111,7 @@ mod deserializers {

impl Project {
#[instrument(level = "debug", skip_all)]
pub fn build(base_path: &Path, codeowners_file_path: &Path, config: &Config) -> Result<Self, Box<dyn Error>> {
pub fn build(base_path: &Path, codeowners_file_path: &Path, config: &Config) -> Result<Self> {
debug!("scanning project ({})", base_path.to_string_lossy());

let mut owned_file_paths: Vec<PathBuf> = Vec::new();
Expand DownExpand Up@@ -272,7 +272,7 @@ fn owned_files(owned_file_paths: Vec<PathBuf>) -> Vec<ProjectFile> {
.collect()
}

fn package_owner(path: &Path) -> Result<Option<String>, Box<dyn Error>> {
fn package_owner(path: &Path) -> Result<Option<String>> {
let deserializer: deserializers::Package = serde_yaml::from_reader(File::open(path)?)?;

if let Some(metadata) = deserializer.metadata {
Expand Down