mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 08:55:40 +02:00
Merge branch 'main' of https://github.com/nushell/engine-q into source-command
This commit is contained in:
@ -17,7 +17,7 @@ impl Command for Source {
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("source").required(
|
||||
"filename",
|
||||
SyntaxShape::FilePath,
|
||||
SyntaxShape::Filepath,
|
||||
"the filepath to the script file to source",
|
||||
)
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ pub fn create_default_context() -> Rc<RefCell<EngineState>> {
|
||||
working_set.add_decl(Box::new(Benchmark));
|
||||
working_set.add_decl(Box::new(BuildString));
|
||||
working_set.add_decl(Box::new(Cd));
|
||||
working_set.add_decl(Box::new(Cp));
|
||||
working_set.add_decl(Box::new(Def));
|
||||
working_set.add_decl(Box::new(Do));
|
||||
working_set.add_decl(Box::new(Each));
|
||||
@ -35,6 +36,7 @@ pub fn create_default_context() -> Rc<RefCell<EngineState>> {
|
||||
working_set.add_decl(Box::new(Lines));
|
||||
working_set.add_decl(Box::new(Ls));
|
||||
working_set.add_decl(Box::new(Module));
|
||||
working_set.add_decl(Box::new(Mv));
|
||||
working_set.add_decl(Box::new(Ps));
|
||||
working_set.add_decl(Box::new(Select));
|
||||
working_set.add_decl(Box::new(Sys));
|
||||
|
@ -15,7 +15,7 @@ impl Command for Cd {
|
||||
}
|
||||
|
||||
fn signature(&self) -> nu_protocol::Signature {
|
||||
Signature::build("cd").optional("path", SyntaxShape::FilePath, "the path to change to")
|
||||
Signature::build("cd").optional("path", SyntaxShape::Filepath, "the path to change to")
|
||||
}
|
||||
|
||||
fn run(
|
||||
@ -28,7 +28,7 @@ impl Command for Cd {
|
||||
|
||||
let path = match path {
|
||||
Some(path) => {
|
||||
let path = nu_path::expand_tilde(path);
|
||||
let path = nu_path::expand_path(path);
|
||||
path.to_string_lossy().to_string()
|
||||
}
|
||||
None => {
|
||||
|
169
crates/nu-command/src/filesystem/cp.rs
Normal file
169
crates/nu-command/src/filesystem/cp.rs
Normal file
@ -0,0 +1,169 @@
|
||||
use std::env::current_dir;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use nu_engine::CallExt;
|
||||
use nu_path::canonicalize_with;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EvaluationContext};
|
||||
use nu_protocol::{ShellError, Signature, SyntaxShape, Value};
|
||||
|
||||
use crate::filesystem::util::FileStructure;
|
||||
|
||||
pub struct Cp;
|
||||
|
||||
impl Command for Cp {
|
||||
fn name(&self) -> &str {
|
||||
"cp"
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Copy files."
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("cp")
|
||||
.required("source", SyntaxShape::GlobPattern, "the place to copy from")
|
||||
.required("destination", SyntaxShape::Filepath, "the place to copy to")
|
||||
.switch(
|
||||
"recursive",
|
||||
"copy recursively through subdirectories",
|
||||
Some('r'),
|
||||
)
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
context: &EvaluationContext,
|
||||
call: &Call,
|
||||
_input: Value,
|
||||
) -> Result<Value, ShellError> {
|
||||
let source: String = call.req(context, 0)?;
|
||||
let destination: String = call.req(context, 1)?;
|
||||
|
||||
let path: PathBuf = current_dir().unwrap();
|
||||
let source = path.join(source.as_str());
|
||||
let destination = path.join(destination.as_str());
|
||||
|
||||
let sources =
|
||||
glob::glob(&source.to_string_lossy()).map_or_else(|_| Vec::new(), Iterator::collect);
|
||||
if sources.is_empty() {
|
||||
return Err(ShellError::FileNotFound(call.positional[0].span));
|
||||
}
|
||||
|
||||
if sources.len() > 1 && !destination.is_dir() {
|
||||
return Err(ShellError::MoveNotPossible {
|
||||
source_message: "Can't move many files".to_string(),
|
||||
source_span: call.positional[0].span,
|
||||
destination_message: "into single file".to_string(),
|
||||
destination_span: call.positional[1].span,
|
||||
});
|
||||
}
|
||||
|
||||
let any_source_is_dir = sources.iter().any(|f| matches!(f, Ok(f) if f.is_dir()));
|
||||
let recursive = call.named.iter().any(|p| &p.0 == "recursive");
|
||||
if any_source_is_dir && !recursive {
|
||||
return Err(ShellError::MoveNotPossibleSingle(
|
||||
"Directories must be copied using \"--recursive\"".to_string(),
|
||||
call.positional[0].span,
|
||||
));
|
||||
}
|
||||
|
||||
for entry in sources.into_iter().flatten() {
|
||||
let mut sources = FileStructure::new();
|
||||
sources.walk_decorate(&entry)?;
|
||||
|
||||
if entry.is_file() {
|
||||
let sources = sources.paths_applying_with(|(source_file, _depth_level)| {
|
||||
if destination.is_dir() {
|
||||
let mut dest = canonicalize_with(&destination, &path)?;
|
||||
if let Some(name) = entry.file_name() {
|
||||
dest.push(name);
|
||||
}
|
||||
Ok((source_file, dest))
|
||||
} else {
|
||||
Ok((source_file, destination.clone()))
|
||||
}
|
||||
})?;
|
||||
|
||||
for (src, dst) in sources {
|
||||
if src.is_file() {
|
||||
std::fs::copy(&src, dst).map_err(|e| {
|
||||
ShellError::MoveNotPossibleSingle(
|
||||
format!(
|
||||
"failed to move containing file \"{}\": {}",
|
||||
src.to_string_lossy(),
|
||||
e
|
||||
),
|
||||
call.positional[0].span,
|
||||
)
|
||||
})?;
|
||||
}
|
||||
}
|
||||
} else if entry.is_dir() {
|
||||
let destination = if !destination.exists() {
|
||||
destination.clone()
|
||||
} else {
|
||||
match entry.file_name() {
|
||||
Some(name) => destination.join(name),
|
||||
None => {
|
||||
return Err(ShellError::FileNotFoundCustom(
|
||||
format!("containing \"{:?}\" is not a valid path", entry),
|
||||
call.positional[0].span,
|
||||
))
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
std::fs::create_dir_all(&destination).map_err(|e| {
|
||||
ShellError::MoveNotPossibleSingle(
|
||||
format!("failed to recursively fill destination: {}", e),
|
||||
call.positional[1].span,
|
||||
)
|
||||
})?;
|
||||
|
||||
let sources = sources.paths_applying_with(|(source_file, depth_level)| {
|
||||
let mut dest = destination.clone();
|
||||
let path = canonicalize_with(&source_file, &path)?;
|
||||
let components = path
|
||||
.components()
|
||||
.map(|fragment| fragment.as_os_str())
|
||||
.rev()
|
||||
.take(1 + depth_level);
|
||||
|
||||
components.for_each(|fragment| dest.push(fragment));
|
||||
Ok((PathBuf::from(&source_file), dest))
|
||||
})?;
|
||||
|
||||
for (src, dst) in sources {
|
||||
if src.is_dir() && !dst.exists() {
|
||||
std::fs::create_dir_all(&dst).map_err(|e| {
|
||||
ShellError::MoveNotPossibleSingle(
|
||||
format!(
|
||||
"failed to create containing directory \"{}\": {}",
|
||||
dst.to_string_lossy(),
|
||||
e
|
||||
),
|
||||
call.positional[1].span,
|
||||
)
|
||||
})?;
|
||||
}
|
||||
|
||||
if src.is_file() {
|
||||
std::fs::copy(&src, &dst).map_err(|e| {
|
||||
ShellError::MoveNotPossibleSingle(
|
||||
format!(
|
||||
"failed to move containing file \"{}\": {}",
|
||||
src.to_string_lossy(),
|
||||
e
|
||||
),
|
||||
call.positional[0].span,
|
||||
)
|
||||
})?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Value::Nothing { span: call.head })
|
||||
}
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
use chrono::{DateTime, Utc};
|
||||
use nu_engine::eval_expression;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EvaluationContext};
|
||||
@ -31,7 +32,17 @@ impl Command for Ls {
|
||||
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
|
||||
let pattern = if let Some(expr) = call.positional.get(0) {
|
||||
let result = eval_expression(context, expr)?;
|
||||
result.as_string()?
|
||||
let mut result = result.as_string()?;
|
||||
|
||||
let path = std::path::Path::new(&result);
|
||||
if path.is_dir() {
|
||||
if !result.ends_with(std::path::MAIN_SEPARATOR) {
|
||||
result.push(std::path::MAIN_SEPARATOR);
|
||||
}
|
||||
result.push('*');
|
||||
}
|
||||
|
||||
result
|
||||
} else {
|
||||
"*".into()
|
||||
};
|
||||
@ -49,25 +60,39 @@ impl Command for Ls {
|
||||
let is_dir = metadata.is_dir();
|
||||
let filesize = metadata.len();
|
||||
|
||||
let mut cols = vec!["name".into(), "type".into(), "size".into()];
|
||||
|
||||
let mut vals = vec![
|
||||
Value::String {
|
||||
val: path.to_string_lossy().to_string(),
|
||||
span: call_span,
|
||||
},
|
||||
if is_file {
|
||||
Value::string("File", call_span)
|
||||
} else if is_dir {
|
||||
Value::string("Dir", call_span)
|
||||
} else {
|
||||
Value::Nothing { span: call_span }
|
||||
},
|
||||
Value::Filesize {
|
||||
val: filesize as i64,
|
||||
span: call_span,
|
||||
},
|
||||
];
|
||||
|
||||
if let Ok(date) = metadata.modified() {
|
||||
let utc: DateTime<Utc> = date.into();
|
||||
|
||||
cols.push("modified".into());
|
||||
vals.push(Value::Date {
|
||||
val: utc.into(),
|
||||
span: call_span,
|
||||
});
|
||||
}
|
||||
|
||||
Value::Record {
|
||||
cols: vec!["name".into(), "type".into(), "size".into()],
|
||||
vals: vec![
|
||||
Value::String {
|
||||
val: path.to_string_lossy().to_string(),
|
||||
span: call_span,
|
||||
},
|
||||
if is_file {
|
||||
Value::string("file", call_span)
|
||||
} else if is_dir {
|
||||
Value::string("dir", call_span)
|
||||
} else {
|
||||
Value::Nothing { span: call_span }
|
||||
},
|
||||
Value::Filesize {
|
||||
val: filesize,
|
||||
span: call_span,
|
||||
},
|
||||
],
|
||||
cols,
|
||||
vals,
|
||||
span: call_span,
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,10 @@
|
||||
mod cd;
|
||||
mod cp;
|
||||
mod ls;
|
||||
mod mv;
|
||||
mod util;
|
||||
|
||||
pub use cd::Cd;
|
||||
pub use cp::Cp;
|
||||
pub use ls::Ls;
|
||||
pub use mv::Mv;
|
||||
|
139
crates/nu-command/src/filesystem/mv.rs
Normal file
139
crates/nu-command/src/filesystem/mv.rs
Normal file
@ -0,0 +1,139 @@
|
||||
use std::env::current_dir;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use nu_engine::CallExt;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EvaluationContext};
|
||||
use nu_protocol::{ShellError, Signature, SyntaxShape, Value};
|
||||
|
||||
pub struct Mv;
|
||||
|
||||
impl Command for Mv {
|
||||
fn name(&self) -> &str {
|
||||
"mv"
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Move files or directories."
|
||||
}
|
||||
|
||||
fn signature(&self) -> nu_protocol::Signature {
|
||||
Signature::build("mv")
|
||||
.required(
|
||||
"source",
|
||||
SyntaxShape::GlobPattern,
|
||||
"the location to move files/directories from",
|
||||
)
|
||||
.required(
|
||||
"destination",
|
||||
SyntaxShape::Filepath,
|
||||
"the location to move files/directories to",
|
||||
)
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
context: &EvaluationContext,
|
||||
call: &Call,
|
||||
_input: Value,
|
||||
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
|
||||
// TODO: handle invalid directory or insufficient permissions
|
||||
let source: String = call.req(context, 0)?;
|
||||
let destination: String = call.req(context, 1)?;
|
||||
|
||||
let path: PathBuf = current_dir().unwrap();
|
||||
let source = path.join(source.as_str());
|
||||
let destination = path.join(destination.as_str());
|
||||
|
||||
let mut sources =
|
||||
glob::glob(&source.to_string_lossy()).map_or_else(|_| Vec::new(), Iterator::collect);
|
||||
|
||||
if sources.is_empty() {
|
||||
return Err(ShellError::FileNotFound(
|
||||
call.positional.first().unwrap().span,
|
||||
));
|
||||
}
|
||||
|
||||
if (destination.exists() && !destination.is_dir() && sources.len() > 1)
|
||||
|| (!destination.exists() && sources.len() > 1)
|
||||
{
|
||||
return Err(ShellError::MoveNotPossible {
|
||||
source_message: "Can't move many files".to_string(),
|
||||
source_span: call.positional[0].span,
|
||||
destination_message: "into single file".to_string(),
|
||||
destination_span: call.positional[1].span,
|
||||
});
|
||||
}
|
||||
|
||||
let some_if_source_is_destination = sources
|
||||
.iter()
|
||||
.find(|f| matches!(f, Ok(f) if destination.starts_with(f)));
|
||||
if destination.exists() && destination.is_dir() && sources.len() == 1 {
|
||||
if let Some(Ok(_filename)) = some_if_source_is_destination {
|
||||
return Err(ShellError::MoveNotPossible {
|
||||
source_message: "Can't move directory".to_string(),
|
||||
source_span: call.positional[0].span,
|
||||
destination_message: "into itself".to_string(),
|
||||
destination_span: call.positional[1].span,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(Ok(_filename)) = some_if_source_is_destination {
|
||||
sources = sources
|
||||
.into_iter()
|
||||
.filter(|f| matches!(f, Ok(f) if !destination.starts_with(f)))
|
||||
.collect();
|
||||
}
|
||||
|
||||
for entry in sources.into_iter().flatten() {
|
||||
move_file(call, &entry, &destination)?
|
||||
}
|
||||
|
||||
Ok(Value::Nothing { span: call.head })
|
||||
}
|
||||
}
|
||||
|
||||
fn move_file(call: &Call, from: &Path, to: &Path) -> Result<(), ShellError> {
|
||||
if to.exists() && from.is_dir() && to.is_file() {
|
||||
return Err(ShellError::MoveNotPossible {
|
||||
source_message: "Can't move a directory".to_string(),
|
||||
source_span: call.positional[0].span,
|
||||
destination_message: "to a file".to_string(),
|
||||
destination_span: call.positional[1].span,
|
||||
});
|
||||
}
|
||||
|
||||
let destination_dir_exists = if to.is_dir() {
|
||||
true
|
||||
} else {
|
||||
to.parent().map(Path::exists).unwrap_or(true)
|
||||
};
|
||||
|
||||
if !destination_dir_exists {
|
||||
return Err(ShellError::DirectoryNotFound(call.positional[1].span));
|
||||
}
|
||||
|
||||
let mut to = to.to_path_buf();
|
||||
if to.is_dir() {
|
||||
let from_file_name = match from.file_name() {
|
||||
Some(name) => name,
|
||||
None => return Err(ShellError::DirectoryNotFound(call.positional[1].span)),
|
||||
};
|
||||
|
||||
to.push(from_file_name);
|
||||
}
|
||||
|
||||
move_item(call, from, &to)
|
||||
}
|
||||
|
||||
fn move_item(call: &Call, from: &Path, to: &Path) -> Result<(), ShellError> {
|
||||
// We first try a rename, which is a quick operation. If that doesn't work, we'll try a copy
|
||||
// and remove the old file/folder. This is necessary if we're moving across filesystems or devices.
|
||||
std::fs::rename(&from, &to).map_err(|_| ShellError::MoveNotPossible {
|
||||
source_message: "failed to move".to_string(),
|
||||
source_span: call.positional[0].span,
|
||||
destination_message: "into".to_string(),
|
||||
destination_span: call.positional[1].span,
|
||||
})
|
||||
}
|
81
crates/nu-command/src/filesystem/util.rs
Normal file
81
crates/nu-command/src/filesystem/util.rs
Normal file
@ -0,0 +1,81 @@
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use nu_path::canonicalize_with;
|
||||
use nu_protocol::ShellError;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FileStructure {
|
||||
pub resources: Vec<Resource>,
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
impl FileStructure {
|
||||
pub fn new() -> FileStructure {
|
||||
FileStructure { resources: vec![] }
|
||||
}
|
||||
|
||||
pub fn contains_more_than_one_file(&self) -> bool {
|
||||
self.resources.len() > 1
|
||||
}
|
||||
|
||||
pub fn contains_files(&self) -> bool {
|
||||
!self.resources.is_empty()
|
||||
}
|
||||
|
||||
pub fn paths_applying_with<F>(
|
||||
&mut self,
|
||||
to: F,
|
||||
) -> Result<Vec<(PathBuf, PathBuf)>, Box<dyn std::error::Error>>
|
||||
where
|
||||
F: Fn((PathBuf, usize)) -> Result<(PathBuf, PathBuf), Box<dyn std::error::Error>>,
|
||||
{
|
||||
self.resources
|
||||
.iter()
|
||||
.map(|f| (PathBuf::from(&f.location), f.at))
|
||||
.map(to)
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn walk_decorate(&mut self, start_path: &Path) -> Result<(), ShellError> {
|
||||
self.resources = Vec::<Resource>::new();
|
||||
self.build(start_path, 0)?;
|
||||
self.resources.sort();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn build(&mut self, src: &Path, lvl: usize) -> Result<(), ShellError> {
|
||||
let source = canonicalize_with(src, std::env::current_dir()?)?;
|
||||
|
||||
if source.is_dir() {
|
||||
for entry in std::fs::read_dir(src)? {
|
||||
let entry = entry?;
|
||||
let path = entry.path();
|
||||
|
||||
if path.is_dir() {
|
||||
self.build(&path, lvl + 1)?;
|
||||
}
|
||||
|
||||
self.resources.push(Resource {
|
||||
location: path.to_path_buf(),
|
||||
at: lvl,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
self.resources.push(Resource {
|
||||
location: source,
|
||||
at: lvl,
|
||||
});
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, Ord, PartialEq, PartialOrd)]
|
||||
pub struct Resource {
|
||||
pub at: usize,
|
||||
pub location: PathBuf,
|
||||
}
|
||||
|
||||
impl Resource {}
|
@ -15,7 +15,7 @@ impl Command for Get {
|
||||
}
|
||||
|
||||
fn signature(&self) -> nu_protocol::Signature {
|
||||
Signature::build("wrap").required(
|
||||
Signature::build("get").required(
|
||||
"cell_path",
|
||||
SyntaxShape::CellPath,
|
||||
"the cell path to the data",
|
||||
|
@ -86,13 +86,13 @@ fn run_ps(call: &Call) -> Result<Value, ShellError> {
|
||||
|
||||
cols.push("mem".into());
|
||||
vals.push(Value::Filesize {
|
||||
val: result.memory() * 1000,
|
||||
val: result.memory() as i64 * 1000,
|
||||
span,
|
||||
});
|
||||
|
||||
cols.push("virtual".into());
|
||||
vals.push(Value::Filesize {
|
||||
val: result.virtual_memory() * 1000,
|
||||
val: result.virtual_memory() as i64 * 1000,
|
||||
span,
|
||||
});
|
||||
|
||||
|
@ -112,13 +112,13 @@ pub fn disks(sys: &mut System, span: Span) -> Option<Value> {
|
||||
|
||||
cols.push("total".into());
|
||||
vals.push(Value::Filesize {
|
||||
val: disk.total_space(),
|
||||
val: disk.total_space() as i64,
|
||||
span,
|
||||
});
|
||||
|
||||
cols.push("free".into());
|
||||
vals.push(Value::Filesize {
|
||||
val: disk.available_space(),
|
||||
val: disk.available_space() as i64,
|
||||
span,
|
||||
});
|
||||
|
||||
@ -148,13 +148,13 @@ pub fn net(sys: &mut System, span: Span) -> Option<Value> {
|
||||
|
||||
cols.push("sent".into());
|
||||
vals.push(Value::Filesize {
|
||||
val: data.total_transmitted(),
|
||||
val: data.total_transmitted() as i64,
|
||||
span,
|
||||
});
|
||||
|
||||
cols.push("recv".into());
|
||||
vals.push(Value::Filesize {
|
||||
val: data.total_received(),
|
||||
val: data.total_received() as i64,
|
||||
span,
|
||||
});
|
||||
|
||||
@ -215,25 +215,25 @@ pub fn mem(sys: &mut System, span: Span) -> Option<Value> {
|
||||
|
||||
cols.push("total".into());
|
||||
vals.push(Value::Filesize {
|
||||
val: total_mem * 1000,
|
||||
val: total_mem as i64 * 1000,
|
||||
span,
|
||||
});
|
||||
|
||||
cols.push("free".into());
|
||||
vals.push(Value::Filesize {
|
||||
val: free_mem * 1000,
|
||||
val: free_mem as i64 * 1000,
|
||||
span,
|
||||
});
|
||||
|
||||
cols.push("swap total".into());
|
||||
vals.push(Value::Filesize {
|
||||
val: total_swap * 1000,
|
||||
val: total_swap as i64 * 1000,
|
||||
span,
|
||||
});
|
||||
|
||||
cols.push("swap free".into());
|
||||
vals.push(Value::Filesize {
|
||||
val: free_swap * 1000,
|
||||
val: free_swap as i64 * 1000,
|
||||
span,
|
||||
});
|
||||
|
||||
@ -276,7 +276,7 @@ pub fn host(sys: &mut System, span: Span) -> Option<Value> {
|
||||
}
|
||||
cols.push("uptime".into());
|
||||
vals.push(Value::Duration {
|
||||
val: 1000000000 * sys.uptime() as u64,
|
||||
val: 1000000000 * sys.uptime() as i64,
|
||||
span,
|
||||
});
|
||||
|
||||
|
Reference in New Issue
Block a user