-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[CLI] Check with multiple arguments #91
Merged
kylewlacy
merged 4 commits into
brioche-dev:main
from
jaudiger:check-multiple-arguments
Jul 17, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One thing, I didn't touch this line, but should I?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is fine as-is, is there a reason it should be changed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I don't think so. I just wanted to raise the attention to this single call to
tracing::info
.