duckscriptsdk/sdk/std/fs/touch/
mod.rs

1use crate::utils::{io, pckg};
2use duckscript::types::command::{Command, CommandInvocationContext, CommandResult};
3
4#[cfg(test)]
5#[path = "./mod_test.rs"]
6mod mod_test;
7
8#[derive(Clone)]
9pub(crate) struct CommandImpl {
10    package: String,
11}
12
13impl Command for CommandImpl {
14    fn name(&self) -> String {
15        pckg::concat(&self.package, "CreateEmptyFile")
16    }
17
18    fn aliases(&self) -> Vec<String> {
19        vec!["touch".to_string()]
20    }
21
22    fn help(&self) -> String {
23        include_str!("help.md").to_string()
24    }
25
26    fn clone_and_box(&self) -> Box<dyn Command> {
27        Box::new((*self).clone())
28    }
29
30    fn run(&self, context: CommandInvocationContext) -> CommandResult {
31        if context.arguments.is_empty() {
32            CommandResult::Error("Path not provided.".to_string())
33        } else {
34            match io::create_empty_file(&context.arguments[0]) {
35                Ok(_) => CommandResult::Continue(Some("true".to_string())),
36                Err(_) => CommandResult::Continue(Some("false".to_string())),
37            }
38        }
39    }
40}
41
42pub(crate) fn create(package: &str) -> Box<dyn Command> {
43    Box::new(CommandImpl {
44        package: package.to_string(),
45    })
46}