Introduced initial cp functionality.

This commit is contained in:
Andrés N. Robalino
2019-07-21 21:23:02 -05:00
parent 12a785f2a2
commit 2da43f4b06
10 changed files with 146 additions and 17 deletions

View File

@ -178,6 +178,7 @@ pub async fn cli() -> Result<(), Box<dyn Error>> {
command("to-yaml", Box::new(to_yaml::to_yaml)),
command("sort-by", Box::new(sort_by::sort_by)),
Arc::new(Remove),
Arc::new(Copycp),
Arc::new(Open),
Arc::new(Where),
Arc::new(Config),

View File

@ -4,6 +4,7 @@ crate mod macros;
crate mod args;
crate mod autoview;
crate mod cd;
crate mod cp;
crate mod rm;
crate mod classified;
crate mod clip;
@ -44,6 +45,7 @@ crate mod where_;
crate use command::command;
crate use config::Config;
crate use cp::Copycp;
crate use rm::Remove;
crate use open::Open;
crate use skip_while::SkipWhile;

80
src/commands/cp.rs Normal file
View File

@ -0,0 +1,80 @@
use crate::errors::ShellError;
use crate::parser::hir::SyntaxType;
use crate::parser::registry::{CommandConfig, NamedType, PositionalType};
use crate::prelude::*;
use indexmap::IndexMap;
use std::path::Path;
pub struct Copycp;
impl Command for Copycp {
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
cp(args)
}
fn name(&self) -> &str {
"cp"
}
fn config(&self) -> CommandConfig {
let mut named: IndexMap<String, NamedType> = IndexMap::new();
named.insert("recursive".to_string(), NamedType::Switch);
CommandConfig {
name: self.name().to_string(),
positional: vec![PositionalType::mandatory("file", SyntaxType::Path)],
rest_positional: false,
named,
is_sink: false,
is_filter: false,
}
}
}
pub fn cp(args: CommandArgs) -> Result<OutputStream, ShellError> {
let mut source = args.env.lock().unwrap().path().to_path_buf();
let mut destination = args.env.lock().unwrap().path().to_path_buf();
let mut src = String::new();
let mut dst = String::new();
match args
.nth(0)
.ok_or_else(|| ShellError::string(&format!("No file or directory specified")))?
.as_string()?
.as_str() {
file => {
src.push_str(file);
source.push(file);
}
}
match args
.nth(1)
.ok_or_else(|| ShellError::string(&format!("No file or directory specified")))?
.as_string()?
.as_str() {
file => {
dst.push_str(file);
destination.push(file);
}
}
if destination.is_dir() {
if source.is_file() {
let file_name = source.file_name().expect("");
let file_name = file_name.to_str().expect("");
destination.push(Path::new(file_name));
} else if source.is_dir() {
return Err(ShellError::string(
&format!("{:?} is a directory (not copied)", src))
);
}
}
std::fs::copy(source, destination).expect("can not copy file");
Ok(OutputStream::empty())
}

View File

@ -24,7 +24,7 @@ impl Command for Remove {
positional: vec![PositionalType::mandatory("file", SyntaxType::Path)],
rest_positional: false,
named,
is_sink: true,
is_sink: false,
is_filter: false,
}
}

View File

@ -1,12 +1,8 @@
use crate::object::{Primitive, Value};
use crate::prelude::*;
use log::debug;
use csv::WriterBuilder;
pub fn value_to_csv_value(v: &Value) -> Value {
debug!("value_to_csv_value(Value::Object(v)) where v = {:?}", v);
match v {
Value::Primitive(Primitive::String(s)) => Value::Primitive(Primitive::String(s.clone())),
Value::Primitive(Primitive::Nothing) => Value::Primitive(Primitive::Nothing),
@ -21,9 +17,6 @@ pub fn to_string(v: &Value) -> Result<String, Box<dyn std::error::Error>> {
match v {
Value::List(_l) => return Ok(String::from("[list list]")),
Value::Object(o) => {
debug!("to_csv:to_string(Value::Object(v)) where v = {:?}", v);
let mut wtr = WriterBuilder::new().from_writer(vec![]);
let mut fields: VecDeque<String> = VecDeque::new();
let mut values: VecDeque<String> = VecDeque::new();

View File

@ -1,6 +1,5 @@
use crate::object::{Primitive, Value};
use crate::prelude::*;
use log::trace;
pub fn value_to_json_value(v: &Value) -> serde_json::Value {
match v {