fixed cd
This commit is contained in:
parent
877beb06f4
commit
2b39a1c695
|
@ -23,14 +23,14 @@ pub struct CommandEntry {
|
|||
}
|
||||
|
||||
impl CommandEntry {
|
||||
pub fn new(command: String) -> Self {
|
||||
pub fn new(env: String, command: String) -> Self {
|
||||
let (buffer, error) = match execute(command.clone()) {
|
||||
Ok(command_buffer) => (Some(command_buffer), String::new()),
|
||||
Err(err) => (None, format!("failed to get results: {}", err)),
|
||||
};
|
||||
|
||||
CommandEntry {
|
||||
env: format_path(&env::current_dir().unwrap_or_else(|_| PathBuf::from("/"))),
|
||||
env,
|
||||
command,
|
||||
output: String::new(),
|
||||
error,
|
||||
|
@ -58,16 +58,19 @@ impl CommandEntry {
|
|||
}
|
||||
|
||||
pub fn send_command(command: String) -> CommandEntry {
|
||||
let env = format_path(&env::current_dir().unwrap_or_else(|_| PathBuf::from("/")));
|
||||
|
||||
if command.len() < 2 {
|
||||
return CommandEntry::new(command);
|
||||
return CommandEntry::new(env, command);
|
||||
}
|
||||
|
||||
if &command[..2] != "cd" {
|
||||
return CommandEntry::new(command);
|
||||
return CommandEntry::new(env, command);
|
||||
}
|
||||
|
||||
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;
|
||||
return entry;
|
||||
}
|
||||
|
@ -76,18 +79,20 @@ pub fn send_command(command: String) -> CommandEntry {
|
|||
let path = Path::new(&path_append);
|
||||
|
||||
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;
|
||||
return entry;
|
||||
}
|
||||
|
||||
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
|
||||
} else {
|
||||
let mut entry =
|
||||
CommandEntry::new(format!("echo Could not find path : {} >&2", path.display()));
|
||||
let mut entry = CommandEntry::new(
|
||||
env,
|
||||
format!("echo Could not find path : {} >&2", path.display()),
|
||||
);
|
||||
entry.command = command;
|
||||
entry
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue