From b36050fbb292c1e8b0c55262a422116f2528f794 Mon Sep 17 00:00:00 2001 From: sagie gur ari Date: Fri, 17 Jan 2020 11:36:51 +0000 Subject: [PATCH] cat command to support multiple files #62 --- CHANGELOG.md | 1 + duckscript_sdk/src/sdk/std/fs/print/help.md | 6 +++--- duckscript_sdk/src/sdk/std/fs/print/mod.rs | 16 +++++++++------- duckscript_sdk/src/sdk/std/fs/print/mod_test.rs | 11 ++++++++++- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8433762..3e08ce7 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * New properties read/write commands #61 * Default command run implementation should crash and not error #63 * \[Breaking Change\] Invoking a command that does not exist should crash and not error +* cat command to support multiple files #62 ### v0.1.6 (2020-01-12) diff --git a/duckscript_sdk/src/sdk/std/fs/print/help.md b/duckscript_sdk/src/sdk/std/fs/print/help.md index 35c335a..e8b359b 100644 --- a/duckscript_sdk/src/sdk/std/fs/print/help.md +++ b/duckscript_sdk/src/sdk/std/fs/print/help.md @@ -1,13 +1,13 @@ ```sh -var = cat file +var = cat [file]+ ``` -The cat command will print out the requested file.
+The cat command will print out the requested file/s.
In addition it will also return the value to the output variable. #### Parameters -A single parameter holding the file path. +Multiple file paths. #### Return Value diff --git a/duckscript_sdk/src/sdk/std/fs/print/mod.rs b/duckscript_sdk/src/sdk/std/fs/print/mod.rs index 7a85647..a112771 100755 --- a/duckscript_sdk/src/sdk/std/fs/print/mod.rs +++ b/duckscript_sdk/src/sdk/std/fs/print/mod.rs @@ -26,16 +26,18 @@ impl Command for CommandImpl { if arguments.is_empty() { CommandResult::Error("File name not provided.".to_string()) } else { - let result = io::read_text_file(&arguments[0]); + let mut all_text = String::new(); + for argument in &arguments { + let result = io::read_text_file(&argument); - match result { - Ok(text) => { - println!("{}", &text); - - CommandResult::Continue(Some(text)) + match result { + Ok(text) => all_text.push_str(&text), + Err(error) => return CommandResult::Error(error.to_string()), } - Err(error) => CommandResult::Error(error.to_string()), } + + println!("{}", &all_text); + CommandResult::Continue(Some(all_text)) } } } diff --git a/duckscript_sdk/src/sdk/std/fs/print/mod_test.rs b/duckscript_sdk/src/sdk/std/fs/print/mod_test.rs index 89d05c3..74cfff5 100644 --- a/duckscript_sdk/src/sdk/std/fs/print/mod_test.rs +++ b/duckscript_sdk/src/sdk/std/fs/print/mod_test.rs @@ -13,10 +13,19 @@ fn run_no_file_provided() { } #[test] -fn run_valid() { +fn run_single_file() { test::run_script_and_validate( vec![create("")], "out = cat ./Cargo.toml", CommandValidation::Contains("out".to_string(), "duckscript".to_string()), ); } + +#[test] +fn run_multiple_files() { + test::run_script_and_validate( + vec![create("")], + "out = cat ./Cargo.toml ./Cargo.toml", + CommandValidation::Contains("out".to_string(), "duckscript".to_string()), + ); +}