mirror of
https://github.com/nushell/nushell.git
synced 2024-12-13 18:52:01 +01:00
56 lines
1.4 KiB
Rust
56 lines
1.4 KiB
Rust
|
use crate::prelude::*;
|
||
|
use nu_errors::ShellError;
|
||
|
use nu_protocol::{CallInfo, Signature, SyntaxShape, Value};
|
||
|
use nu_source::Tagged;
|
||
|
use std::error::Error;
|
||
|
use std::fs::OpenOptions;
|
||
|
use std::path::PathBuf;
|
||
|
|
||
|
pub struct Touch;
|
||
|
|
||
|
#[derive(Deserialize)]
|
||
|
pub struct TouchArgs {
|
||
|
pub target: Tagged<PathBuf>,
|
||
|
}
|
||
|
|
||
|
impl PerItemCommand for Touch {
|
||
|
fn name(&self) -> &str {
|
||
|
"touch"
|
||
|
}
|
||
|
fn signature(&self) -> Signature {
|
||
|
Signature::build("touch").required(
|
||
|
"filename",
|
||
|
SyntaxShape::Path,
|
||
|
"the path of the file you want to create",
|
||
|
)
|
||
|
}
|
||
|
fn usage(&self) -> &str {
|
||
|
"creates a file"
|
||
|
}
|
||
|
fn run(
|
||
|
&self,
|
||
|
call_info: &CallInfo,
|
||
|
_registry: &CommandRegistry,
|
||
|
raw_args: &RawCommandArgs,
|
||
|
_input: Value,
|
||
|
) -> Result<OutputStream, ShellError> {
|
||
|
call_info
|
||
|
.process(&raw_args.shell_manager, raw_args.ctrl_c.clone(), touch)?
|
||
|
.run()
|
||
|
}
|
||
|
}
|
||
|
fn touch(args: TouchArgs, _context: &RunnablePerItemContext) -> Result<OutputStream, ShellError> {
|
||
|
match OpenOptions::new()
|
||
|
.write(true)
|
||
|
.create(true)
|
||
|
.open(&args.target)
|
||
|
{
|
||
|
Ok(_) => Ok(OutputStream::empty()),
|
||
|
Err(err) => Err(ShellError::labeled_error(
|
||
|
"File Error",
|
||
|
err.description(),
|
||
|
&args.target.tag,
|
||
|
)),
|
||
|
}
|
||
|
}
|