feat: add the base64 command

This commit is contained in:
Teesh 2025-03-27 16:46:41 +02:00
parent 979a6c5979
commit 72f3d3dc46
3 changed files with 41 additions and 0 deletions

View file

@ -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<dyn Read> = 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);
}
}
}

View file

@ -20,3 +20,4 @@ command!(sleep::Sleep);
command!(whoami::WhoAmI);
command!(hostname::Hostname);
command!(tee::Tee);
command!(base64::Base64);

View file

@ -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
});