|
1 | 1 | use crate::changed::{get_changed_files, get_staged_files}; |
2 | 2 | use crate::cli_options::{CliOptions, CliReporter, ColorsArg, cli_options}; |
3 | | -use crate::execute::Stdin; |
4 | 3 | use crate::logging::LoggingKind; |
5 | | -use crate::{ |
6 | | - CliDiagnostic, CliSession, Execution, LoggingLevel, VERSION, execute_mode, setup_cli_subscriber, |
7 | | -}; |
| 4 | +use crate::{CliDiagnostic, LoggingLevel, VERSION}; |
8 | 5 | use bpaf::Bpaf; |
9 | 6 | use pgt_configuration::{PartialConfiguration, partial_configuration}; |
10 | | -use pgt_console::{Console, ConsoleExt, markup}; |
11 | | -use pgt_fs::{ConfigName, FileSystem}; |
12 | | -use pgt_workspace::PartialConfigurationExt; |
13 | | -use pgt_workspace::configuration::{LoadedConfiguration, load_configuration}; |
14 | | -use pgt_workspace::workspace::{RegisterProjectFolderParams, UpdateSettingsParams}; |
15 | | -use pgt_workspace::{DynRef, Workspace, WorkspaceError}; |
| 7 | +use pgt_fs::FileSystem; |
| 8 | +use pgt_workspace::DynRef; |
16 | 9 | use std::ffi::OsString; |
17 | 10 | use std::path::PathBuf; |
18 | 11 | pub(crate) mod check; |
19 | 12 | pub(crate) mod clean; |
20 | 13 | pub(crate) mod daemon; |
| 14 | +pub(crate) mod dblint; |
21 | 15 | pub(crate) mod init; |
22 | 16 | pub(crate) mod version; |
23 | 17 |
|
@@ -286,145 +280,7 @@ impl PgtCommand { |
286 | 280 | } |
287 | 281 | } |
288 | 282 |
|
289 | | -/// Generic interface for executing commands. |
290 | | -/// |
291 | | -/// Consumers must implement the following methods: |
292 | | -/// |
293 | | -/// - [CommandRunner::merge_configuration] |
294 | | -/// - [CommandRunner::get_files_to_process] |
295 | | -/// - [CommandRunner::get_stdin_file_path] |
296 | | -/// - [CommandRunner::should_write] |
297 | | -/// - [CommandRunner::get_execution] |
298 | | -/// |
299 | | -/// Optional methods: |
300 | | -/// - [CommandRunner::check_incompatible_arguments] |
301 | | -pub(crate) trait CommandRunner: Sized { |
302 | | - const COMMAND_NAME: &'static str; |
303 | | - |
304 | | - /// The main command to use. |
305 | | - fn run(&mut self, session: CliSession, cli_options: &CliOptions) -> Result<(), CliDiagnostic> { |
306 | | - setup_cli_subscriber(cli_options.log_level, cli_options.log_kind); |
307 | | - let fs = &session.app.fs; |
308 | | - let console = &mut *session.app.console; |
309 | | - let workspace = &*session.app.workspace; |
310 | | - self.check_incompatible_arguments()?; |
311 | | - let (execution, paths) = self.configure_workspace(fs, console, workspace, cli_options)?; |
312 | | - execute_mode(execution, session, cli_options, paths) |
313 | | - } |
314 | | - |
315 | | - /// This function prepares the workspace with the following: |
316 | | - /// - Loading the configuration file. |
317 | | - /// - Configure the VCS integration |
318 | | - /// - Computes the paths to traverse/handle. This changes based on the VCS arguments that were passed. |
319 | | - /// - Register a project folder using the working directory. |
320 | | - /// - Updates the settings that belong to the project registered |
321 | | - fn configure_workspace( |
322 | | - &mut self, |
323 | | - fs: &DynRef<'_, dyn FileSystem>, |
324 | | - console: &mut dyn Console, |
325 | | - workspace: &dyn Workspace, |
326 | | - cli_options: &CliOptions, |
327 | | - ) -> Result<(Execution, Vec<OsString>), CliDiagnostic> { |
328 | | - let loaded_configuration = |
329 | | - load_configuration(fs, cli_options.as_configuration_path_hint())?; |
330 | | - |
331 | | - // Check for deprecated config filename |
332 | | - if let Some(config_path) = &loaded_configuration.file_path { |
333 | | - if let Some(file_name) = config_path.file_name().and_then(|n| n.to_str()) { |
334 | | - if ConfigName::is_deprecated(file_name) { |
335 | | - console.log(markup! { |
336 | | - <Warn>"Warning: "</Warn>"You are using the deprecated config filename '"<Emphasis>"postgrestools.jsonc"</Emphasis>"'. \ |
337 | | - Please rename it to '"<Emphasis>"postgres-language-server.jsonc"</Emphasis>"'. \ |
338 | | - Support for the old filename will be removed in a future version.\n" |
339 | | - }); |
340 | | - } |
341 | | - } |
342 | | - } |
343 | | - |
344 | | - let configuration_path = loaded_configuration.directory_path.clone(); |
345 | | - let configuration = self.merge_configuration(loaded_configuration, fs, console)?; |
346 | | - let vcs_base_path = configuration_path.or(fs.working_directory()); |
347 | | - let (vcs_base_path, gitignore_matches) = |
348 | | - configuration.retrieve_gitignore_matches(fs, vcs_base_path.as_deref())?; |
349 | | - let paths = self.get_files_to_process(fs, &configuration)?; |
350 | | - workspace.register_project_folder(RegisterProjectFolderParams { |
351 | | - path: fs.working_directory(), |
352 | | - set_as_current_workspace: true, |
353 | | - })?; |
354 | | - |
355 | | - workspace.update_settings(UpdateSettingsParams { |
356 | | - workspace_directory: fs.working_directory(), |
357 | | - configuration, |
358 | | - vcs_base_path, |
359 | | - gitignore_matches, |
360 | | - })?; |
361 | | - |
362 | | - let execution = self.get_execution(cli_options, console, workspace)?; |
363 | | - Ok((execution, paths)) |
364 | | - } |
365 | | - |
366 | | - /// Computes [Stdin] if the CLI has the necessary information. |
367 | | - /// |
368 | | - /// ## Errors |
369 | | - /// - If the user didn't provide anything via `stdin` but the option `--stdin-file-path` is passed. |
370 | | - fn get_stdin(&self, console: &mut dyn Console) -> Result<Option<Stdin>, CliDiagnostic> { |
371 | | - let stdin = if let Some(stdin_file_path) = self.get_stdin_file_path() { |
372 | | - let input_code = console.read(); |
373 | | - if let Some(input_code) = input_code { |
374 | | - let path = PathBuf::from(stdin_file_path); |
375 | | - Some((path, input_code).into()) |
376 | | - } else { |
377 | | - // we provided the argument without a piped stdin, we bail |
378 | | - return Err(CliDiagnostic::missing_argument("stdin", Self::COMMAND_NAME)); |
379 | | - } |
380 | | - } else { |
381 | | - None |
382 | | - }; |
383 | | - |
384 | | - Ok(stdin) |
385 | | - } |
386 | | - |
387 | | - // Below, the methods that consumers must implement. |
388 | | - |
389 | | - /// Implements this method if you need to merge CLI arguments to the loaded configuration. |
390 | | - /// |
391 | | - /// The CLI arguments take precedence over the option configured in the configuration file. |
392 | | - fn merge_configuration( |
393 | | - &mut self, |
394 | | - loaded_configuration: LoadedConfiguration, |
395 | | - fs: &DynRef<'_, dyn FileSystem>, |
396 | | - console: &mut dyn Console, |
397 | | - ) -> Result<PartialConfiguration, WorkspaceError>; |
398 | | - |
399 | | - /// It returns the paths that need to be handled/traversed. |
400 | | - fn get_files_to_process( |
401 | | - &self, |
402 | | - fs: &DynRef<'_, dyn FileSystem>, |
403 | | - configuration: &PartialConfiguration, |
404 | | - ) -> Result<Vec<OsString>, CliDiagnostic>; |
405 | | - |
406 | | - /// It returns the file path to use in `stdin` mode. |
407 | | - fn get_stdin_file_path(&self) -> Option<&str>; |
408 | | - |
409 | | - /// Returns the [Execution] mode. |
410 | | - fn get_execution( |
411 | | - &self, |
412 | | - cli_options: &CliOptions, |
413 | | - console: &mut dyn Console, |
414 | | - workspace: &dyn Workspace, |
415 | | - ) -> Result<Execution, CliDiagnostic>; |
416 | | - |
417 | | - // Below, methods that consumers can implement |
418 | | - |
419 | | - /// Optional method that can be implemented to check if some CLI arguments aren't compatible. |
420 | | - /// |
421 | | - /// The method is called before loading the configuration from disk. |
422 | | - fn check_incompatible_arguments(&self) -> Result<(), CliDiagnostic> { |
423 | | - Ok(()) |
424 | | - } |
425 | | -} |
426 | | - |
427 | | -fn get_files_to_process_with_cli_options( |
| 283 | +pub(crate) fn get_files_to_process_with_cli_options( |
428 | 284 | since: Option<&str>, |
429 | 285 | changed: bool, |
430 | 286 | staged: bool, |
|
0 commit comments