This commit is contained in:
Penwing 2024-01-26 08:33:55 +01:00
parent 877beb06f4
commit 2b39a1c695

View file

@ -23,14 +23,14 @@ pub struct CommandEntry {
} }
impl CommandEntry { impl CommandEntry {
pub fn new(command: String) -> Self { pub fn new(env: String, command: String) -> Self {
let (buffer, error) = match execute(command.clone()) { let (buffer, error) = match execute(command.clone()) {
Ok(command_buffer) => (Some(command_buffer), String::new()), Ok(command_buffer) => (Some(command_buffer), String::new()),
Err(err) => (None, format!("failed to get results: {}", err)), Err(err) => (None, format!("failed to get results: {}", err)),
}; };
CommandEntry { CommandEntry {
env: format_path(&env::current_dir().unwrap_or_else(|_| PathBuf::from("/"))), env,
command, command,
output: String::new(), output: String::new(),
error, error,
@ -58,16 +58,19 @@ impl CommandEntry {
} }
pub fn send_command(command: String) -> CommandEntry { pub fn send_command(command: String) -> CommandEntry {
let env = format_path(&env::current_dir().unwrap_or_else(|_| PathBuf::from("/")));
if command.len() < 2 { if command.len() < 2 {
return CommandEntry::new(command); return CommandEntry::new(env, command);
} }
if &command[..2] != "cd" { if &command[..2] != "cd" {
return CommandEntry::new(command); return CommandEntry::new(env, command);
} }
if command.len() < 4 { if command.len() < 4 {
let mut entry = CommandEntry::new("echo Invalid cd, should provide path >&2".to_string()); let mut entry =
CommandEntry::new(env, "echo Invalid cd, should provide path >&2".to_string());
entry.command = command; entry.command = command;
return entry; return entry;
} }
@ -76,18 +79,20 @@ pub fn send_command(command: String) -> CommandEntry {
let path = Path::new(&path_append); let path = Path::new(&path_append);
if format!("{}", path.display()) == "/" { if format!("{}", path.display()) == "/" {
let mut entry = CommandEntry::new("echo Root access denied >&2".to_string()); let mut entry = CommandEntry::new(env, "echo Root access denied >&2".to_string());
entry.command = command; entry.command = command;
return entry; return entry;
} }
if env::set_current_dir(path).is_ok() { if env::set_current_dir(path).is_ok() {
let mut entry = CommandEntry::new(format!("echo Moved to : {}", path.display())); let mut entry = CommandEntry::new(env, format!("echo Moved to : {}", path.display()));
entry.command = command; entry.command = command;
entry entry
} else { } else {
let mut entry = let mut entry = CommandEntry::new(
CommandEntry::new(format!("echo Could not find path : {} >&2", path.display())); env,
format!("echo Could not find path : {} >&2", path.display()),
);
entry.command = command; entry.command = command;
entry entry
} }