mirror of
https://github.com/RGBCube/alejandra
synced 2025-07-30 12:07:46 +00:00
refac: use clap's builtin env reader
This commit is contained in:
parent
3ce9e7d115
commit
d6ac7ad4d0
4 changed files with 15 additions and 18 deletions
|
@ -7,6 +7,7 @@ alejandra = { path = "../alejandra" }
|
||||||
clap = { version = "*", default-features = false, features = [
|
clap = { version = "*", default-features = false, features = [
|
||||||
"color",
|
"color",
|
||||||
"derive",
|
"derive",
|
||||||
|
"env",
|
||||||
"std",
|
"std",
|
||||||
"strsim",
|
"strsim",
|
||||||
] }
|
] }
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use std::env;
|
|
||||||
use std::fs::read_to_string;
|
use std::fs::read_to_string;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
|
|
||||||
|
@ -49,7 +48,11 @@ struct CLIArgs {
|
||||||
|
|
||||||
/// Number of formatting threads to spawn. Defaults to the number of
|
/// Number of formatting threads to spawn. Defaults to the number of
|
||||||
/// physical CPUs.
|
/// physical CPUs.
|
||||||
#[clap(long, short, value_parser = value_parser!(u8).range(1..))]
|
#[clap(
|
||||||
|
long, short,
|
||||||
|
env = "ALEJANDRA_THREADS",
|
||||||
|
value_parser = value_parser!(u8).range(1..),
|
||||||
|
)]
|
||||||
threads: Option<u8>,
|
threads: Option<u8>,
|
||||||
|
|
||||||
/// Use once to hide informational messages,
|
/// Use once to hide informational messages,
|
||||||
|
@ -146,15 +149,8 @@ pub fn main() -> ! {
|
||||||
let include: Vec<&str> =
|
let include: Vec<&str> =
|
||||||
args.include.iter().map(String::as_str).collect::<Vec<&str>>();
|
args.include.iter().map(String::as_str).collect::<Vec<&str>>();
|
||||||
|
|
||||||
// Try CLI value, then env var, then fall back to number of physical CPUs.
|
let threads =
|
||||||
let threads: usize = if let Some(cli_threads) = args.threads {
|
args.threads.map_or_else(num_cpus::get_physical, Into::<usize>::into);
|
||||||
cli_threads.into() // convert u8 to usize
|
|
||||||
} else {
|
|
||||||
env::var("ALEJANDRA_THREADS")
|
|
||||||
.ok()
|
|
||||||
.and_then(|v| v.parse::<usize>().ok())
|
|
||||||
.unwrap_or_else(num_cpus::get_physical)
|
|
||||||
};
|
|
||||||
|
|
||||||
let verbosity = match args.quiet {
|
let verbosity = match args.quiet {
|
||||||
0 => Verbosity::Everything,
|
0 => Verbosity::Everything,
|
||||||
|
|
|
@ -128,7 +128,7 @@ fn cases() {
|
||||||
output_got.push_str(&format!("args: {:?}\n", case.args));
|
output_got.push_str(&format!("args: {:?}\n", case.args));
|
||||||
|
|
||||||
let mut child = Command::new("cargo")
|
let mut child = Command::new("cargo")
|
||||||
.env("ALEJANDRA_THREADS", "4")
|
.env("ALEJANDRA_THREADS", "1")
|
||||||
.args(["run", "--quiet", "--"])
|
.args(["run", "--quiet", "--"])
|
||||||
.args(case.args)
|
.args(case.args)
|
||||||
.stdin(Stdio::piped())
|
.stdin(Stdio::piped())
|
||||||
|
|
|
@ -33,7 +33,7 @@ stdout:
|
||||||
|
|
||||||
-t, --threads <THREADS>
|
-t, --threads <THREADS>
|
||||||
Number of formatting threads to spawn. Defaults to the number of
|
Number of formatting threads to spawn. Defaults to the number of
|
||||||
physical CPUs
|
physical CPUs [env: ALEJANDRA_THREADS=1]
|
||||||
|
|
||||||
-V, --version
|
-V, --version
|
||||||
Print version information
|
Print version information
|
||||||
|
@ -184,7 +184,7 @@ exit code: Some(1)
|
||||||
===
|
===
|
||||||
args: [".", "--exclude", "."]
|
args: [".", "--exclude", "."]
|
||||||
stderr:
|
stderr:
|
||||||
Checking style in 0 files using 4 threads.
|
Checking style in 0 files using 1 thread.
|
||||||
|
|
||||||
|
|
||||||
Congratulations! Your code complies with the Alejandra style.
|
Congratulations! Your code complies with the Alejandra style.
|
||||||
|
@ -203,7 +203,7 @@ stdout:
|
||||||
Requires formatting: tests/inputs/changed.nix
|
Requires formatting: tests/inputs/changed.nix
|
||||||
|
|
||||||
stderr:
|
stderr:
|
||||||
Checking style in 1 file using 4 threads.
|
Checking style in 1 file using 1 thread.
|
||||||
|
|
||||||
|
|
||||||
Alert! 1 file requires formatting.
|
Alert! 1 file requires formatting.
|
||||||
|
@ -218,7 +218,7 @@ stdout:
|
||||||
Requires formatting: tests/inputs/changed.nix
|
Requires formatting: tests/inputs/changed.nix
|
||||||
|
|
||||||
stderr:
|
stderr:
|
||||||
Checking style in 1 file using 4 threads.
|
Checking style in 1 file using 1 thread.
|
||||||
|
|
||||||
|
|
||||||
Alert! 1 file requires formatting.
|
Alert! 1 file requires formatting.
|
||||||
|
@ -233,7 +233,7 @@ exit code: Some(2)
|
||||||
===
|
===
|
||||||
args: ["-c", "tests/inputs/unchanged.nix"]
|
args: ["-c", "tests/inputs/unchanged.nix"]
|
||||||
stderr:
|
stderr:
|
||||||
Checking style in 1 file using 4 threads.
|
Checking style in 1 file using 1 thread.
|
||||||
|
|
||||||
|
|
||||||
Congratulations! Your code complies with the Alejandra style.
|
Congratulations! Your code complies with the Alejandra style.
|
||||||
|
@ -249,7 +249,7 @@ exit code: Some(0)
|
||||||
===
|
===
|
||||||
args: ["--check", "tests/inputs/error.nix"]
|
args: ["--check", "tests/inputs/error.nix"]
|
||||||
stderr:
|
stderr:
|
||||||
Checking style in 1 file using 4 threads.
|
Checking style in 1 file using 1 thread.
|
||||||
|
|
||||||
|
|
||||||
Failed! 1 error found at:
|
Failed! 1 error found at:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue