-
Notifications
You must be signed in to change notification settings - Fork 106
refactor(cli): simplify abstractions #574
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
Changes from 3 commits
880812f
60edbaf
bd5ff58
888356d
3f04408
99c2264
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,76 +1,141 @@ | ||
| use crate::cli_options::CliOptions; | ||
| use crate::{CliDiagnostic, Execution, TraversalMode}; | ||
| use biome_deserialize::Merge; | ||
| use crate::commands::get_files_to_process_with_cli_options; | ||
| use crate::execute::{StdinPayload, run_files, run_stdin}; | ||
| use crate::reporter::Report; | ||
| use crate::{CliDiagnostic, CliSession, VcsIntegration}; | ||
| use crate::{ExecutionConfig, ExecutionMode, VcsTargeting}; | ||
| use pgt_configuration::PartialConfiguration; | ||
| use pgt_console::Console; | ||
| use pgt_diagnostics::category; | ||
| use pgt_fs::FileSystem; | ||
| use pgt_workspace::{DynRef, Workspace, WorkspaceError, configuration::LoadedConfiguration}; | ||
| use pgt_workspace::DynRef; | ||
| use std::ffi::OsString; | ||
|
|
||
| use super::{CommandRunner, get_files_to_process_with_cli_options}; | ||
| #[allow(dead_code)] | ||
| pub struct CheckArgs { | ||
| pub configuration: Option<PartialConfiguration>, | ||
| pub paths: Vec<OsString>, | ||
| pub stdin_file_path: Option<String>, | ||
| pub staged: bool, | ||
| pub changed: bool, | ||
| pub since: Option<String>, | ||
| pub apply: bool, | ||
| pub apply_unsafe: bool, | ||
|
||
| } | ||
|
|
||
| pub fn check( | ||
| mut session: CliSession, | ||
| cli_options: &CliOptions, | ||
| args: CheckArgs, | ||
| ) -> Result<(), CliDiagnostic> { | ||
| validate_args(&args)?; | ||
|
|
||
| let configuration = session.prepare_with_config(cli_options, args.configuration.clone())?; | ||
| session.setup_workspace(configuration.clone(), VcsIntegration::Enabled)?; | ||
|
|
||
| let paths = resolve_paths(session.fs(), &configuration, &args)?; | ||
|
|
||
| let vcs = VcsTargeting { | ||
| staged: args.staged, | ||
| changed: args.changed, | ||
| }; | ||
|
|
||
| let max_diagnostics = if cli_options.reporter.is_default() { | ||
| cli_options.max_diagnostics.into() | ||
| } else { | ||
| u32::MAX | ||
| }; | ||
|
|
||
| let mode = ExecutionMode::Check { vcs }; | ||
| let execution = ExecutionConfig::new(mode, max_diagnostics); | ||
|
|
||
| pub(crate) struct CheckCommandPayload { | ||
| pub(crate) configuration: Option<PartialConfiguration>, | ||
| pub(crate) paths: Vec<OsString>, | ||
| pub(crate) stdin_file_path: Option<String>, | ||
| pub(crate) staged: bool, | ||
| pub(crate) changed: bool, | ||
| pub(crate) since: Option<String>, | ||
| if let Some(stdin_path) = args.stdin_file_path.as_deref() { | ||
| let payload = read_stdin_payload(stdin_path, session.console())?; | ||
| run_stdin(&mut session, &execution, payload) | ||
| } else { | ||
| let report: Report = run_files(&mut session, &execution, paths)?; | ||
|
|
||
| let exit_result = enforce_exit_codes(cli_options, &report); | ||
| session.report("check", cli_options, &report)?; | ||
| exit_result | ||
| } | ||
| } | ||
|
|
||
| impl CommandRunner for CheckCommandPayload { | ||
| const COMMAND_NAME: &'static str = "check"; | ||
|
|
||
| fn merge_configuration( | ||
| &mut self, | ||
| loaded_configuration: LoadedConfiguration, | ||
| _fs: &DynRef<'_, dyn FileSystem>, | ||
| _console: &mut dyn Console, | ||
| ) -> Result<PartialConfiguration, WorkspaceError> { | ||
| let LoadedConfiguration { | ||
| configuration: mut fs_configuration, | ||
| .. | ||
| } = loaded_configuration; | ||
|
|
||
| if let Some(configuration) = self.configuration.clone() { | ||
| // overwrite fs config with cli args | ||
| fs_configuration.merge_with(configuration); | ||
| fn resolve_paths( | ||
| fs: &DynRef<'_, dyn FileSystem>, | ||
| configuration: &PartialConfiguration, | ||
| args: &CheckArgs, | ||
| ) -> Result<Vec<OsString>, CliDiagnostic> { | ||
| let mut paths = get_files_to_process_with_cli_options( | ||
| args.since.as_deref(), | ||
| args.changed, | ||
| args.staged, | ||
| fs, | ||
| configuration, | ||
| )? | ||
| .unwrap_or_else(|| args.paths.clone()); | ||
|
|
||
| if paths.is_empty() && args.stdin_file_path.is_none() { | ||
| if let Some(current_dir) = fs.working_directory() { | ||
| paths.push(current_dir.into_os_string()); | ||
| } | ||
| } | ||
|
|
||
| Ok(fs_configuration) | ||
| Ok(paths) | ||
| } | ||
|
|
||
| fn read_stdin_payload( | ||
| path: &str, | ||
| console: &mut dyn Console, | ||
| ) -> Result<StdinPayload, CliDiagnostic> { | ||
| let input_code = console.read(); | ||
| if let Some(input_code) = input_code { | ||
| Ok(StdinPayload { | ||
| path: path.into(), | ||
| content: input_code, | ||
| }) | ||
| } else { | ||
| Err(CliDiagnostic::missing_argument("stdin", "check")) | ||
| } | ||
| } | ||
|
|
||
| fn enforce_exit_codes(cli_options: &CliOptions, payload: &Report) -> Result<(), CliDiagnostic> { | ||
| let traversal = payload.traversal.as_ref(); | ||
| let processed = traversal.map_or(0, |t| t.changed + t.unchanged); | ||
| let skipped = traversal.map_or(0, |t| t.skipped); | ||
|
|
||
| fn get_files_to_process( | ||
| &self, | ||
| fs: &DynRef<'_, dyn FileSystem>, | ||
| configuration: &PartialConfiguration, | ||
| ) -> Result<Vec<OsString>, CliDiagnostic> { | ||
| let paths = get_files_to_process_with_cli_options( | ||
| self.since.as_deref(), | ||
| self.changed, | ||
| self.staged, | ||
| fs, | ||
| configuration, | ||
| )? | ||
| .unwrap_or(self.paths.clone()); | ||
|
|
||
| Ok(paths) | ||
| if processed.saturating_sub(skipped) == 0 && !cli_options.no_errors_on_unmatched { | ||
| return Err(CliDiagnostic::no_files_processed()); | ||
| } | ||
|
|
||
| fn get_stdin_file_path(&self) -> Option<&str> { | ||
| self.stdin_file_path.as_deref() | ||
| let warnings = payload.warnings; | ||
| let errors = payload.errors; | ||
| let category = category!("check"); | ||
|
|
||
| if errors > 0 { | ||
| return Err(CliDiagnostic::check_error(category)); | ||
| } | ||
|
|
||
| fn get_execution( | ||
| &self, | ||
| cli_options: &CliOptions, | ||
| console: &mut dyn Console, | ||
| _workspace: &dyn Workspace, | ||
| ) -> Result<Execution, CliDiagnostic> { | ||
| Ok(Execution::new(TraversalMode::Check { | ||
| stdin: self.get_stdin(console)?, | ||
| vcs_targeted: (self.staged, self.changed).into(), | ||
| }) | ||
| .set_report(cli_options)) | ||
| if warnings > 0 && cli_options.error_on_warnings { | ||
| return Err(CliDiagnostic::check_warnings(category)); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| fn validate_args(args: &CheckArgs) -> Result<(), CliDiagnostic> { | ||
| if args.since.is_some() { | ||
| if !args.changed { | ||
| return Err(CliDiagnostic::incompatible_arguments("since", "changed")); | ||
| } | ||
| if args.staged { | ||
| return Err(CliDiagnostic::incompatible_arguments("since", "staged")); | ||
| } | ||
| } | ||
|
|
||
| if args.changed && args.staged { | ||
| return Err(CliDiagnostic::incompatible_arguments("changed", "staged")); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| use crate::cli_options::CliOptions; | ||
| use crate::reporter::Report; | ||
| use crate::{CliDiagnostic, CliSession, VcsIntegration}; | ||
| use pgt_configuration::PartialConfiguration; | ||
|
|
||
| pub fn dblint( | ||
| mut session: CliSession, | ||
| cli_options: &CliOptions, | ||
| cli_configuration: Option<PartialConfiguration>, | ||
| ) -> Result<(), CliDiagnostic> { | ||
| let configuration = session.prepare_with_config(cli_options, cli_configuration)?; | ||
| session.setup_workspace(configuration, VcsIntegration::Disabled)?; | ||
|
|
||
| // TODO: Implement actual dblint logic here | ||
| let report = Report::new(vec![], std::time::Duration::new(0, 0), 0, None); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is clearly a peak MVP |
||
|
|
||
| session.report("dblint", cli_options, &report) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.