2019-08-15 05:02:02 +00:00
|
|
|
use crate::commands::{RawCommandArgs, WholeStreamCommand};
|
2019-06-07 07:50:26 +00:00
|
|
|
use crate::errors::ShellError;
|
|
|
|
use crate::prelude::*;
|
|
|
|
|
2019-08-02 19:15:07 +00:00
|
|
|
pub struct Autoview;
|
|
|
|
|
2019-08-03 02:17:28 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct AutoviewArgs {}
|
|
|
|
|
2019-08-15 05:02:02 +00:00
|
|
|
impl WholeStreamCommand for Autoview {
|
2019-08-02 19:15:07 +00:00
|
|
|
fn name(&self) -> &str {
|
|
|
|
"autoview"
|
|
|
|
}
|
|
|
|
|
2019-08-29 22:52:32 +00:00
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("autoview")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"View the contents of the pipeline as a table or list."
|
|
|
|
}
|
|
|
|
|
2019-08-02 19:15:07 +00:00
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
2019-08-17 03:53:39 +00:00
|
|
|
Ok(args.process_raw(registry, autoview)?.run())
|
2019-08-02 19:15:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-03 02:17:28 +00:00
|
|
|
pub fn autoview(
|
|
|
|
AutoviewArgs {}: AutoviewArgs,
|
|
|
|
mut context: RunnableContext,
|
|
|
|
raw: RawCommandArgs,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
2019-08-06 16:26:33 +00:00
|
|
|
Ok(OutputStream::new(async_stream_block! {
|
2019-08-03 02:17:28 +00:00
|
|
|
let input = context.input.drain_vec().await;
|
|
|
|
|
|
|
|
if input.len() > 0 {
|
2019-08-09 04:51:21 +00:00
|
|
|
if let Tagged {
|
2019-09-12 18:29:16 +00:00
|
|
|
item: Value::Primitive(Primitive::Binary(_)),
|
2019-08-03 02:17:28 +00:00
|
|
|
..
|
2019-08-09 04:51:21 +00:00
|
|
|
} = input[0usize]
|
2019-08-03 02:17:28 +00:00
|
|
|
{
|
2019-09-07 07:32:07 +00:00
|
|
|
let binary = context.get_command("binaryview");
|
|
|
|
if let Some(binary) = binary {
|
2019-09-17 02:09:15 +00:00
|
|
|
let result = binary.run(raw.with_input(input), &context.commands, false);
|
2019-09-07 07:32:07 +00:00
|
|
|
result.collect::<Vec<_>>().await;
|
|
|
|
} else {
|
|
|
|
for i in input {
|
|
|
|
match i.item {
|
2019-09-12 18:29:16 +00:00
|
|
|
Value::Primitive(Primitive::Binary(b)) => {
|
2019-09-07 07:32:07 +00:00
|
|
|
use pretty_hex::*;
|
|
|
|
println!("{:?}", b.hex_dump());
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-09-08 01:35:02 +00:00
|
|
|
} else if is_single_origined_text_value(&input) {
|
2019-09-07 07:32:07 +00:00
|
|
|
let text = context.get_command("textview");
|
|
|
|
if let Some(text) = text {
|
2019-09-17 02:09:15 +00:00
|
|
|
let result = text.run(raw.with_input(input), &context.commands, false);
|
2019-09-07 07:32:07 +00:00
|
|
|
result.collect::<Vec<_>>().await;
|
|
|
|
} else {
|
|
|
|
for i in input {
|
|
|
|
match i.item {
|
|
|
|
Value::Primitive(Primitive::String(s)) => {
|
|
|
|
println!("{}", s);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-08 01:35:02 +00:00
|
|
|
} else if is_single_text_value(&input) {
|
|
|
|
for i in input {
|
|
|
|
match i.item {
|
|
|
|
Value::Primitive(Primitive::String(s)) => {
|
|
|
|
println!("{}", s);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2019-08-03 02:17:28 +00:00
|
|
|
} else {
|
2019-08-10 20:18:14 +00:00
|
|
|
let table = context.expect_command("table");
|
2019-09-17 02:09:15 +00:00
|
|
|
let result = table.run(raw.with_input(input), &context.commands, false);
|
2019-08-10 20:18:14 +00:00
|
|
|
result.collect::<Vec<_>>().await;
|
2019-06-07 07:50:26 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-06 16:26:33 +00:00
|
|
|
}))
|
2019-06-07 07:50:26 +00:00
|
|
|
}
|
|
|
|
|
2019-08-01 01:58:42 +00:00
|
|
|
fn is_single_text_value(input: &Vec<Tagged<Value>>) -> bool {
|
2019-07-20 01:12:04 +00:00
|
|
|
if input.len() != 1 {
|
|
|
|
return false;
|
|
|
|
}
|
2019-08-01 01:58:42 +00:00
|
|
|
if let Tagged {
|
2019-07-20 01:12:04 +00:00
|
|
|
item: Value::Primitive(Primitive::String(_)),
|
|
|
|
..
|
|
|
|
} = input[0]
|
|
|
|
{
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2019-09-08 01:35:02 +00:00
|
|
|
|
|
|
|
fn is_single_origined_text_value(input: &Vec<Tagged<Value>>) -> bool {
|
|
|
|
if input.len() != 1 {
|
|
|
|
return false;
|
|
|
|
}
|
2019-09-18 06:37:04 +00:00
|
|
|
|
2019-09-08 01:35:02 +00:00
|
|
|
if let Tagged {
|
|
|
|
item: Value::Primitive(Primitive::String(_)),
|
2019-09-18 07:05:33 +00:00
|
|
|
tag: Tag { origin, .. },
|
2019-09-08 01:35:02 +00:00
|
|
|
} = input[0]
|
|
|
|
{
|
2019-09-14 16:30:24 +00:00
|
|
|
origin != uuid::Uuid::nil()
|
2019-09-08 01:35:02 +00:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|