From c959957373bfe714c0fdc8901cd8dcc3617bd3d6 Mon Sep 17 00:00:00 2001 From: fdai7375 Date: Tue, 24 Jan 2023 09:46:56 +0100 Subject: [PATCH] feat: add open command --- src/builtins/open.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/builtins/open.rs b/src/builtins/open.rs index b3cfbf4..6dec080 100644 --- a/src/builtins/open.rs +++ b/src/builtins/open.rs @@ -1,3 +1,5 @@ +use std::{fs::File, io::stdout}; + use crate::error::ShellError; use super::{Builtin, BuiltinConfig}; @@ -5,7 +7,18 @@ use super::{Builtin, BuiltinConfig}; pub struct Open; impl Builtin for Open { - fn execute(&mut self, _: &mut BuiltinConfig, _: Vec) -> Result<(), ShellError> { + fn execute(&mut self, _: &mut BuiltinConfig, args: Vec) -> 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(()) } }