From cde1e886d6873c607ce3fcff8a06866f12958ba5 Mon Sep 17 00:00:00 2001 From: Timothy Mertz Date: Mon, 9 Oct 2023 08:20:31 -0500 Subject: [PATCH] cli: Add ability for autofmt to read stdin (#1529) This change adds the ability for autofmt to read from stdin by using '-f -' or '--file -' --- packages/cli/src/cli/autoformat.rs | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/packages/cli/src/cli/autoformat.rs b/packages/cli/src/cli/autoformat.rs index f436ab4c4..1d1cbcd24 100644 --- a/packages/cli/src/cli/autoformat.rs +++ b/packages/cli/src/cli/autoformat.rs @@ -46,18 +46,28 @@ impl Autoformat { // Format single file if let Some(file) = self.file { - let file_content = fs::read_to_string(&file); + let file_content = if file == "-" { + let mut contents = String::new(); + std::io::stdin().read_to_string(&mut contents)?; + Ok(contents) + } else { + fs::read_to_string(&file) + }; match file_content { Ok(s) => { let edits = dioxus_autofmt::fmt_file(&s); let out = dioxus_autofmt::apply_formats(&s, edits); - match fs::write(&file, out) { - Ok(_) => { - println!("formatted {}", file); - } - Err(e) => { - eprintln!("failed to write formatted content to file: {}", e); + if file == "-" { + print!("{}", out); + } else { + match fs::write(&file, out) { + Ok(_) => { + println!("formatted {}", file); + } + Err(e) => { + eprintln!("failed to write formatted content to file: {}", e); + } } } }