Finish adding support for protecting value shells

This commit is contained in:
Jonathan Turner 2019-08-22 16:13:40 +12:00
parent 78ca297e47
commit 303d27d4b6
5 changed files with 95 additions and 54 deletions

View File

@ -35,8 +35,6 @@ fn mkdir(
args: MkdirArgs, args: MkdirArgs,
context: &RunnablePerItemContext, context: &RunnablePerItemContext,
) -> Result<VecDeque<ReturnValue>, ShellError> { ) -> Result<VecDeque<ReturnValue>, ShellError> {
println!("shell clone");
let shell_manager = context.shell_manager.clone(); let shell_manager = context.shell_manager.clone();
println!("mkdir");
shell_manager.mkdir(args, context) shell_manager.mkdir(args, context)
} }

View File

@ -1,4 +1,4 @@
use crate::commands::command::{EvaluatedWholeStreamCommandArgs, RunnablePerItemContext}; use crate::commands::command::EvaluatedWholeStreamCommandArgs;
use crate::commands::cp::CopyArgs; use crate::commands::cp::CopyArgs;
use crate::commands::mkdir::MkdirArgs; use crate::commands::mkdir::MkdirArgs;
use crate::commands::mv::MoveArgs; use crate::commands::mv::MoveArgs;
@ -200,12 +200,13 @@ impl Shell for FilesystemShell {
dst, dst,
recursive, recursive,
}: CopyArgs, }: CopyArgs,
context: &RunnablePerItemContext, name: Span,
path: &str,
) -> Result<VecDeque<ReturnValue>, ShellError> { ) -> Result<VecDeque<ReturnValue>, ShellError> {
let name_span = context.name; let name_span = name;
let mut source = PathBuf::from(context.shell_manager.path()); let mut source = PathBuf::from(path);
let mut destination = PathBuf::from(context.shell_manager.path()); let mut destination = PathBuf::from(path);
source.push(&src.item); source.push(&src.item);
destination.push(&dst.item); destination.push(&dst.item);
@ -476,15 +477,16 @@ impl Shell for FilesystemShell {
fn mkdir( fn mkdir(
&self, &self,
mkdir_args: MkdirArgs, MkdirArgs { rest: directories }: MkdirArgs,
RunnablePerItemContext { // RunnablePerItemContext {
name, // name,
shell_manager, // shell_manager,
.. // ..
}: &RunnablePerItemContext, // }: &RunnablePerItemContext,
name: Span,
path: &str,
) -> Result<VecDeque<ReturnValue>, ShellError> { ) -> Result<VecDeque<ReturnValue>, ShellError> {
println!("full path"); let full_path = PathBuf::from(path);
let full_path = PathBuf::from(shell_manager.path());
if directories.len() == 0 { if directories.len() == 0 {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
@ -494,17 +496,13 @@ impl Shell for FilesystemShell {
)); ));
} }
println!("dirs");
for dir in directories.iter() { for dir in directories.iter() {
println!("create at:");
let create_at = { let create_at = {
let mut loc = full_path.clone(); let mut loc = full_path.clone();
loc.push(&dir.item); loc.push(&dir.item);
loc loc
}; };
println!("{:?}", create_at);
match std::fs::create_dir_all(create_at) { match std::fs::create_dir_all(create_at) {
Err(reason) => { Err(reason) => {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
@ -513,27 +511,23 @@ impl Shell for FilesystemShell {
dir.span(), dir.span(),
)) ))
} }
Ok(_) => { Ok(_) => {}
println!("OK");
}
} }
println!("End loop");
} }
println!("Done");
Ok(VecDeque::new()) Ok(VecDeque::new())
} }
fn mv( fn mv(
&self, &self,
MoveArgs { src, dst }: MoveArgs, MoveArgs { src, dst }: MoveArgs,
context: &RunnablePerItemContext, name: Span,
path: &str,
) -> Result<VecDeque<ReturnValue>, ShellError> { ) -> Result<VecDeque<ReturnValue>, ShellError> {
let name_span = context.name; let name_span = name;
let mut source = PathBuf::from(context.shell_manager.path()); let mut source = PathBuf::from(path);
let mut destination = PathBuf::from(context.shell_manager.path()); let mut destination = PathBuf::from(path);
source.push(&src.item); source.push(&src.item);
destination.push(&dst.item); destination.push(&dst.item);
@ -832,11 +826,12 @@ impl Shell for FilesystemShell {
fn rm( fn rm(
&self, &self,
RemoveArgs { target, recursive }: RemoveArgs, RemoveArgs { target, recursive }: RemoveArgs,
context: &RunnablePerItemContext, name: Span,
path: &str,
) -> Result<VecDeque<ReturnValue>, ShellError> { ) -> Result<VecDeque<ReturnValue>, 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); path.push(&target.item);

View File

@ -1,4 +1,4 @@
use crate::commands::command::{EvaluatedWholeStreamCommandArgs, RunnablePerItemContext}; use crate::commands::command::EvaluatedWholeStreamCommandArgs;
use crate::commands::cp::CopyArgs; use crate::commands::cp::CopyArgs;
use crate::commands::mkdir::MkdirArgs; use crate::commands::mkdir::MkdirArgs;
use crate::commands::mv::MoveArgs; use crate::commands::mv::MoveArgs;
@ -15,22 +15,26 @@ pub trait Shell: std::fmt::Debug {
fn cp( fn cp(
&self, &self,
args: CopyArgs, args: CopyArgs,
context: &RunnablePerItemContext, name: Span,
path: &str,
) -> Result<VecDeque<ReturnValue>, ShellError>; ) -> Result<VecDeque<ReturnValue>, ShellError>;
fn mkdir( fn mkdir(
&self, &self,
args: MkdirArgs, args: MkdirArgs,
context: &RunnablePerItemContext, name: Span,
path: &str,
) -> Result<VecDeque<ReturnValue>, ShellError>; ) -> Result<VecDeque<ReturnValue>, ShellError>;
fn mv( fn mv(
&self, &self,
args: MoveArgs, args: MoveArgs,
context: &RunnablePerItemContext, name: Span,
path: &str,
) -> Result<VecDeque<ReturnValue>, ShellError>; ) -> Result<VecDeque<ReturnValue>, ShellError>;
fn rm( fn rm(
&self, &self,
args: RemoveArgs, args: RemoveArgs,
context: &RunnablePerItemContext, name: Span,
path: &str,
) -> Result<VecDeque<ReturnValue>, ShellError>; ) -> Result<VecDeque<ReturnValue>, ShellError>;
fn path(&self) -> String; fn path(&self) -> String;
fn set_path(&mut self, path: String); fn set_path(&mut self, path: String);

View File

@ -117,9 +117,19 @@ impl ShellManager {
args: CopyArgs, args: CopyArgs,
context: &RunnablePerItemContext, context: &RunnablePerItemContext,
) -> Result<VecDeque<ReturnValue>, ShellError> { ) -> Result<VecDeque<ReturnValue>, 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( pub fn rm(
@ -127,9 +137,19 @@ impl ShellManager {
args: RemoveArgs, args: RemoveArgs,
context: &RunnablePerItemContext, context: &RunnablePerItemContext,
) -> Result<VecDeque<ReturnValue>, ShellError> { ) -> Result<VecDeque<ReturnValue>, 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( pub fn mkdir(
@ -137,9 +157,19 @@ impl ShellManager {
args: MkdirArgs, args: MkdirArgs,
context: &RunnablePerItemContext, context: &RunnablePerItemContext,
) -> Result<VecDeque<ReturnValue>, ShellError> { ) -> Result<VecDeque<ReturnValue>, 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( pub fn mv(
@ -147,8 +177,18 @@ impl ShellManager {
args: MoveArgs, args: MoveArgs,
context: &RunnablePerItemContext, context: &RunnablePerItemContext,
) -> Result<VecDeque<ReturnValue>, ShellError> { ) -> Result<VecDeque<ReturnValue>, 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,
)),
}
} }
} }

View File

@ -1,4 +1,4 @@
use crate::commands::command::{EvaluatedWholeStreamCommandArgs, RunnablePerItemContext}; use crate::commands::command::EvaluatedWholeStreamCommandArgs;
use crate::commands::cp::CopyArgs; use crate::commands::cp::CopyArgs;
use crate::commands::mkdir::MkdirArgs; use crate::commands::mkdir::MkdirArgs;
use crate::commands::mv::MoveArgs; use crate::commands::mv::MoveArgs;
@ -106,48 +106,52 @@ impl Shell for ValueShell {
fn cp( fn cp(
&self, &self,
_args: CopyArgs, _args: CopyArgs,
context: &RunnablePerItemContext, name: Span,
_path: &str,
) -> Result<VecDeque<ReturnValue>, ShellError> { ) -> Result<VecDeque<ReturnValue>, ShellError> {
Err(ShellError::labeled_error( Err(ShellError::labeled_error(
"cp not currently supported on values", "cp not currently supported on values",
"not currently supported", "not currently supported",
context.name, name,
)) ))
} }
fn mv( fn mv(
&self, &self,
_args: MoveArgs, _args: MoveArgs,
context: &RunnablePerItemContext, name: Span,
_path: &str,
) -> Result<VecDeque<ReturnValue>, ShellError> { ) -> Result<VecDeque<ReturnValue>, ShellError> {
Err(ShellError::labeled_error( Err(ShellError::labeled_error(
"mv not currently supported on values", "mv not currently supported on values",
"not currently supported", "not currently supported",
context.name, name,
)) ))
} }
fn mkdir( fn mkdir(
&self, &self,
_args: MkdirArgs, _args: MkdirArgs,
context: &RunnablePerItemContext, name: Span,
_path: &str,
) -> Result<VecDeque<ReturnValue>, ShellError> { ) -> Result<VecDeque<ReturnValue>, ShellError> {
Err(ShellError::labeled_error( Err(ShellError::labeled_error(
"mkdir not currently supported on values", "mkdir not currently supported on values",
"not currently supported", "not currently supported",
context.name, name,
)) ))
} }
fn rm( fn rm(
&self, &self,
_args: RemoveArgs, _args: RemoveArgs,
context: &RunnablePerItemContext, name: Span,
_path: &str,
) -> Result<VecDeque<ReturnValue>, ShellError> { ) -> Result<VecDeque<ReturnValue>, ShellError> {
Err(ShellError::labeled_error( Err(ShellError::labeled_error(
"rm not currently supported on values", "rm not currently supported on values",
"not currently supported", "not currently supported",
context.name, name,
)) ))
} }