Merge pull request #205 from androbtech/cp_play

Introduced initial cp functionality.
This commit is contained in:
Jonathan Turner 2019-07-24 04:46:30 +12:00 committed by GitHub
commit 72384629fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 146 additions and 16 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 |
| ------------- | ------------- |
| cd path | Change to a new path |
| cp source path | Copy files |
| ls (path) | View the contents of the current or given path |
| ps | View current processes |
| sysinfo | View information about the current system |
@ -144,6 +145,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-toml | Convert table into .toml text |
| to-yaml | Convert table into .yaml text |
| to-csv | Convert table into .csv text |
## Filters on text (unstructured data)
| 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("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 dst = String::new();
match args
.nth(0)
.ok_or_else(|| ShellError::string(&format!("No file or directory specified")))?
.as_string()?
.as_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)",
source.to_string_lossy()
)));
}
}
match std::fs::copy(source, destination) {
Err(_error) => Err(ShellError::string("can not copy file")),
Ok(_) => 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

@ -7,7 +7,8 @@ use helpers as h;
fn lines() {
nu!(output,
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");
}
@ -38,7 +39,8 @@ fn open_can_parse_toml() {
fn open_can_parse_json() {
nu!(output,
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")
}
@ -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"));
}
#[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]
fn rm_can_remove_a_file() {
let directory = "tests/fixtures/nuplayground";

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() {
nu!(output,
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");
}
@ -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() {
nu!(output,
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");
}
@ -47,7 +49,8 @@ fn can_convert_table_to_yaml_text_and_from_yaml_text_back_into_table() {
fn can_sort_by_column() {
nu!(output,
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");
}
@ -56,7 +59,8 @@ fn can_sort_by_column() {
fn can_split_by_column() {
nu!(output,
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");
}

View File

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