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

@ -120,6 +120,7 @@ Nu adheres closely to a set of goals that make up its design philosophy. As feat
| command | description | | command | description |
| ------------- | ------------- | | ------------- | ------------- |
| cd path | Change to a new path | | cd path | Change to a new path |
| cp source path | Copy files |
| ls (path) | View the contents of the current or given path | | ls (path) | View the contents of the current or given path |
| ps | View current processes | | ps | View current processes |
| sysinfo | View information about the current system | | sysinfo | View information about the current system |
@ -142,6 +143,7 @@ Nu adheres closely to a set of goals that make up its design philosophy. As feat
| to-json | Convert table into .json text | | to-json | Convert table into .json text |
| to-toml | Convert table into .toml text | | to-toml | Convert table into .toml text |
| to-yaml | Convert table into .yaml text | | to-yaml | Convert table into .yaml text |
| to-csv | Convert table into .csv text |
## Filters on text (unstructured data) ## Filters on text (unstructured data)
| command | description | | command | description |

View File

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

View File

@ -4,6 +4,7 @@ crate mod macros;
crate mod args; crate mod args;
crate mod autoview; crate mod autoview;
crate mod cd; crate mod cd;
crate mod cp;
crate mod rm; crate mod rm;
crate mod classified; crate mod classified;
crate mod clip; crate mod clip;
@ -44,6 +45,7 @@ crate mod where_;
crate use command::command; crate use command::command;
crate use config::Config; crate use config::Config;
crate use cp::Copycp;
crate use rm::Remove; crate use rm::Remove;
crate use open::Open; crate use open::Open;
crate use skip_while::SkipWhile; 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)], positional: vec![PositionalType::mandatory("file", SyntaxType::Path)],
rest_positional: false, rest_positional: false,
named, named,
is_sink: true, is_sink: false,
is_filter: false, is_filter: false,
} }
} }

View File

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

View File

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

View File

