From 303d27d4b6b5f0a27396c2dcb4ac62e25bacf122 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Thu, 22 Aug 2019 16:13:40 +1200 Subject: [PATCH] Finish adding support for protecting value shells --- src/commands/mkdir.rs | 2 -- src/shell/filesystem_shell.rs | 55 ++++++++++++++++------------------ src/shell/shell.rs | 14 +++++---- src/shell/shell_manager.rs | 56 ++++++++++++++++++++++++++++++----- src/shell/value_shell.rs | 22 ++++++++------ 5 files changed, 95 insertions(+), 54 deletions(-) diff --git a/src/commands/mkdir.rs b/src/commands/mkdir.rs index 668b2111ab..56fec8777e 100644 --- a/src/commands/mkdir.rs +++ b/src/commands/mkdir.rs @@ -35,8 +35,6 @@ fn mkdir( args: MkdirArgs, context: &RunnablePerItemContext, ) -> Result, ShellError> { - println!("shell clone"); let shell_manager = context.shell_manager.clone(); - println!("mkdir"); shell_manager.mkdir(args, context) } diff --git a/src/shell/filesystem_shell.rs b/src/shell/filesystem_shell.rs index 732b077437..e7038c40c4 100644 --- a/src/shell/filesystem_shell.rs +++ b/src/shell/filesystem_shell.rs @@ -1,4 +1,4 @@ -use crate::commands::command::{EvaluatedWholeStreamCommandArgs, RunnablePerItemContext}; +use crate::commands::command::EvaluatedWholeStreamCommandArgs; use crate::commands::cp::CopyArgs; use crate::commands::mkdir::MkdirArgs; use crate::commands::mv::MoveArgs; @@ -200,12 +200,13 @@ impl Shell for FilesystemShell { dst, recursive, }: CopyArgs, - context: &RunnablePerItemContext, + name: Span, + path: &str, ) -> Result, ShellError> { - let name_span = context.name; + let name_span = name; - let mut source = PathBuf::from(context.shell_manager.path()); - let mut destination = PathBuf::from(context.shell_manager.path()); + let mut source = PathBuf::from(path); + let mut destination = PathBuf::from(path); source.push(&src.item); destination.push(&dst.item); @@ -476,15 +477,16 @@ impl Shell for FilesystemShell { fn mkdir( &self, - mkdir_args: MkdirArgs, - RunnablePerItemContext { - name, - shell_manager, - .. - }: &RunnablePerItemContext, + MkdirArgs { rest: directories }: MkdirArgs, + // RunnablePerItemContext { + // name, + // shell_manager, + // .. + // }: &RunnablePerItemContext, + name: Span, + path: &str, ) -> Result, ShellError> { - println!("full path"); - let full_path = PathBuf::from(shell_manager.path()); + let full_path = PathBuf::from(path); if directories.len() == 0 { return Err(ShellError::labeled_error( @@ -494,17 +496,13 @@ impl Shell for FilesystemShell { )); } - println!("dirs"); for dir in directories.iter() { - println!("create at:"); let create_at = { let mut loc = full_path.clone(); loc.push(&dir.item); loc }; - println!("{:?}", create_at); - match std::fs::create_dir_all(create_at) { Err(reason) => { return Err(ShellError::labeled_error( @@ -513,27 +511,23 @@ impl Shell for FilesystemShell { dir.span(), )) } - Ok(_) => { - println!("OK"); - } + Ok(_) => {} } - println!("End loop"); } - println!("Done"); - Ok(VecDeque::new()) } fn mv( &self, MoveArgs { src, dst }: MoveArgs, - context: &RunnablePerItemContext, + name: Span, + path: &str, ) -> Result, ShellError> { - let name_span = context.name; + let name_span = name; - let mut source = PathBuf::from(context.shell_manager.path()); - let mut destination = PathBuf::from(context.shell_manager.path()); + let mut source = PathBuf::from(path); + let mut destination = PathBuf::from(path); source.push(&src.item); destination.push(&dst.item); @@ -832,11 +826,12 @@ impl Shell for FilesystemShell { fn rm( &self, RemoveArgs { target, recursive }: RemoveArgs, - context: &RunnablePerItemContext, + name: Span, + path: &str, ) -> Result, ShellError> { - let name_span = context.name; + let name_span = name; - let mut path = PathBuf::from(context.shell_manager.path()); + let mut path = PathBuf::from(path); path.push(&target.item); diff --git a/src/shell/shell.rs b/src/shell/shell.rs index a641d84a53..442bb351f6 100644 --- a/src/shell/shell.rs +++ b/src/shell/shell.rs @@ -1,4 +1,4 @@ -use crate::commands::command::{EvaluatedWholeStreamCommandArgs, RunnablePerItemContext}; +use crate::commands::command::EvaluatedWholeStreamCommandArgs; use crate::commands::cp::CopyArgs; use crate::commands::mkdir::MkdirArgs; use crate::commands::mv::MoveArgs; @@ -15,22 +15,26 @@ pub trait Shell: std::fmt::Debug { fn cp( &self, args: CopyArgs, - context: &RunnablePerItemContext, + name: Span, + path: &str, ) -> Result, ShellError>; fn mkdir( &self, args: MkdirArgs, - context: &RunnablePerItemContext, + name: Span, + path: &str, ) -> Result, ShellError>; fn mv( &self, args: MoveArgs, - context: &RunnablePerItemContext, + name: Span, + path: &str, ) -> Result, ShellError>; fn rm( &self, args: RemoveArgs, - context: &RunnablePerItemContext, + name: Span, + path: &str, ) -> Result, ShellError>; fn path(&self) -> String; fn set_path(&mut self, path: String); diff --git a/src/shell/shell_manager.rs b/src/shell/shell_manager.rs index 4b822dde97..cc10351dbe 100644 --- a/src/shell/shell_manager.rs +++ b/src/shell/shell_manager.rs @@ -117,9 +117,19 @@ impl ShellManager { args: CopyArgs, context: &RunnablePerItemContext, ) -> Result, ShellError> { - let env = self.shells.lock().unwrap(); + let env = self.shells.lock(); - env[self.current_shell].cp(args, context) + match env { + Ok(x) => { + let path = x[self.current_shell].path(); + x[self.current_shell].cp(args, context.name, &path) + } + Err(e) => Err(ShellError::labeled_error( + format!("Internal error: could not lock {}", e), + "Internal error: could not lock", + context.name, + )), + } } pub fn rm( @@ -127,9 +137,19 @@ impl ShellManager { args: RemoveArgs, context: &RunnablePerItemContext, ) -> Result, ShellError> { - let env = self.shells.lock().unwrap(); + let env = self.shells.lock(); - env[self.current_shell].rm(args, context) + match env { + Ok(x) => { + let path = x[self.current_shell].path(); + x[self.current_shell].rm(args, context.name, &path) + } + Err(e) => Err(ShellError::labeled_error( + format!("Internal error: could not lock {}", e), + "Internal error: could not lock", + context.name, + )), + } } pub fn mkdir( @@ -137,9 +157,19 @@ impl ShellManager { args: MkdirArgs, context: &RunnablePerItemContext, ) -> Result, ShellError> { - let env = self.shells.lock().unwrap(); + let env = self.shells.lock(); - env[self.current_shell].mkdir(args, context) + match env { + Ok(x) => { + let path = x[self.current_shell].path(); + x[self.current_shell].mkdir(args, context.name, &path) + } + Err(e) => Err(ShellError::labeled_error( + format!("Internal error: could not lock {}", e), + "Internal error: could not lock", + context.name, + )), + } } pub fn mv( @@ -147,8 +177,18 @@ impl ShellManager { args: MoveArgs, context: &RunnablePerItemContext, ) -> Result, ShellError> { - let env = self.shells.lock().unwrap(); + let env = self.shells.lock(); - env[self.current_shell].mv(args, context) + match env { + Ok(x) => { + let path = x[self.current_shell].path(); + x[self.current_shell].mv(args, context.name, &path) + } + Err(e) => Err(ShellError::labeled_error( + format!("Internal error: could not lock {}", e), + "Internal error: could not lock", + context.name, + )), + } } } diff --git a/src/shell/value_shell.rs b/src/shell/value_shell.rs index d6d80168a9..62b7de9a19 100644 --- a/src/shell/value_shell.rs +++ b/src/shell/value_shell.rs @@ -1,4 +1,4 @@ -use crate::commands::command::{EvaluatedWholeStreamCommandArgs, RunnablePerItemContext}; +use crate::commands::command::EvaluatedWholeStreamCommandArgs; use crate::commands::cp::CopyArgs; use crate::commands::mkdir::MkdirArgs; use crate::commands::mv::MoveArgs; @@ -106,48 +106,52 @@ impl Shell for ValueShell { fn cp( &self, _args: CopyArgs, - context: &RunnablePerItemContext, + name: Span, + _path: &str, ) -> Result, ShellError> { Err(ShellError::labeled_error( "cp not currently supported on values", "not currently supported", - context.name, + name, )) } fn mv( &self, _args: MoveArgs, - context: &RunnablePerItemContext, + name: Span, + _path: &str, ) -> Result, ShellError> { Err(ShellError::labeled_error( "mv not currently supported on values", "not currently supported", - context.name, + name, )) } fn mkdir( &self, _args: MkdirArgs, - context: &RunnablePerItemContext, + name: Span, + _path: &str, ) -> Result, ShellError> { Err(ShellError::labeled_error( "mkdir not currently supported on values", "not currently supported", - context.name, + name, )) } fn rm( &self, _args: RemoveArgs, - context: &RunnablePerItemContext, + name: Span, + _path: &str, ) -> Result, ShellError> { Err(ShellError::labeled_error( "rm not currently supported on values", "not currently supported", - context.name, + name, )) }