feat: add the base32 command

This commit is contained in:
Teesh 2025-03-27 20:15:40 +02:00
parent 72f3d3dc46
commit 1bd25373cd
6 changed files with 44 additions and 0 deletions

View file

@ -0,0 +1,39 @@
use boxutils::args::ArgParser;
use boxutils::commands::Command;
use boxutils::encoding::base32;
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 Base32;
impl Command for Base32 {
fn execute(&self) {
let args = ArgParser::builder()
.add_flag("-d") // decode flag
.parse_args("base32");
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!("base32: multiple files provided"),
};
let mut buffer = String::new();
while boxutils::input::repl_with_file(&mut file, &mut buffer) {
let data = if decode {
base32::decode(buffer.clone())
} else {
base32::encode(buffer.clone())
};
println!("{}", data);
}
}
}

View file

@ -21,3 +21,4 @@ command!(whoami::WhoAmI);
command!(hostname::Hostname);
command!(tee::Tee);
command!(base64::Base64);
command!(base32::Base32);

View file

@ -31,6 +31,7 @@ pub fn get_registry() -> CommandRegistry {
"hostname" => coreutils::commands::Hostname,
"tee" => coreutils::commands::Tee,
"base64" => coreutils::commands::Base64,
"base32" => coreutils::commands::Base32,
"box" => Boxcmd
});

View file

@ -25,6 +25,7 @@ pub fn decode(to_decode: String) -> String {
}
mod tests {
#[allow(unused_imports)]
use super::*;
#[test]

View file

@ -48,6 +48,7 @@ pub fn decode(to_decode: String) -> String {
}
mod tests {
#[allow(unused_imports)]
use super::*;
#[test]

View file

@ -64,6 +64,7 @@ pub fn decode(to_decode: String) -> String {
}
mod tests {
#[allow(unused_imports)]
use super::*;
#[test]