2022-12-26 23:44:53 -05:00
|
|
|
use super::Cmd;
|
2023-01-03 19:47:00 -05:00
|
|
|
use clap::{value_parser, Arg, ArgAction, Command};
|
2022-12-26 23:44:53 -05:00
|
|
|
use data_encoding::BASE32;
|
|
|
|
use std::{
|
2023-01-04 15:14:16 -05:00
|
|
|
error::Error,
|
2022-12-26 23:44:53 -05:00
|
|
|
fs,
|
2023-01-04 14:26:44 -05:00
|
|
|
io::{self, Read, Write},
|
2022-12-26 23:44:53 -05:00
|
|
|
};
|
2023-01-04 14:26:44 -05:00
|
|
|
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
|
2022-12-26 23:44:53 -05:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Base32 {
|
|
|
|
name: &'static str,
|
|
|
|
path: Option<crate::Path>,
|
|
|
|
}
|
|
|
|
|
2023-01-06 23:41:02 -05:00
|
|
|
impl Default for Base32 {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
name: "base32",
|
|
|
|
path: Some(crate::Path::UsrBin),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-26 23:44:53 -05:00
|
|
|
|
|
|
|
impl Cmd for Base32 {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
self.name
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cli(&self) -> clap::Command {
|
|
|
|
Command::new("base32")
|
2023-01-03 12:49:09 -05:00
|
|
|
.author("Nathan Fisher")
|
2022-12-26 23:44:53 -05:00
|
|
|
.about("Base32 encode/decode data and print to standard output")
|
|
|
|
.args([
|
|
|
|
Arg::new("INPUT")
|
|
|
|
.help("The input file to use")
|
|
|
|
.num_args(1..),
|
|
|
|
Arg::new("DECODE")
|
|
|
|
.help("Decode rather than encode")
|
|
|
|
.short('d')
|
|
|
|
.long("decode")
|
|
|
|
.action(ArgAction::SetTrue),
|
|
|
|
Arg::new("IGNORE")
|
|
|
|
.help("Ignore whitespace when decoding")
|
|
|
|
.short('i')
|
|
|
|
.long("ignore-space")
|
|
|
|
.action(ArgAction::SetTrue),
|
|
|
|
Arg::new("WRAP")
|
|
|
|
.help("Wrap encoded lines after n characters")
|
|
|
|
.short('w')
|
|
|
|
.long("wrap")
|
2023-01-01 11:26:56 -05:00
|
|
|
.value_parser(value_parser!(usize))
|
2022-12-26 23:44:53 -05:00
|
|
|
.default_value("76"),
|
2023-01-04 14:26:44 -05:00
|
|
|
Arg::new("color")
|
|
|
|
.short('c')
|
|
|
|
.long("color")
|
|
|
|
.value_parser(["always", "ansi", "auto", "never"]),
|
2022-12-26 23:44:53 -05:00
|
|
|
Arg::new("VERBOSE")
|
|
|
|
.help("Display a header naming each file")
|
|
|
|
.short('v')
|
|
|
|
.long("verbose")
|
|
|
|
.action(ArgAction::SetTrue),
|
|
|
|
Arg::new("QUIET")
|
|
|
|
.help("Do not display header, even with multiple files")
|
|
|
|
.short('q')
|
|
|
|
.long("quiet")
|
|
|
|
.action(ArgAction::SetTrue),
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(&self, matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> {
|
2023-01-03 19:47:00 -05:00
|
|
|
let Some(matches) = matches else {
|
|
|
|
return Err(io::Error::new(io::ErrorKind::Other, "No input").into());
|
2022-12-26 23:44:53 -05:00
|
|
|
};
|
|
|
|
let files: Vec<_> = match matches.get_many::<String>("INPUT") {
|
2023-01-04 15:14:16 -05:00
|
|
|
Some(c) => c.cloned().collect(),
|
2022-12-26 23:44:53 -05:00
|
|
|
None => vec![String::from("-")],
|
|
|
|
};
|
|
|
|
let len = files.len();
|
2023-01-04 15:14:16 -05:00
|
|
|
let color = match matches.get_one::<String>("color").map(String::as_str) {
|
2023-01-04 14:26:44 -05:00
|
|
|
Some("always") => ColorChoice::Always,
|
|
|
|
Some("ansi") => ColorChoice::AlwaysAnsi,
|
|
|
|
Some("auto") => {
|
|
|
|
if atty::is(atty::Stream::Stdout) {
|
|
|
|
ColorChoice::Auto
|
|
|
|
} else {
|
|
|
|
ColorChoice::Never
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => ColorChoice::Never,
|
|
|
|
};
|
2022-12-26 23:44:53 -05:00
|
|
|
for (index, file) in files.into_iter().enumerate() {
|
|
|
|
if { len > 1 || matches.get_flag("VERBOSE") } && !matches.get_flag("QUIET") {
|
2023-01-04 14:26:44 -05:00
|
|
|
let mut stdout = StandardStream::stdout(color);
|
|
|
|
stdout.set_color(ColorSpec::new().set_fg(Some(Color::Green)))?;
|
2022-12-26 23:44:53 -05:00
|
|
|
match index {
|
2023-01-04 14:26:44 -05:00
|
|
|
0 => writeln!(stdout, "===> {file} <==="),
|
|
|
|
_ => writeln!(stdout, "\n===> {file} <==="),
|
|
|
|
}?;
|
|
|
|
stdout.reset()?;
|
2022-12-26 23:44:53 -05:00
|
|
|
} else if index > 0 {
|
|
|
|
println!();
|
|
|
|
}
|
2023-01-04 15:14:16 -05:00
|
|
|
let contents = get_contents(&file)?;
|
2022-12-26 23:44:53 -05:00
|
|
|
if matches.get_flag("DECODE") {
|
2023-01-04 15:14:16 -05:00
|
|
|
decode_base32(contents, matches.get_flag("IGNORE"))?;
|
2022-12-26 23:44:53 -05:00
|
|
|
} else {
|
|
|
|
encode_base32(
|
|
|
|
&contents,
|
|
|
|
match matches.get_one::<usize>("WRAP") {
|
|
|
|
Some(c) => *c,
|
|
|
|
None => {
|
|
|
|
return Err(io::Error::new(io::ErrorKind::Other, "Invalid wrap").into())
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn path(&self) -> Option<crate::Path> {
|
|
|
|
self.path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-04 15:14:16 -05:00
|
|
|
fn decode_base32(mut contents: String, ignore: bool) -> Result<(), Box<dyn Error>> {
|
2022-12-26 23:44:53 -05:00
|
|
|
if ignore {
|
|
|
|
contents.retain(|c| !c.is_whitespace());
|
|
|
|
} else {
|
|
|
|
contents = contents.replace('\n', "");
|
|
|
|
}
|
2023-01-04 15:14:16 -05:00
|
|
|
let decoded = BASE32.decode(contents.as_bytes())?;
|
|
|
|
let output = String::from_utf8(decoded)?;
|
2023-01-06 23:41:02 -05:00
|
|
|
println!("{}\n", output.trim_end());
|
2023-01-04 15:14:16 -05:00
|
|
|
Ok(())
|
2022-12-26 23:44:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn encode_base32(contents: &str, wrap: usize) {
|
2023-01-04 15:14:16 -05:00
|
|
|
BASE32
|
2022-12-26 23:44:53 -05:00
|
|
|
.encode(contents.as_bytes())
|
|
|
|
.chars()
|
|
|
|
.collect::<Vec<char>>()
|
|
|
|
.chunks(wrap)
|
|
|
|
.map(|c| c.iter().collect::<String>())
|
2023-01-04 15:14:16 -05:00
|
|
|
.for_each(|line| println!("{line}"));
|
2022-12-26 23:44:53 -05:00
|
|
|
}
|
|
|
|
|
2023-01-04 15:14:16 -05:00
|
|
|
fn get_contents(file: &str) -> Result<String, Box<dyn Error>> {
|
2022-12-26 23:44:53 -05:00
|
|
|
let mut contents = String::new();
|
|
|
|
if file == "-" {
|
2023-01-04 15:14:16 -05:00
|
|
|
io::stdin().read_to_string(&mut contents)?;
|
2022-12-26 23:44:53 -05:00
|
|
|
} else {
|
2023-01-04 15:14:16 -05:00
|
|
|
contents = fs::read_to_string(file)?;
|
2022-12-26 23:44:53 -05:00
|
|
|
}
|
2023-01-04 15:14:16 -05:00
|
|
|
Ok(contents)
|
2022-12-26 23:44:53 -05:00
|
|
|
}
|