This commit is contained in:
nora 2021-07-17 22:04:54 +02:00
parent d0733b1a7c
commit 9b7260660e
5 changed files with 320 additions and 30 deletions

View file

@ -1,12 +1,21 @@
use clap::clap_app;
use jsonformat::format_json;
use std::fs;
use std::io;
use std::io::Read;
fn main() -> Result<(), io::Error> {
let filename = std::env::args().skip(1).next();
let matches = clap_app!(jsonformat =>
(version: "1.0")
(author: "nilstrieb <nilstrieb@gmail.com>")
(about: "Formats json")
(@arg indentation: -i --indent +takes_value "Set the indentation used (\\s for space, \\t for tab)")
(@arg output: -o --output +takes_value "The output file for the formatted json")
(@arg input: "The input file to format")
)
.get_matches();
let str = match filename {
let str = match matches.value_of("input") {
Some(path) => fs::read_to_string(path)?,
None => {
let mut buf = String::new();
@ -16,7 +25,18 @@ fn main() -> Result<(), io::Error> {
}
};
println!("{}", format_json(&str, " "));
let replaced_indent = matches
.value_of("indentation")
.map(|value| value.replace("s", " ").replace("t", "\t"));
let formatted = format_json(&str, replaced_indent.as_deref());
match matches.value_of("output") {
Some(file) => {
fs::write(file, formatted)?;
}
None => println!("{}", formatted),
}
Ok(())
}