updating builtin definitions
parent
621f64d3f1
commit
9c7d64e512
@ -0,0 +1,35 @@
|
||||
mod echo;
|
||||
mod printenv;
|
||||
|
||||
use crate::{
|
||||
error::ExecError,
|
||||
run::{Call, Context, Value},
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum Builtin {
|
||||
Echo,
|
||||
Printenv,
|
||||
}
|
||||
|
||||
impl Builtin {
|
||||
fn resolve(&self) -> &dyn Call {
|
||||
use Builtin::*;
|
||||
match self {
|
||||
Echo => &echo::Echo,
|
||||
Printenv => &printenv::Printenv,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Call for Builtin {
|
||||
fn call(&self, ctx: &mut Context, args: &[Value]) -> Result<Value, ExecError> {
|
||||
self.resolve().call(ctx, args)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn all() -> HashMap<&'static str, Builtin> {
|
||||
use Builtin::*;
|
||||
HashMap::from([("echo", Echo), ("printenv", Printenv)])
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
use crate::{
|
||||
error::ExecError,
|
||||
run::{Call, Context, Value},
|
||||
};
|
||||
use std::io::Write;
|
||||
|
||||
pub struct Echo;
|
||||
|
||||
impl Call for Echo {
|
||||
fn call(&self, ctx: &mut Context, args: &[Value]) -> Result<Value, ExecError> {
|
||||
let args = args
|
||||
.into_iter()
|
||||
.map(|v| v.try_as_str())
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
_ = write!(ctx.stdout, "{}\n", args.join(" "));
|
||||
Ok(Value::None)
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
use crate::{
|
||||
error::ExecError,
|
||||
run::{Call, Context, Value},
|
||||
};
|
||||
use std::{env, io::Write};
|
||||
|
||||
pub struct Printenv;
|
||||
|
||||
impl Call for Printenv {
|
||||
fn call(&self, ctx: &mut Context, args: &[Value]) -> Result<Value, ExecError> {
|
||||
if args.len() > 0 {
|
||||
for arg in args {
|
||||
let name = arg.try_as_str()?;
|
||||
_ = match env::var(name) {
|
||||
Ok(val) => writeln!(ctx.stdout, "{}", val),
|
||||
Err(e) => writeln!(ctx.stderr, "ERROR {}", e),
|
||||
};
|
||||
}
|
||||
} else {
|
||||
for (name, val) in env::vars() {
|
||||
_ = writeln!(ctx.stdout, "{name}={val}");
|
||||
}
|
||||
}
|
||||
Ok(Value::None)
|
||||
}
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
use crate::{
|
||||
error::ExecError,
|
||||
run::{Call, Context, Value},
|
||||
};
|
||||
use std::{boxed::Box, collections::HashMap, io::Write};
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum Builtin {
|
||||
Echo,
|
||||
}
|
||||
|
||||
impl Call for Builtin {
|
||||
fn call(&self, ctx: &mut Context, args: &[Value]) -> Result<Value, ExecError> {
|
||||
match self {
|
||||
Builtin::Echo => Echo.call(ctx, args),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Echo;
|
||||
|
||||
impl Call for Echo {
|
||||
fn call(&self, ctx: &mut Context, args: &[Value]) -> Result<Value, ExecError> {
|
||||
let args = args
|
||||
.into_iter()
|
||||
.map(|v| v.try_as_str())
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
_ = write!(ctx.stdout, "{}\n", args.join(" "));
|
||||
Ok(Value::None)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn all() -> HashMap<&'static str, Builtin> {
|
||||
use Builtin::*;
|
||||
HashMap::from([("echo", Echo)])
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
use crate::ext::Command;
|
||||
use anyhow::Result;
|
||||
|
||||
pub struct Echo {}
|
||||
|
||||
impl Command for Echo {
|
||||
fn name() -> String {
|
||||
String::from("echo")
|
||||
}
|
||||
|
||||
fn create() -> Self {
|
||||
Self {}
|
||||
}
|
||||
|
||||
fn exec(&mut self, args: Vec<&str>) -> Result<bool> {
|
||||
println!("{}", args.join(" "));
|
||||
Ok(true)
|
||||
}
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
use crate::ext::Command;
|
||||
use anyhow::Result;
|
||||
|
||||
pub struct Printenv {}
|
||||
|
||||
impl Command for Printenv {
|
||||
fn name() -> String {
|
||||
String::from("printenv")
|
||||
}
|
||||
|
||||
fn create() -> Self {
|
||||
Self {}
|
||||
}
|
||||
|
||||
fn exec(&mut self, args: Vec<&str>) -> Result<bool> {
|
||||
if args.len() > 0 {
|
||||
let name = args[0];
|
||||
match std::env::var(name) {
|
||||
Ok(val) => {
|
||||
println!("{}", val);
|
||||
Ok(true)
|
||||
}
|
||||
Err(e) => {
|
||||
println!("ERROR {}", e);
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
println!("which variable you fucking dork");
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue