From 72f3d3dc469934bfd504c6fccd394d76ee6e02a8 Mon Sep 17 00:00:00 2001 From: teesh3rt Date: Thu, 27 Mar 2025 16:46:41 +0200 Subject: [PATCH] feat: add the base64 command --- coreutils/src/commands/base64.rs | 39 ++++++++++++++++++++++++++++++++ coreutils/src/commands/mod.rs | 1 + src/registry.rs | 1 + 3 files changed, 41 insertions(+) create mode 100644 coreutils/src/commands/base64.rs diff --git a/coreutils/src/commands/base64.rs b/coreutils/src/commands/base64.rs new file mode 100644 index 0000000..ec20700 --- /dev/null +++ b/coreutils/src/commands/base64.rs @@ -0,0 +1,39 @@ +use boxutils::args::ArgParser; +use boxutils::commands::Command; +use boxutils::encoding::base64; +use std::fs::OpenOptions; +use std::io::Read; + +// TODO: Add the -w flag +// we dont have a way to do text +// warping in boxutils yet haha + +pub struct Base64; + +impl Command for Base64 { + fn execute(&self) { + let args = ArgParser::builder() + .add_flag("-d") // decode flag + .parse_args("base64"); + + let decode = args.get_flag("-d"); + + // FIXME: This is jank! + let mut file: Box = match &args.get_normal_args()[..] { + [] => Box::new(std::io::stdin()), + [file] => Box::new(OpenOptions::new().read(true).open(file).unwrap()), + _ => panic!("base64: multiple files provided"), + }; + + let mut buffer = String::new(); + while boxutils::input::repl_with_file(&mut file, &mut buffer) { + let data = if decode { + base64::decode(buffer.clone()) + } else { + base64::encode(buffer.clone()) + }; + + println!("{}", data); + } + } +} diff --git a/coreutils/src/commands/mod.rs b/coreutils/src/commands/mod.rs index 30ba6a7..44e70eb 100644 --- a/coreutils/src/commands/mod.rs +++ b/coreutils/src/commands/mod.rs @@ -20,3 +20,4 @@ command!(sleep::Sleep); command!(whoami::WhoAmI); command!(hostname::Hostname); command!(tee::Tee); +command!(base64::Base64); diff --git a/src/registry.rs b/src/registry.rs index a848fb8..50e51c8 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -30,6 +30,7 @@ pub fn get_registry() -> CommandRegistry { "whoami" => coreutils::commands::WhoAmI, "hostname" => coreutils::commands::Hostname, "tee" => coreutils::commands::Tee, + "base64" => coreutils::commands::Base64, "box" => Boxcmd });