Require input for date format (#7043)

This commit is contained in:
Reilly Wood 2022-11-09 14:16:14 -08:00 committed by GitHub
parent 14d7ba5cc9
commit da8f6c5682
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 12 deletions

View File

@ -61,6 +61,13 @@ impl Command for SubCommand {
let format = call.opt::<Spanned<String>>(engine_state, stack, 0)?;
if input.is_nothing() {
return Err(ShellError::UnsupportedInput(
"Input was nothing. You must pipe an input to this command.".into(),
head,
));
}
input.map(
move |value| match &format {
Some(format) => format_helper(value, format.item.as_str(), format.span, head),
@ -147,10 +154,6 @@ fn format_helper(value: Value, formatter: &str, formatter_span: Span, head_span:
Err(e) => e,
}
}
Value::Nothing { .. } => {
let dt = Local::now();
format_from(dt, formatter, formatter_span)
}
_ => Value::Error {
error: ShellError::DatetimeParseError(head_span),
},
@ -176,13 +179,6 @@ fn format_helper_rfc2822(value: Value, span: Span) -> Value {
Err(e) => e,
}
}
Value::Nothing { span: _ } => {
let dt = Local::now();
Value::String {
val: dt.with_timezone(dt.offset()).to_rfc2822(),
span,
}
}
_ => Value::Error {
error: ShellError::DatetimeParseError(span),
},

View File

@ -5,7 +5,7 @@ fn formatter_not_valid() {
let actual = nu!(
cwd: ".", pipeline(
r#"
date format '%N'
date now | date format '%N'
"#
)
);
@ -13,6 +13,19 @@ fn formatter_not_valid() {
assert!(actual.err.contains("invalid format"));
}
#[test]
fn fails_without_input() {
let actual = nu!(
cwd: ".", pipeline(
r#"
date format "%c"
"#
)
);
assert!(actual.err.contains("Unsupported input"));
}
#[test]
fn locale_format_respect_different_locale() {
let actual = nu!(