mirror of
https://github.com/sagiegurari/duckscript
synced 2024-09-06 00:34:07 +00:00
Implement std::fs::GetFileSize (get_file_size) command
This commit is contained in:
parent
237123c287
commit
996bab6725
19
duckscript_sdk/src/sdk/std/fs/get_file_size/help.md
Normal file
19
duckscript_sdk/src/sdk/std/fs/get_file_size/help.md
Normal file
|
@ -0,0 +1,19 @@
|
|||
```sh
|
||||
var = get_file_size path
|
||||
```
|
||||
|
||||
This command will return the size of the file in bytes.
|
||||
|
||||
### Parameters
|
||||
|
||||
The path to check.
|
||||
|
||||
### Return Value
|
||||
|
||||
The size of the file in bytes or false in case path does not exist.
|
||||
|
||||
### Examples
|
||||
|
||||
```sh
|
||||
size = get_file_size ./dir/somefile.txt
|
||||
```
|
46
duckscript_sdk/src/sdk/std/fs/get_file_size/mod.rs
Normal file
46
duckscript_sdk/src/sdk/std/fs/get_file_size/mod.rs
Normal file
|
@ -0,0 +1,46 @@
|
|||
use crate::utils::{io, pckg};
|
||||
use duckscript::types::command::{Command, CommandResult};
|
||||
|
||||
#[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, "GetFileSize")
|
||||
}
|
||||
|
||||
fn aliases(&self) -> Vec<String> {
|
||||
vec!["get_file_size".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 {
|
||||
if arguments.is_empty() {
|
||||
CommandResult::Error("Path not provided.".to_string())
|
||||
} else {
|
||||
match io::get_file_size(&arguments[0]) {
|
||||
Ok(time) => CommandResult::Continue(Some(time.to_string())),
|
||||
Err(error) => CommandResult::Error(error),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn create(package: &str) -> Box<dyn Command> {
|
||||
Box::new(CommandImpl {
|
||||
package: package.to_string(),
|
||||
})
|
||||
}
|
44
duckscript_sdk/src/sdk/std/fs/get_file_size/mod_test.rs
Normal file
44
duckscript_sdk/src/sdk/std/fs/get_file_size/mod_test.rs
Normal file
|
@ -0,0 +1,44 @@
|
|||
use super::*;
|
||||
use crate::test;
|
||||
use crate::test::CommandValidation;
|
||||
|
||||
#[test]
|
||||
fn common_functions() {
|
||||
test::test_common_command_functions(create(""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_no_path_provided() {
|
||||
test::run_script_and_error(
|
||||
vec![create("")],
|
||||
"out = get_file_size",
|
||||
"out"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_file() {
|
||||
test::run_script_and_validate(
|
||||
vec![create("")],
|
||||
"out = get_file_size ./Cargo.toml",
|
||||
CommandValidation::PositiveNumber("out".to_string()),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_directory() {
|
||||
test::run_script_and_error(
|
||||
vec![create("")],
|
||||
"out = get_file_size ./src",
|
||||
"out",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_not_found() {
|
||||
test::run_script_and_error(
|
||||
vec![create("")],
|
||||
"out = get_file_size ./badpath",
|
||||
"out",
|
||||
);
|
||||
}
|
|
@ -6,6 +6,7 @@ mod cp_glob;
|
|||
mod dirname;
|
||||
mod exists;
|
||||
mod get_last_modified_time;
|
||||
mod get_file_size;
|
||||
mod gitignore_path_array;
|
||||
mod glob_array;
|
||||
mod is_directory;
|
||||
|
@ -46,6 +47,7 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr
|
|||
commands.set(dirname::create(&package))?;
|
||||
commands.set(exists::create(&package))?;
|
||||
commands.set(get_last_modified_time::create(&package))?;
|
||||
commands.set(get_file_size::create(&package))?;
|
||||
commands.set(gitignore_path_array::create(&package))?;
|
||||
commands.set(glob_array::create(&package))?;
|
||||
commands.set(is_directory::create(&package))?;
|
||||
|
|
|
@ -54,3 +54,14 @@ pub(crate) fn get_last_modified_time(path: &str) -> Result<u128, String> {
|
|||
Err(error) => Err(error.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn get_file_size(path: &str) -> Result<u64, String> {
|
||||
match std::fs::metadata(path) {
|
||||
Ok(metadata) => if metadata.is_file() {
|
||||
Ok(metadata.len())
|
||||
} else {
|
||||
Err("The provided path is not a file.".to_string())
|
||||
},
|
||||
Err(_error) => Err("Unable to extract metadata for path.".to_string()),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -99,3 +99,25 @@ fn create_empty_file_exists() {
|
|||
let result = create_empty_file(path);
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_file_size_exists() {
|
||||
let size = get_file_size("Cargo.toml");
|
||||
|
||||
assert!(size.is_ok());
|
||||
assert!(size.unwrap() > 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_file_size_not_exists() {
|
||||
let size = get_file_size("Cargo.toml2");
|
||||
|
||||
assert!(size.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_dir_size() {
|
||||
let size = get_file_size("src");
|
||||
|
||||
assert!(size.is_err());
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue