forked from extern/nushell
Implements from-ssv
This commit is contained in:
parent
648d4865b1
commit
de1c4e6c88
@ -272,6 +272,7 @@ pub async fn cli() -> Result<(), Box<dyn Error>> {
|
|||||||
whole_stream_command(Env),
|
whole_stream_command(Env),
|
||||||
whole_stream_command(FromCSV),
|
whole_stream_command(FromCSV),
|
||||||
whole_stream_command(FromTSV),
|
whole_stream_command(FromTSV),
|
||||||
|
whole_stream_command(FromSSV),
|
||||||
whole_stream_command(FromINI),
|
whole_stream_command(FromINI),
|
||||||
whole_stream_command(FromBSON),
|
whole_stream_command(FromBSON),
|
||||||
whole_stream_command(FromJSON),
|
whole_stream_command(FromJSON),
|
||||||
|
@ -33,11 +33,91 @@ impl WholeStreamCommand for FromSSV {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn from_ssv_string_to_value(
|
||||||
|
s: &str,
|
||||||
|
headerless: bool,
|
||||||
|
tag: impl Into<Tag>,
|
||||||
|
) -> Result<Tagged<Value>, &str> {
|
||||||
|
let mut lines = s.lines();
|
||||||
|
let tag = tag.into();
|
||||||
|
|
||||||
|
let headers = lines
|
||||||
|
.next()
|
||||||
|
.expect("No content.")
|
||||||
|
.split_whitespace()
|
||||||
|
.map(|s| s.to_owned())
|
||||||
|
.collect::<Vec<String>>();
|
||||||
|
|
||||||
|
let header_row = if headerless {
|
||||||
|
(0..headers.len())
|
||||||
|
.map(|i| format!("Column{}", i + 1))
|
||||||
|
.collect::<Vec<String>>()
|
||||||
|
} else {
|
||||||
|
headers
|
||||||
|
};
|
||||||
|
|
||||||
|
let rows = lines
|
||||||
|
.map(|l| {
|
||||||
|
let mut row = TaggedDictBuilder::new(tag);
|
||||||
|
for (column, value) in header_row.iter().zip(l.split_whitespace()) {
|
||||||
|
row.insert_tagged(
|
||||||
|
column.to_owned(),
|
||||||
|
Value::Primitive(Primitive::String(String::from(value))).tagged(tag),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
row.into_tagged_value()
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
Ok(Tagged::from_item(Value::Table(rows), tag))
|
||||||
|
}
|
||||||
|
|
||||||
fn from_ssv(
|
fn from_ssv(
|
||||||
FromSSVArgs {
|
FromSSVArgs { headerless }: FromSSVArgs,
|
||||||
headerless: headerless,
|
|
||||||
}: FromSSVArgs,
|
|
||||||
RunnableContext { input, name, .. }: RunnableContext,
|
RunnableContext { input, name, .. }: RunnableContext,
|
||||||
) -> Result<OutputStream, ShellError> {
|
) -> Result<OutputStream, ShellError> {
|
||||||
unimplemented!()
|
let stream = async_stream! {
|
||||||
|
let values: Vec<Tagged<Value>> = input.values.collect().await;
|
||||||
|
let mut concat_string = String::new();
|
||||||
|
let mut latest_tag: Option<Tag> = None;
|
||||||
|
|
||||||
|
for value in values {
|
||||||
|
let value_tag = value.tag();
|
||||||
|
latest_tag = Some(value_tag);
|
||||||
|
match value.item {
|
||||||
|
Value::Primitive(Primitive::String(s)) => {
|
||||||
|
concat_string.push_str(&s);
|
||||||
|
concat_string.push_str("\n");
|
||||||
|
|
||||||
|
}
|
||||||
|
_ => yield Err(ShellError::labeled_error_with_secondary (
|
||||||
|
"Expected a string from pipeline",
|
||||||
|
"requires string input",
|
||||||
|
name,
|
||||||
|
"value originates from here",
|
||||||
|
value_tag
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
match from_ssv_string_to_value(&concat_string, headerless, name) {
|
||||||
|
Ok(x) => match x {
|
||||||
|
Tagged { item: Value::Table(list), ..} => {
|
||||||
|
for l in list { yield ReturnSuccess::value(l) }
|
||||||
|
}
|
||||||
|
x => yield ReturnSuccess::value(x)
|
||||||
|
},
|
||||||
|
Err(_) => if let Some(last_tag) = latest_tag {
|
||||||
|
yield Err(ShellError::labeled_error_with_secondary(
|
||||||
|
"Could not parse as SSV",
|
||||||
|
"input cannot be parsed ssv",
|
||||||
|
name,
|
||||||
|
"value originates from here",
|
||||||
|
last_tag,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(stream.to_output_stream())
|
||||||
}
|
}
|
||||||
|
@ -359,7 +359,7 @@ fn converts_from_tsv_text_skipping_headers_to_structured_table() {
|
|||||||
fn converts_from_ssv_text_to_structured_table() {
|
fn converts_from_ssv_text_to_structured_table() {
|
||||||
Playground::setup("filter_from_ssv_test_1", |dirs, sandbox| {
|
Playground::setup("filter_from_ssv_test_1", |dirs, sandbox| {
|
||||||
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
||||||
"oc_get_svc.ssv",
|
"oc_get_svc.txt",
|
||||||
r#"
|
r#"
|
||||||
NAME LABELS SELECTOR IP PORT(S)
|
NAME LABELS SELECTOR IP PORT(S)
|
||||||
docker-registry docker-registry=default docker-registry=default 172.30.78.158 5000/TCP
|
docker-registry docker-registry=default docker-registry=default 172.30.78.158 5000/TCP
|
||||||
@ -371,15 +371,15 @@ fn converts_from_ssv_text_to_structured_table() {
|
|||||||
let actual = nu!(
|
let actual = nu!(
|
||||||
cwd: dirs.test(), h::pipeline(
|
cwd: dirs.test(), h::pipeline(
|
||||||
r#"
|
r#"
|
||||||
open oc_get_svc.ssv
|
open oc_get_svc.txt
|
||||||
| from-ssv
|
| from-ssv
|
||||||
| nth 0
|
| nth 0
|
||||||
| get NAME
|
| get IP
|
||||||
| echo $it
|
| echo $it
|
||||||
"#
|
"#
|
||||||
));
|
));
|
||||||
|
|
||||||
assert_eq!(actual, "docker-registry");
|
assert_eq!(actual, "172.30.78.158");
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -387,7 +387,7 @@ fn converts_from_ssv_text_to_structured_table() {
|
|||||||
fn converts_from_ssv_text_skipping_headers_to_structured_table() {
|
fn converts_from_ssv_text_skipping_headers_to_structured_table() {
|
||||||
Playground::setup("filter_from_ssv_test_2", |dirs, sandbox| {
|
Playground::setup("filter_from_ssv_test_2", |dirs, sandbox| {
|
||||||
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
||||||
"oc_get_svc.ssv",
|
"oc_get_svc.txt",
|
||||||
r#"
|
r#"
|
||||||
NAME LABELS SELECTOR IP PORT(S)
|
NAME LABELS SELECTOR IP PORT(S)
|
||||||
docker-registry docker-registry=default docker-registry=default 172.30.78.158 5000/TCP
|
docker-registry docker-registry=default docker-registry=default 172.30.78.158 5000/TCP
|
||||||
@ -399,7 +399,7 @@ fn converts_from_ssv_text_skipping_headers_to_structured_table() {
|
|||||||
let actual = nu!(
|
let actual = nu!(
|
||||||
cwd: dirs.test(), h::pipeline(
|
cwd: dirs.test(), h::pipeline(
|
||||||
r#"
|
r#"
|
||||||
open oc_get_svc.ssv
|
open oc_get_svc.txt
|
||||||
| from-ssv --headerless
|
| from-ssv --headerless
|
||||||
| nth 2
|
| nth 2
|
||||||
| get Column2
|
| get Column2
|
||||||
|
Loading…
Reference in New Issue
Block a user