nushell/crates/nu-cli/src/commands/mv.rs

81 lines
2.1 KiB
Rust
Raw Normal View History

use crate::commands::WholeStreamCommand;
use crate::context::CommandRegistry;
2019-08-14 22:08:10 +02:00
use crate::prelude::*;
use nu_errors::ShellError;
use nu_protocol::{Signature, SyntaxShape};
use nu_source::Tagged;
2019-08-14 22:08:10 +02:00
use std::path::PathBuf;
pub struct Move;
#[derive(Deserialize)]
pub struct MoveArgs {
2019-08-21 19:03:59 +02:00
pub src: Tagged<PathBuf>,
pub dst: Tagged<PathBuf>,
}
impl WholeStreamCommand for Move {
2019-08-14 22:08:10 +02:00
fn name(&self) -> &str {
"mv"
}
fn signature(&self) -> Signature {
Signature::build("mv")
2019-10-28 06:15:35 +01:00
.required(
"source",
SyntaxShape::Pattern,
"the location to move files/directories from",
)
.required(
"destination",
SyntaxShape::Path,
"the location to move files/directories to",
)
2019-08-14 22:08:10 +02:00
}
fn usage(&self) -> &str {
"Move files or directories."
}
fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
2019-08-24 21:36:19 +02:00
) -> Result<OutputStream, ShellError> {
mv(args, registry)
}
fn examples(&self) -> &[Example] {
&[
Example {
description: "Rename a file",
example: "mv before.txt after.txt",
},
Example {
description: "Move a file into a directory",
example: "mv test.txt my/subdirectory",
},
Example {
description: "Move many files into a directory",
example: "mv *.txt my/subdirectory",
},
]
}
2019-08-14 22:08:10 +02:00
}
fn mv(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let stream = async_stream! {
let name = args.call_info.name_tag.clone();
let shell_manager = args.shell_manager.clone();
let (args, _) = args.process(&registry).await?;
let mut result = shell_manager.mv(args, name)?;
while let Some(item) = result.next().await {
yield item;
}
};
Ok(stream.to_output_stream())
2019-08-14 22:08:10 +02:00
}