Browse Source

feat: add open command

main
fdai7375 2 years ago
parent
commit
c959957373
  1. 15
      src/builtins/open.rs

15
src/builtins/open.rs

@ -1,3 +1,5 @@
use std::{fs::File, io::stdout};
use crate::error::ShellError; use crate::error::ShellError;
use super::{Builtin, BuiltinConfig}; use super::{Builtin, BuiltinConfig};
@ -5,7 +7,18 @@ use super::{Builtin, BuiltinConfig};
pub struct Open; pub struct Open;
impl Builtin for Open { impl Builtin for Open {
fn execute(&mut self, _: &mut BuiltinConfig, _: Vec<String>) -> Result<(), ShellError> {
fn execute(&mut self, _: &mut BuiltinConfig, args: Vec<String>) -> Result<(), ShellError> {
let path = match args.get(0) {
Some(p) => p,
None => return Err(ShellError::MalformedArgs("No file specified".to_string())),
};
let mut file = match File::open(path) {
Ok(f) => f,
Err(e) => return Err(ShellError::ExecuteFailure(e.to_string())),
};
if let Err(e) = std::io::copy(&mut file, &mut stdout()) {
return Err(ShellError::ExecuteFailure(e.to_string()));
}
Ok(()) Ok(())
} }
} }
Loading…
Cancel
Save