summaryrefslogtreecommitdiffstats
path: root/fatcat-cli/src/main.rs
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@archive.org>2021-02-09 22:37:45 -0800
committerBryan Newbold <bnewbold@archive.org>2021-02-09 22:37:45 -0800
commitefdc17248cbf8336f28c377a301752604fb6db74 (patch)
treec8700b54c39c4015600f7eedd41681fcf2e9d365 /fatcat-cli/src/main.rs
parent86fee15a36758dde3e118a92469cc71c11ab50b4 (diff)
downloadfatcat-cli-efdc17248cbf8336f28c377a301752604fb6db74.tar.gz
fatcat-cli-efdc17248cbf8336f28c377a301752604fb6db74.zip
add colored JSON output for some commands
Diffstat (limited to 'fatcat-cli/src/main.rs')
-rw-r--r--fatcat-cli/src/main.rs25
1 files changed, 14 insertions, 11 deletions
diff --git a/fatcat-cli/src/main.rs b/fatcat-cli/src/main.rs
index 3b0d382..3b4a81b 100644
--- a/fatcat-cli/src/main.rs
+++ b/fatcat-cli/src/main.rs
@@ -1,6 +1,5 @@
use crate::{path_or_stdin, BatchGrouper, BatchOp};
use anyhow::{anyhow, Context, Result};
-use fatcat_cli::ApiModelSer;
use fatcat_cli::*;
#[allow(unused_imports)]
use log::{self, debug, info};
@@ -8,6 +7,7 @@ use std::io::Write;
use std::path::PathBuf;
use structopt::StructOpt;
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
+use colored_json::to_colored_json_auto;
#[derive(StructOpt)]
#[structopt(rename_all = "kebab-case", about = "CLI interface to Fatcat API")]
@@ -285,6 +285,9 @@ fn main() -> Result<()> {
debug!("Args parsed, starting up");
+ #[cfg(windows)]
+ colored_json::enable_ansi_support();
+
if let Err(err) = run(opt) {
// Be graceful about some errors
if let Some(io_err) = err.root_cause().downcast_ref::<std::io::Error>() {
@@ -323,7 +326,7 @@ fn run(opt: Opt) -> Result<()> {
if toml {
writeln!(&mut std::io::stdout(), "{}", result.to_toml_string()?)?
} else if json || true {
- writeln!(&mut std::io::stdout(), "{}", result.to_json_string()?)?
+ writeln!(&mut std::io::stdout(), "{}", to_colored_json_auto(&result.to_json_value()?)?)?
}
}
Command::Create {
@@ -333,7 +336,7 @@ fn run(opt: Opt) -> Result<()> {
} => {
let json_str = read_entity_file(input_path)?;
let ee = api_client.create_entity_from_json(entity_type, &json_str, editgroup_id)?;
- println!("{}", serde_json::to_string(&ee)?);
+ writeln!(&mut std::io::stdout(), "{}", to_colored_json_auto(&serde_json::to_value(&ee)?)?)?
}
Command::Update {
specifier,
@@ -357,7 +360,7 @@ fn run(opt: Opt) -> Result<()> {
};
let ee =
api_client.update_entity_from_json(exact_specifier, &json_str, editgroup_id)?;
- println!("{}", serde_json::to_string(&ee)?);
+ writeln!(&mut std::io::stdout(), "{}", to_colored_json_auto(&serde_json::to_value(&ee)?)?)?
}
Command::Edit {
specifier,
@@ -373,7 +376,7 @@ fn run(opt: Opt) -> Result<()> {
json,
editing_command,
)?;
- println!("{}", serde_json::to_string(&ee)?);
+ writeln!(&mut std::io::stdout(), "{}", to_colored_json_auto(&serde_json::to_value(&ee)?)?)?
}
Command::Changelog { limit, json } => {
let resp = api_client
@@ -598,25 +601,25 @@ fn run(opt: Opt) -> Result<()> {
cmd: EditgroupCommand::Create { description },
} => {
let eg = api_client.create_editgroup(Some(description))?;
- println!("{}", serde_json::to_string(&eg)?)
+ writeln!(&mut std::io::stdout(), "{}", to_colored_json_auto(&serde_json::to_value(&eg)?)?)?
}
Command::Editgroup {
cmd: EditgroupCommand::Accept { editgroup_id },
} => {
let msg = api_client.accept_editgroup(editgroup_id.clone())?;
- println!("{}", serde_json::to_string(&msg)?);
+ writeln!(&mut std::io::stdout(), "{}", to_colored_json_auto(&serde_json::to_value(&msg)?)?)?
}
Command::Editgroup {
cmd: EditgroupCommand::Submit { editgroup_id },
} => {
- let eg = api_client.update_editgroup_submit(editgroup_id, true)?;
- println!("{}", eg.to_json_string()?);
+ let msg = api_client.update_editgroup_submit(editgroup_id, true)?;
+ writeln!(&mut std::io::stdout(), "{}", to_colored_json_auto(&serde_json::to_value(&msg)?)?)?
}
Command::Editgroup {
cmd: EditgroupCommand::Unsubmit { editgroup_id },
} => {
- let eg = api_client.update_editgroup_submit(editgroup_id, false)?;
- println!("{}", eg.to_json_string()?);
+ let msg = api_client.update_editgroup_submit(editgroup_id, false)?;
+ writeln!(&mut std::io::stdout(), "{}", to_colored_json_auto(&serde_json::to_value(&msg)?)?)?
}
Command::Status { json } => {
let status = ClientStatus::generate(&mut api_client)?;