-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CLI] Check with multiple arguments (#91)
* feat: add ability to specify multiple registries and projects while doing a check Signed-off-by: jaudiger <jeremy.audiger@ioterop.com> * refactor: uniformize with what is done in the check function Signed-off-by: jaudiger <jeremy.audiger@ioterop.com> * chore: use registry_project instead of registry Signed-off-by: jaudiger <jeremy.audiger@ioterop.com> * chore: remove a unused import. Signed-off-by: jaudiger <jeremy.audiger@ioterop.com> --------- Signed-off-by: jaudiger <jeremy.audiger@ioterop.com>
- Loading branch information
Showing
3 changed files
with
174 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,128 @@ | ||
use std::path::PathBuf; | ||
use std::process::ExitCode; | ||
|
||
use brioche_core::project::ProjectHash; | ||
use brioche_core::project::Projects; | ||
use brioche_core::reporter::ConsoleReporterKind; | ||
use brioche_core::reporter::Reporter; | ||
use brioche_core::Brioche; | ||
use clap::Parser; | ||
use tracing::Instrument; | ||
|
||
use crate::consolidate_result; | ||
|
||
#[derive(Debug, Parser)] | ||
pub struct CheckArgs { | ||
#[command(flatten)] | ||
project: super::ProjectArgs, | ||
project: super::MultipleProjectArgs, | ||
} | ||
|
||
pub async fn check(args: CheckArgs) -> anyhow::Result<ExitCode> { | ||
let (reporter, mut guard) = | ||
brioche_core::reporter::start_console_reporter(ConsoleReporterKind::Auto)?; | ||
|
||
let brioche = brioche_core::BriocheBuilder::new(reporter).build().await?; | ||
let brioche = brioche_core::BriocheBuilder::new(reporter.clone()) | ||
.build() | ||
.await?; | ||
let projects = brioche_core::project::Projects::default(); | ||
|
||
let check_future = async { | ||
let project_hash = super::load_project(&brioche, &projects, &args.project).await?; | ||
let mut error_result = Option::None; | ||
|
||
let num_lockfiles_updated = projects.commit_dirty_lockfiles().await?; | ||
if num_lockfiles_updated > 0 { | ||
tracing::info!(num_lockfiles_updated, "updated lockfiles"); | ||
} | ||
// Handle the case where no projects and no registries are specified | ||
let projects_path = | ||
if args.project.project.is_empty() && args.project.registry_project.is_empty() { | ||
vec![PathBuf::from(".")] | ||
} else { | ||
args.project.project | ||
}; | ||
|
||
let checked = brioche_core::script::check::check(&brioche, &projects, project_hash).await?; | ||
// Loop over the projects | ||
for project_path in projects_path { | ||
let project_name = format!("project '{name}'", name = project_path.display()); | ||
|
||
guard.shutdown_console().await; | ||
match projects.load(&brioche, &project_path, true).await { | ||
Ok(project_hash) => { | ||
let result = | ||
run_check(&reporter, &brioche, &projects, project_hash, &project_name).await; | ||
consolidate_result(&reporter, &project_name, result, &mut error_result); | ||
} | ||
Err(e) => { | ||
consolidate_result(&reporter, &project_name, Err(e), &mut error_result); | ||
} | ||
} | ||
} | ||
|
||
let result = checked.ensure_ok(brioche_core::script::check::DiagnosticLevel::Message); | ||
// Loop over the registry projects | ||
for registry_project in args.project.registry_project { | ||
let project_name = format!("registry project '{registry_project}'"); | ||
|
||
match result { | ||
Ok(()) => { | ||
println!("No errors found 🎉"); | ||
anyhow::Ok(ExitCode::SUCCESS) | ||
match projects | ||
.load_from_registry( | ||
&brioche, | ||
®istry_project, | ||
&brioche_core::project::Version::Any, | ||
) | ||
.await | ||
{ | ||
Ok(project_hash) => { | ||
let result = | ||
run_check(&reporter, &brioche, &projects, project_hash, &project_name).await; | ||
consolidate_result(&reporter, &project_name, result, &mut error_result); | ||
} | ||
Err(diagnostics) => { | ||
diagnostics.write(&brioche.vfs, &mut std::io::stdout())?; | ||
anyhow::Ok(ExitCode::FAILURE) | ||
Err(e) => { | ||
consolidate_result(&reporter, &project_name, Err(e), &mut error_result); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
let exit_code = check_future | ||
.instrument(tracing::info_span!("check")) | ||
.await?; | ||
guard.shutdown_console().await; | ||
|
||
let exit_code = if error_result.is_some() { | ||
ExitCode::FAILURE | ||
} else { | ||
ExitCode::SUCCESS | ||
}; | ||
|
||
Ok(exit_code) | ||
} | ||
|
||
async fn run_check( | ||
reporter: &Reporter, | ||
brioche: &Brioche, | ||
projects: &Projects, | ||
project_hash: ProjectHash, | ||
project_name: &String, | ||
) -> Result<bool, anyhow::Error> { | ||
let num_lockfiles_updated = projects.commit_dirty_lockfiles().await?; | ||
if num_lockfiles_updated > 0 { | ||
tracing::info!(num_lockfiles_updated, "updated lockfiles"); | ||
} | ||
|
||
let result = | ||
async { brioche_core::script::check::check(brioche, projects, project_hash).await } | ||
.instrument(tracing::info_span!("check")) | ||
.await? | ||
.ensure_ok(brioche_core::script::check::DiagnosticLevel::Message); | ||
|
||
match result { | ||
Ok(()) => { | ||
reporter.emit(superconsole::Lines::from_multiline_string( | ||
&format!("No errors found in {project_name} 🎉",), | ||
superconsole::style::ContentStyle::default(), | ||
)); | ||
|
||
Ok(true) | ||
} | ||
Err(diagnostics) => { | ||
let mut output = Vec::new(); | ||
diagnostics.write(&brioche.vfs, &mut output)?; | ||
|
||
reporter.emit(superconsole::Lines::from_multiline_string( | ||
&String::from_utf8(output)?, | ||
superconsole::style::ContentStyle::default(), | ||
)); | ||
|
||
Ok(false) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters