use clap::Parser;
use colored::*;
use std::path::PathBuf;
use std::process;
use maskit::{error::MaskitError, MaskitProcessor};
#[derive(Parser)]
#[command(
name = "maskit",
version,
about = "A CLI tool to mask sensitive fields in configuration files",
long_about = "Maskit helps you safely share configuration files by masking sensitive fields like API keys and secrets"
)]
struct Cli {
input: PathBuf,
#[arg(short, long)]
output: Option<PathBuf>,
#[arg(
short,
long,
value_delimiter = ',',
default_value = "key,secret,password,token,credential,auth,private,cert"
)]
keywords: Vec<String>,
#[arg(short, long)]
silent: bool,
}
fn main() {
let args = Cli::parse();
let processor = MaskitProcessor::new(args.input, args.output, args.keywords);
if let Err(e) = processor.process(args.silent) {
if !args.silent {
print_error(&e);
}
process::exit(1);
}
}
fn print_error(error: &MaskitError) {
eprintln!("{} {}", "❌ Error:".red().bold(), error);
match error {
MaskitError::FileRead(_) => {
eprintln!(
"{}",
"💡 Please check if the file exists and you have read permissions.".yellow()
);
}
MaskitError::FileWrite(_) => {
eprintln!(
"{}",
"💡 Please check if you have write permissions in the target directory.".yellow()
);
}
MaskitError::UnsupportedFormat(ext) => {
eprintln!("{} JSON, YAML, TOML", "💡 Supported formats:".yellow());
eprintln!("{} {}", " File extension detected:".yellow(), ext);
}
MaskitError::ParseError { format, .. } => {
eprintln!("{} {}", "💡 Please check if your".yellow(), format);
eprintln!("{}", " file is valid and properly formatted.".yellow());
}
_ => {}
}
}