Create dummy format CLI (#4453)

* Create dummy format CLI

* Hide format from clap, too

Missed that this is a separate option from `#[doc(hidden)]`

* Remove cargo feature and replace with warning

* No-alloc files parameter matching

* beta warning: warn -> warn_user_once

* Rephrase warning
This commit is contained in:
konstin 2023-05-19 11:45:52 +02:00 committed by GitHub
parent 2f35099f81
commit 32f1edc555
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 4 deletions

View file

@ -1,10 +1,11 @@
use std::io::{self, BufWriter};
use std::path::PathBuf;
use std::io::{self, stdout, BufWriter, Write};
use std::path::{Path, PathBuf};
use std::process::ExitCode;
use std::sync::mpsc::channel;
use anyhow::Result;
use anyhow::{Context, Result};
use clap::CommandFactory;
use log::warn;
use notify::{recommended_watcher, RecursiveMode, Watcher};
use ruff::logging::{set_up_logging, LogLevel};
@ -13,6 +14,7 @@ use ruff::settings::{flags, CliSettings};
use ruff::{fs, warn_user_once};
use crate::args::{Args, CheckArgs, Command};
use crate::commands::run_stdin::read_from_stdin;
use crate::printer::{Flags as PrinterFlags, Printer};
pub mod args;
@ -117,11 +119,41 @@ quoting the executed command, along with the relevant file contents and `pyproje
shell.generate(&mut Args::command(), &mut io::stdout());
}
Command::Check(args) => return check(args, log_level),
Command::Format { files } => return format(&files),
}
Ok(ExitStatus::Success)
}
fn format(files: &[PathBuf]) -> Result<ExitStatus> {
warn_user_once!(
"`ruff format` is a work-in-progress, subject to change at any time, and intended for \
internal use only."
);
// dummy
let format_code = |code: &str| code.replace("# DEL", "");
match &files {
// Check if we should read from stdin
[path] if path == Path::new("-") => {
let unformatted = read_from_stdin()?;
let formatted = format_code(&unformatted);
stdout().lock().write_all(formatted.as_bytes())?;
}
_ => {
for file in files {
let unformatted = std::fs::read_to_string(file)
.with_context(|| format!("Could not read {}: ", file.display()))?;
let formatted = format_code(&unformatted);
std::fs::write(file, formatted)
.with_context(|| format!("Could not write to {}, exiting", file.display()))?;
}
}
}
Ok(ExitStatus::Success)
}
fn check(args: CheckArgs, log_level: LogLevel) -> Result<ExitStatus> {
#[cfg(feature = "ecosystem_ci")]
let ecosystem_ci = args.ecosystem_ci;