mirror of
https://github.com/sagiegurari/duckscript
synced 2024-10-04 15:09:08 +00:00
New temp_file command #85
This commit is contained in:
parent
dc281bc3a9
commit
59f2a4ab40
|
@ -2,6 +2,7 @@
|
|||
|
||||
### v0.2.1
|
||||
|
||||
* New temp_file command #85
|
||||
* New spawn command #87
|
||||
* Update build makefile.
|
||||
* Runtime - Add support for **Any** state type #86
|
||||
|
|
29
docs/sdk.md
29
docs/sdk.md
|
@ -65,6 +65,7 @@
|
|||
* [std::fs::Print (cat)](#std__fs__Print)
|
||||
* [std::fs::ReadBytes (readbinfile, read_binary_file)](#std__fs__ReadBytes)
|
||||
* [std::fs::ReadText (readfile, read_text_file)](#std__fs__ReadText)
|
||||
* [std::fs::TempFile (temp_file)](#std__fs__TempFile)
|
||||
* [std::fs::WriteBytes (writebinfile, write_binary_file)](#std__fs__WriteBytes)
|
||||
* [std::fs::WriteText (writefile, write_text_file)](#std__fs__WriteText)
|
||||
* [std::math::Calc (calc)](#std__math__Calc)
|
||||
|
@ -2478,6 +2479,34 @@ text = readfile ./Cargo.toml
|
|||
#### Aliases:
|
||||
readfile, read_text_file
|
||||
|
||||
<a name="std__fs__TempFile"></a>
|
||||
## std::fs::TempFile
|
||||
```sh
|
||||
path = temp_file [extension]
|
||||
```
|
||||
|
||||
This command will create a new empty temporary file and return its path.
|
||||
|
||||
#### Parameters
|
||||
|
||||
Optional file extension.
|
||||
|
||||
#### Return Value
|
||||
|
||||
The file path.
|
||||
|
||||
#### Examples
|
||||
|
||||
```sh
|
||||
path = temp_file toml
|
||||
|
||||
echo ${path}
|
||||
```
|
||||
|
||||
|
||||
#### Aliases:
|
||||
temp_file
|
||||
|
||||
<a name="std__fs__WriteBytes"></a>
|
||||
## std::fs::WriteBytes
|
||||
```sh
|
||||
|
|
|
@ -27,7 +27,7 @@ attohttpc = "^0.11"
|
|||
base64 = "^0.11"
|
||||
duckscript = { version = "^0.2.0", path = "../duckscript" }
|
||||
fs_extra = "^1"
|
||||
fsio = "^0.1"
|
||||
fsio = { version = "^0.1", features = ["temp-path"] }
|
||||
home = "^0.5"
|
||||
java-properties = "^1"
|
||||
meval = "^0.2"
|
||||
|
|
|
@ -11,6 +11,7 @@ mod read_bytes;
|
|||
mod read_text;
|
||||
mod rm;
|
||||
mod rmdir;
|
||||
mod temp_file;
|
||||
mod touch;
|
||||
mod write_bytes;
|
||||
mod write_text;
|
||||
|
@ -37,6 +38,7 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr
|
|||
commands.set(read_text::create(&package))?;
|
||||
commands.set(rm::create(&package))?;
|
||||
commands.set(rmdir::create(&package))?;
|
||||
commands.set(temp_file::create(&package))?;
|
||||
commands.set(touch::create(&package))?;
|
||||
commands.set(write_bytes::create(&package))?;
|
||||
commands.set(write_text::create(&package))?;
|
||||
|
|
21
duckscript_sdk/src/sdk/std/fs/temp_file/help.md
Normal file
21
duckscript_sdk/src/sdk/std/fs/temp_file/help.md
Normal file
|
@ -0,0 +1,21 @@
|
|||
```sh
|
||||
path = temp_file [extension]
|
||||
```
|
||||
|
||||
This command will create a new empty temporary file and return its path.
|
||||
|
||||
#### Parameters
|
||||
|
||||
Optional file extension.
|
||||
|
||||
#### Return Value
|
||||
|
||||
The file path.
|
||||
|
||||
#### Examples
|
||||
|
||||
```sh
|
||||
path = temp_file toml
|
||||
|
||||
echo ${path}
|
||||
```
|
51
duckscript_sdk/src/sdk/std/fs/temp_file/mod.rs
Executable file
51
duckscript_sdk/src/sdk/std/fs/temp_file/mod.rs
Executable file
|
@ -0,0 +1,51 @@
|
|||
use crate::utils::{io, pckg};
|
||||
use duckscript::types::command::{Command, CommandResult};
|
||||
use fsio::path::get_temporary_file_path;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "./mod_test.rs"]
|
||||
mod mod_test;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct CommandImpl {
|
||||
package: String,
|
||||
}
|
||||
|
||||
impl Command for CommandImpl {
|
||||
fn name(&self) -> String {
|
||||
pckg::concat(&self.package, "TempFile")
|
||||
}
|
||||
|
||||
fn aliases(&self) -> Vec<String> {
|
||||
vec!["temp_file".to_string()]
|
||||
}
|
||||
|
||||
fn help(&self) -> String {
|
||||
include_str!("help.md").to_string()
|
||||
}
|
||||
|
||||
fn clone_and_box(&self) -> Box<dyn Command> {
|
||||
Box::new((*self).clone())
|
||||
}
|
||||
|
||||
fn run(&self, arguments: Vec<String>) -> CommandResult {
|
||||
let extension = if arguments.is_empty() {
|
||||
"tmp"
|
||||
} else {
|
||||
&arguments[0]
|
||||
};
|
||||
|
||||
let path = get_temporary_file_path(extension);
|
||||
|
||||
match io::create_empty_file(&path) {
|
||||
Ok(()) => CommandResult::Continue(Some(path)),
|
||||
Err(error) => CommandResult::Error(error),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn create(package: &str) -> Box<dyn Command> {
|
||||
Box::new(CommandImpl {
|
||||
package: package.to_string(),
|
||||
})
|
||||
}
|
26
duckscript_sdk/src/sdk/std/fs/temp_file/mod_test.rs
Normal file
26
duckscript_sdk/src/sdk/std/fs/temp_file/mod_test.rs
Normal file
|
@ -0,0 +1,26 @@
|
|||
use super::*;
|
||||
use crate::test;
|
||||
use crate::test::CommandValidation;
|
||||
|
||||
#[test]
|
||||
fn common_functions() {
|
||||
test::test_common_command_functions(create(""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_no_extension_provided() {
|
||||
test::run_script_and_validate(
|
||||
vec![create("")],
|
||||
"out = temp_file",
|
||||
CommandValidation::Contains("out".to_string(), ".tmp".to_string()),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_extension_provided() {
|
||||
test::run_script_and_validate(
|
||||
vec![create("")],
|
||||
"out = temp_file toml",
|
||||
CommandValidation::Contains("out".to_string(), ".toml".to_string()),
|
||||
);
|
||||
}
|
16
test/std/fs/temp_file_test.ds
Normal file
16
test/std/fs/temp_file_test.ds
Normal file
|
@ -0,0 +1,16 @@
|
|||
|
||||
fn test_no_extension
|
||||
file = temp_file
|
||||
|
||||
valid = ends_with ${file} .tmp
|
||||
|
||||
assert ${valid}
|
||||
end
|
||||
|
||||
fn test_with_extension
|
||||
file = temp_file txt
|
||||
|
||||
valid = ends_with ${file} .txt
|
||||
|
||||
assert ${valid}
|
||||
end
|
Loading…
Reference in a new issue