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 -'
This commit is contained in:
Timothy Mertz authored and GitHub committed 2023-10-09 08:20:31 -05:00
1 parent 0d7bc86250
commit cde1e886d6
1 file changed
+17 -7
+17 -7
View File
@@ -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);
}
}
}
}