Skip to content

Commit

Permalink
refactor: extract function
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Correa Casablanca <[email protected]>
  • Loading branch information
castarco committed Apr 11, 2024
1 parent a96c855 commit a0e859b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 55 deletions.
24 changes: 7 additions & 17 deletions crates/biome_cli/src/commands/check.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::changed::{get_changed_files, get_staged_files};
use crate::cli_options::CliOptions;
use crate::commands::{get_stdin, resolve_manifest, validate_configuration_diagnostics};
use crate::commands::{
get_files_to_process, get_stdin, resolve_manifest, validate_configuration_diagnostics,
};
use crate::{
execute_mode, setup_cli_subscriber, CliDiagnostic, CliSession, Execution, TraversalMode,
};
Expand Down Expand Up @@ -122,23 +123,12 @@ pub(crate) fn check(

let stdin = get_stdin(stdin_file_path, &mut *session.app.console, "check")?;

if since.is_some() {
if !changed {
return Err(CliDiagnostic::incompatible_arguments("since", "changed"));
}
if staged {
return Err(CliDiagnostic::incompatible_arguments("since", "staged"));
}
if let Some(_paths) =
get_files_to_process(since, changed, staged, &session.app.fs, &fs_configuration)?
{
paths = _paths;
}

if changed {
if staged {
return Err(CliDiagnostic::incompatible_arguments("changed", "staged"));
}
paths = get_changed_files(&session.app.fs, &fs_configuration, since)?;
} else if staged {
paths = get_staged_files(&session.app.fs)?;
}
session
.app
.workspace
Expand Down
25 changes: 7 additions & 18 deletions crates/biome_cli/src/commands/format.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::changed::{get_changed_files, get_staged_files};
use crate::cli_options::CliOptions;
use crate::commands::{get_stdin, resolve_manifest, validate_configuration_diagnostics};
use crate::commands::{
get_files_to_process, get_stdin, resolve_manifest, validate_configuration_diagnostics,
};
use crate::diagnostics::DeprecatedArgument;
use crate::execute::ReportMode;
use crate::{
Expand Down Expand Up @@ -159,22 +160,10 @@ pub(crate) fn format(
let (vcs_base_path, gitignore_matches) =
configuration.retrieve_gitignore_matches(&session.app.fs, vcs_base_path.as_deref())?;

if since.is_some() {
if !changed {
return Err(CliDiagnostic::incompatible_arguments("since", "changed"));
}
if staged {
return Err(CliDiagnostic::incompatible_arguments("since", "staged"));
}
}

if changed {
if staged {
return Err(CliDiagnostic::incompatible_arguments("changed", "staged"));
}
paths = get_changed_files(&session.app.fs, &configuration, since)?;
} else if staged {
paths = get_staged_files(&session.app.fs)?;
if let Some(_paths) =
get_files_to_process(since, changed, staged, &session.app.fs, &configuration)?
{
paths = _paths;
}

session
Expand Down
25 changes: 7 additions & 18 deletions crates/biome_cli/src/commands/lint.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::changed::{get_changed_files, get_staged_files};
use crate::cli_options::CliOptions;
use crate::commands::{get_stdin, resolve_manifest, validate_configuration_diagnostics};
use crate::commands::{
get_files_to_process, get_stdin, resolve_manifest, validate_configuration_diagnostics,
};
use crate::{
execute_mode, setup_cli_subscriber, CliDiagnostic, CliSession, Execution, TraversalMode,
};
Expand Down Expand Up @@ -97,22 +98,10 @@ pub(crate) fn lint(session: CliSession, payload: LintCommandPayload) -> Result<(
let (vcs_base_path, gitignore_matches) =
fs_configuration.retrieve_gitignore_matches(&session.app.fs, vcs_base_path.as_deref())?;

if since.is_some() {
if !changed {
return Err(CliDiagnostic::incompatible_arguments("since", "changed"));
}
if staged {
return Err(CliDiagnostic::incompatible_arguments("since", "staged"));
}
}

if changed {
if staged {
return Err(CliDiagnostic::incompatible_arguments("changed", "staged"));
}
paths = get_changed_files(&session.app.fs, &fs_configuration, since)?;
} else if staged {
paths = get_staged_files(&session.app.fs)?;
if let Some(_paths) =
get_files_to_process(since, changed, staged, &session.app.fs, &fs_configuration)?
{
paths = _paths;
}

let stdin = get_stdin(stdin_file_path, &mut *session.app.console, "lint")?;
Expand Down
33 changes: 31 additions & 2 deletions crates/biome_cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::changed::{get_changed_files, get_staged_files};
use crate::cli_options::{cli_options, CliOptions, ColorsArg};
use crate::diagnostics::DeprecatedConfigurationFile;
use crate::execute::Stdin;
Expand All @@ -14,11 +15,11 @@ use biome_configuration::{
use biome_configuration::{ConfigurationDiagnostic, PartialConfiguration};
use biome_console::{markup, Console, ConsoleExt};
use biome_diagnostics::{Diagnostic, PrintDiagnostic};
use biome_fs::BiomePath;
use biome_fs::{BiomePath, FileSystem};
use biome_service::configuration::LoadedConfiguration;
use biome_service::documentation::Doc;
use biome_service::workspace::{OpenProjectParams, UpdateProjectParams};
use biome_service::WorkspaceError;
use biome_service::{DynRef, WorkspaceError};
use bpaf::Bpaf;
use std::ffi::OsString;
use std::path::PathBuf;
Expand Down Expand Up @@ -524,6 +525,34 @@ pub(crate) fn get_stdin(
Ok(stdin)
}

fn get_files_to_process(
since: Option<String>,
changed: bool,
staged: bool,
fs: &DynRef<'_, dyn FileSystem>,
configuration: &PartialConfiguration,
) -> Result<Option<Vec<OsString>>, CliDiagnostic> {
if since.is_some() {
if !changed {
return Err(CliDiagnostic::incompatible_arguments("since", "changed"));
}
if staged {
return Err(CliDiagnostic::incompatible_arguments("since", "staged"));
}
}

if changed {
if staged {
return Err(CliDiagnostic::incompatible_arguments("changed", "staged"));
}
Ok(Some(get_changed_files(fs, configuration, since)?))
} else if staged {
Ok(Some(get_staged_files(fs)?))
} else {
Ok(None)
}
}

/// Tests that all CLI options adhere to the invariants expected by `bpaf`.
#[test]
fn check_options() {
Expand Down

0 comments on commit a0e859b

Please sign in to comment.