[Teton] Add ls and cd commands to rust impl.

This commit is contained in:
Drew Galbraith 2024-08-14 08:47:29 -07:00
parent c155247f1d
commit f5a27156d2
4 changed files with 71 additions and 20 deletions

View File

@ -0,0 +1,27 @@
use alloc::{
string::{String, ToString as _},
vec::Vec,
};
use mammoth::zion::ZError;
pub fn exists(path: &str) -> Result<bool, ZError> {
let vfs = crate::get_client();
let result = vfs.get_directory(&crate::GetDirectoryRequest {
path: path.to_string(),
});
match result {
Ok(_) => Ok(true),
Err(ZError::NOT_FOUND) => Ok(false),
Err(e) => Err(e),
}
}
pub fn ls(path: &str) -> Result<Vec<String>, ZError> {
let vfs = crate::get_client();
let result = vfs.get_directory(&crate::GetDirectoryRequest {
path: path.to_string(),
})?;
Ok(result.filenames.split(',').map(|s| s.to_string()).collect())
}

View File

@ -1,32 +1,14 @@
use crate::OpenFileRequest;
use crate::VFSClient;
use alloc::string::ToString;
use mammoth::zion::ZError;
static mut VFS_CLIENT: Option<VFSClient> = None;
fn get_client() -> &'static mut VFSClient {
unsafe {
if let None = VFS_CLIENT {
let endpoint_cap = yellowstone::from_init_endpoint()
.get_endpoint(&yellowstone::GetEndpointRequest {
endpoint_name: "victoriafalls".to_string(),
})
.expect("Failed to get VFS endpoint");
VFS_CLIENT = Some(VFSClient::new(endpoint_cap.endpoint));
}
VFS_CLIENT.as_mut().unwrap()
}
}
pub struct File {
memory: mammoth::mem::MemoryRegion,
}
impl File {
pub fn open(path: &str) -> Result<Self, ZError> {
let vfs = get_client();
let vfs = crate::get_client();
let resp = vfs.open_file(&OpenFileRequest {
path: path.to_string(),
})?;

View File

@ -4,4 +4,22 @@ use core::include;
include!(concat!(env!("OUT_DIR"), "/yunq.rs"));
pub mod dir;
pub mod file;
static mut VFS_CLIENT: Option<VFSClient> = None;
fn get_client() -> &'static mut VFSClient {
unsafe {
if let None = VFS_CLIENT {
let endpoint_cap = yellowstone::from_init_endpoint()
.get_endpoint(&yellowstone::GetEndpointRequest {
endpoint_name: "victoriafalls".to_string(),
})
.expect("Failed to get VFS endpoint");
VFS_CLIENT = Some(VFSClient::new(endpoint_cap.endpoint));
}
VFS_CLIENT.as_mut().unwrap()
}
}

View File

@ -5,6 +5,7 @@ use alloc::{
format,
string::{String, ToString},
};
use victoriafalls::dir;
use voyageurs::listener::KeyboardHandler;
pub struct Terminal {
@ -84,10 +85,33 @@ impl Terminal {
self.curr_cmd.clear()
}
fn execute_command_parsed(&mut self, cmd: &str, _args: Split<'_, char>) {
fn execute_command_parsed(&mut self, cmd: &str, mut args: Split<'_, char>) {
// TODO: Check that no extraneous params are provided.
match cmd {
"help" => self.write_line("Available commands are 'pwd', 'ls', 'cd', and 'exec'"),
"pwd" => self.write_line(&self.cwd.clone()),
"cd" => match args.next() {
None => self.write_line("Specify a directory"),
Some(directory) => match dir::exists(directory) {
Ok(true) => self.cwd = directory.to_string(),
Ok(false) => self.write_line(&format!("Directory not found: {}", directory)),
Err(e) => self.write_line(&format!("Error stating directory {:?}", e)),
},
},
"ls" => {
let use_dir = match args.next() {
None => self.cwd.clone(),
Some(d) => d.to_string(),
};
match dir::ls(&use_dir) {
Err(e) => self.write_line(&format!("Error reading directory {:?}", e)),
Ok(files) => {
for file in files {
self.write_line(&file);
}
}
}
}
_ => self.write_line(&format!("Unrecognized command: {}", cmd)),
}
}