@ -7,7 +7,8 @@ use helpers as h;
fn lines() { fn lines() {
nu!(output, nu!(output,
cwd("tests/fixtures/formats"), cwd("tests/fixtures/formats"),
"open cargo_sample.toml --raw | lines | skip-while $it != \"[dependencies]\" | skip 1 | first 1 | split-column \"=\" | get Column1 | trim | echo $it"); "open cargo_sample.toml --raw | lines | skip-while $it != \"[dependencies]\" | skip 1 | first 1 | split-column \"=\" | get Column1 | trim | echo $it"
);
assert_eq!(output, "rustyline"); assert_eq!(output, "rustyline");
} }
@ -38,7 +39,8 @@ fn open_can_parse_toml() {
fn open_can_parse_json() { fn open_can_parse_json() {
nu!(output, nu!(output,
cwd("tests/fixtures/formats"), cwd("tests/fixtures/formats"),
"open sgml_description.json | get glossary.GlossDiv.GlossList.GlossEntry.GlossSee | echo $it"); "open sgml_description.json | get glossary.GlossDiv.GlossList.GlossEntry.GlossSee | echo $it"
);
assert_eq!(output, "markup") assert_eq!(output, "markup")
} }
@ -96,6 +98,52 @@ fn save_can_write_out_csv() {
assert!(actual.contains("[list list],A shell for the GitHub era,2018,ISC,nu,0.2.0")); assert!(actual.contains("[list list],A shell for the GitHub era,2018,ISC,nu,0.2.0"));
} }
#[test]
fn cp_can_copy_a_file() {
let (playground_path, tests_dir) = h::setup_playground_for("cp_test");
let full_path = format!("{}/{}", playground_path, tests_dir );
let expected_file = format!("{}/{}", full_path , "sample.ini" );
nu!(
_output,
cwd(&playground_path),
"cp ../formats/sample.ini cp_test/sample.ini"
);
assert!(h::file_exists_at(&expected_file));
}
#[test]
fn cp_copies_the_file_inside_directory_if_path_to_copy_is_directory() {
let (playground_path, tests_dir) = h::setup_playground_for("cp_test_2");
let full_path = format!("{}/{}", playground_path, tests_dir );
let expected_file = format!("{}/{}", full_path , "sample.ini" );
nu!(
_output,
cwd(&playground_path),
"cp ../formats/sample.ini cp_test_2"
);
assert!(h::file_exists_at(&expected_file));
}
#[test]
fn cp_error_if_attempting_to_copy_a_directory_to_another_directory() {
let (playground_path, _) = h::setup_playground_for("cp_test_3");
nu_error!(
output,
cwd(&playground_path),
"cp ../formats cp_test_3"
);
assert!(output.contains("../formats"));
assert!(output.contains("is a directory (not copied)"));
}
#[test] #[test]
fn rm_can_remove_a_file() { fn rm_can_remove_a_file() {
let directory = "tests/fixtures/nuplayground"; let directory = "tests/fixtures/nuplayground";
@ -149,4 +197,4 @@ fn rm_error_if_attempting_to_delete_two_dot_as_argument() {
nu_error!(output, cwd("tests/fixtures/nuplayground"), "rm .."); nu_error!(output, cwd("tests/fixtures/nuplayground"), "rm ..");
assert!(output.contains("may not be removed")); assert!(output.contains("may not be removed"));
} }

View File

@ -7,7 +7,8 @@ use helpers::in_directory as cwd;
fn can_convert_table_to_csv_text_and_from_csv_text_back_into_table() { fn can_convert_table_to_csv_text_and_from_csv_text_back_into_table() {
nu!(output, nu!(output,
cwd("tests/fixtures/formats"), cwd("tests/fixtures/formats"),
"open caco3_plastics.csv | to-csv | from-csv | first 1 | get origin | echo $it"); "open caco3_plastics.csv | to-csv | from-csv | first 1 | get origin | echo $it"
);
assert_eq!(output, "SPAIN"); assert_eq!(output, "SPAIN");
} }
@ -16,7 +17,8 @@ fn can_convert_table_to_csv_text_and_from_csv_text_back_into_table() {
fn can_convert_table_to_json_text_and_from_json_text_back_into_table() { fn can_convert_table_to_json_text_and_from_json_text_back_into_table() {
nu!(output, nu!(output,
cwd("tests/fixtures/formats"), cwd("tests/fixtures/formats"),
"open sgml_description.json | to-json | from-json | get glossary.GlossDiv.GlossList.GlossEntry.GlossSee | echo $it"); "open sgml_description.json | to-json | from-json | get glossary.GlossDiv.GlossList.GlossEntry.GlossSee | echo $it"
);
assert_eq!(output, "markup"); assert_eq!(output, "markup");
} }
@ -47,7 +49,8 @@ fn can_convert_table_to_yaml_text_and_from_yaml_text_back_into_table() {
fn can_sort_by_column() { fn can_sort_by_column() {
nu!(output, nu!(output,
cwd("tests/fixtures/formats"), cwd("tests/fixtures/formats"),
"open cargo_sample.toml --raw | lines | skip 1 | first 4 | split-column \"=\" | sort-by Column1 | skip 1 | first 1 | get Column1 | trim | echo $it"); "open cargo_sample.toml --raw | lines | skip 1 | first 4 | split-column \"=\" | sort-by Column1 | skip 1 | first 1 | get Column1 | trim | echo $it"
);
assert_eq!(output, "description"); assert_eq!(output, "description");
} }
@ -56,7 +59,8 @@ fn can_sort_by_column() {
fn can_split_by_column() { fn can_split_by_column() {
nu!(output, nu!(output,
cwd("tests/fixtures/formats"), cwd("tests/fixtures/formats"),
"open cargo_sample.toml --raw | lines | skip 1 | first 1 | split-column \"=\" | get Column1 | trim | echo $it"); "open cargo_sample.toml --raw | lines | skip 1 | first 1 | split-column \"=\" | get Column1 | trim | echo $it"
);
assert_eq!(output, "name"); assert_eq!(output, "name");
} }

View File

@ -1,2 +1,2 @@
*_test *_test*
*.txt *.txt