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:
1 parent
0d7bc86250
commit
cde1e886d6
1 file changed
+17
-7
@@ -46,18 +46,28 @@ impl Autoformat {
|
|||||||
|
|
||||||
// Format single file
|
// Format single file
|
||||||
if let Some(file) = self.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 {
|
match file_content {
|
||||||
Ok(s) => {
|
Ok(s) => {
|
||||||
let edits = dioxus_autofmt::fmt_file(&s);
|
let edits = dioxus_autofmt::fmt_file(&s);
|
||||||
let out = dioxus_autofmt::apply_formats(&s, edits);
|
let out = dioxus_autofmt::apply_formats(&s, edits);
|
||||||
match fs::write(&file, out) {
|
if file == "-" {
|
||||||
Ok(_) => {
|
print!("{}", out);
|
||||||
println!("formatted {}", file);
|
} else {
|
||||||
}
|
match fs::write(&file, out) {
|
||||||
Err(e) => {
|
Ok(_) => {
|
||||||
eprintln!("failed to write formatted content to file: {}", e);
|
println!("formatted {}", file);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("failed to write formatted content to file: {}", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user