Skip to content

Commit

Permalink
Implement json format for lookup command
Browse files Browse the repository at this point in the history
- Implement `json` format output for `lookup` command.

- Adjust some documentation wording for cli arguments and commands.

- Also, move result and printing to the parent of the `lookup`
function.
  • Loading branch information
feds01 committed Feb 2, 2025
1 parent 884874a commit 580d8dc
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 13 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions crates/teamsearch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ log = { workspace = true }
once_cell = { workspace = true }
rayon = { workspace = true }
wild = { workspace = true}
serde = { workspace = true }
serde_json = { workspace = true }
20 changes: 19 additions & 1 deletion crates/teamsearch/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub struct FindCommand {
/// }
/// ]
/// ```
#[clap(long, help = "Display the results using a JSON format")]
#[clap(long, help = "Display the results using in JSON format")]
pub json: bool,
}

Expand All @@ -101,4 +101,22 @@ pub struct LookupCommand {
/// Specify the path of the file of the codeowners.
#[clap(long, short, help = "Specify the path of the CODEOWNERS file [default: CODEOWNERS]")]
pub codeowners: PathBuf,

/// Display the results using a JSON format. We output the contents
/// of the search in the following format:
///
/// ```json
/// [
/// {
/// "path": "some/foo/result.rs",
/// "team": "@some-team"
/// },
/// {
/// "path": "some/bar/result.rs",
/// "team": null
/// },
/// ]
/// ```
#[clap(long, help = "Display the results using in JSON format")]
pub json: bool,
}
5 changes: 3 additions & 2 deletions crates/teamsearch/src/commands/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ use teamsearch_workspace::{
settings::{FilePattern, Settings},
};

/// The result of a search.
#[derive(Default, Constructor)]
pub struct FindResult {
pub(crate) struct FindResult {
/// The items that we're found within the files.
pub file_matches: Vec<FileMatches>,
}

pub fn find(
pub(crate) fn find(
files: &[PathBuf],
mut settings: Settings,
team: Vec<String>,
Expand Down
30 changes: 25 additions & 5 deletions crates/teamsearch/src/commands/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,31 @@ use std::{iter::once, path::PathBuf};

use anyhow::Result;
use itertools::Itertools;
use serde::Serialize;
use teamsearch_utils::fs;
use teamsearch_workspace::{codeowners::CodeOwners, settings::Settings};

pub fn lookup(files: &[PathBuf], settings: Settings) -> Result<()> {
/// An lookup entry, representing a file and its corresponding
/// owners.
#[derive(Serialize)]
pub(crate) struct LookupEntry {
/// The owner of the file, if any.
pub(crate) team: Option<String>,

/// The path of the entry.
pub(crate) path: PathBuf,
}

/// The result of an owner lookup.
#[derive(Serialize, Default)]
#[serde(transparent)]
pub(crate) struct LookupResult {
pub(crate) entries: Vec<LookupEntry>,
}

pub fn lookup(files: &[PathBuf], settings: Settings) -> Result<LookupResult> {
if files.is_empty() {
return Ok(());
return Ok(LookupResult::default());
}

// Compute the "root" of all of the paths including the provided paths and the
Expand All @@ -20,11 +39,12 @@ pub fn lookup(files: &[PathBuf], settings: Settings) -> Result<()> {
// extract the given patterns that are specified for the particular team.
let codeowners = CodeOwners::parse_from_file(&settings.codeowners, &root)?;

let mut entries = Vec::new();

// For each path (other than last), we need to find the team that owns it.
for path in paths.iter().take(paths.len() - 1) {
let team = codeowners.lookup(path).unwrap_or("none");
println!("{}: {}", path.display(), team);
entries.push(LookupEntry { path: path.clone(), team: codeowners.lookup(path) });
}

Ok(())
Ok(LookupResult { entries })
}
13 changes: 11 additions & 2 deletions crates/teamsearch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::{
use annotate_snippets::{Level, Renderer, Snippet};
use anyhow::{Ok, Result, anyhow};
use cli::{FindCommand, LookupCommand};
use commands::find::FindResult;
use commands::{find::FindResult, lookup::LookupEntry};
use crash::crash_handler;
use log::info;
use teamsearch_utils::{logging::ToolLogger, stream::CompilerOutputStream};
Expand Down Expand Up @@ -146,7 +146,16 @@ fn lookup(args: LookupCommand) -> Result<ExitStatus> {
}

let settings = Settings::new(true, args.codeowners);
commands::lookup::lookup(&files, settings)?;
let results = commands::lookup::lookup(&files, settings)?;

if args.json {
// Print out the results in JSON format.
println!("{}", serde_json::to_string_pretty(&results)?);
} else {
for LookupEntry { path, team } in results.entries {
info!("{}: {}", path.display(), team.as_ref().map_or("none", |t| t.as_str()))
}
}

Ok(ExitStatus::Success)
}
Expand Down
4 changes: 2 additions & 2 deletions crates/teamsearch_workspace/src/codeowners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl CodeOwners {
}

/// Lookup a file path to see which team owns it.
pub fn lookup(&self, path: &PathBuf) -> Option<&str> {
pub fn lookup(&self, path: &PathBuf) -> Option<String> {
let path = fs::normalize_path(path);

// @@Hack: Check if we're missing a `/` at the end of the path.
Expand All @@ -46,7 +46,7 @@ impl CodeOwners {
let set = self.get_pattern_for_team(owner);

if set.is_match(&path_pat) {
return Some(owner);
return Some(owner.to_string());
}
}

Expand Down

0 comments on commit 580d8dc

Please sign in to comment.