refactor: extract json_display
This commit is contained in:
@@ -1,9 +1,6 @@
|
|||||||
use std::{
|
use std::sync::{
|
||||||
io::stdout,
|
|
||||||
sync::{
|
|
||||||
Arc,
|
Arc,
|
||||||
atomic::{AtomicBool, Ordering},
|
atomic::{AtomicBool, Ordering},
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use nyquest_preset::nyquest::ClientBuilder;
|
use nyquest_preset::nyquest::ClientBuilder;
|
||||||
@@ -28,7 +25,7 @@ use spdlog::{error, info, warn};
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
commands::Cli,
|
commands::Cli,
|
||||||
utils::{human_readable_display, login_action},
|
utils::{human_readable_display, json_display, login_action},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(feature = "fetchall")]
|
#[cfg(feature = "fetchall")]
|
||||||
@@ -139,8 +136,7 @@ async fn main() -> Result<(), Box<dyn snafu::Error>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !human_readable {
|
if !human_readable {
|
||||||
let lock = stdout().lock();
|
json_display(resp)?;
|
||||||
serde_json::to_writer_pretty(lock, &resp)?;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -48,6 +48,12 @@ pub async fn login_action<R>(
|
|||||||
Ok(return_data)
|
Ok(return_data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn json_display(value: impl Serialize) -> Result<(), Box<dyn snafu::Error>> {
|
||||||
|
let lock = stdout().lock();
|
||||||
|
serde_json::to_writer_pretty(lock, &value)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn human_readable_display(
|
pub fn human_readable_display(
|
||||||
value: impl Display + Serialize,
|
value: impl Display + Serialize,
|
||||||
human_readable: bool,
|
human_readable: bool,
|
||||||
@@ -55,8 +61,7 @@ pub fn human_readable_display(
|
|||||||
if human_readable {
|
if human_readable {
|
||||||
println!("{value}");
|
println!("{value}");
|
||||||
} else {
|
} else {
|
||||||
let lock = stdout().lock();
|
json_display(value)?;
|
||||||
serde_json::to_writer_pretty(lock, &value)?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
Reference in New Issue
Block a user