id3-image-rs/src/bin/id3-image-remove.rs

67 lines
1.8 KiB
Rust
Raw Normal View History

2019-02-24 09:24:32 +00:00
use std::io::{self, Write};
use std::path::PathBuf;
2022-03-22 16:50:38 +00:00
use std::process;
2019-02-24 09:24:32 +00:00
2022-03-22 16:56:46 +00:00
use id3_image_rs::remove_images;
2022-03-22 16:50:38 +00:00
use structopt::StructOpt;
2019-02-24 09:24:32 +00:00
#[derive(StructOpt, Debug)]
#[structopt(name = "id3-image-remove")]
struct Opt {
/// Verbose mode (-v, -vv, -vvv, etc.)
#[structopt(short = "v", long = "verbose", parse(from_occurrences))]
2021-02-16 16:43:01 +00:00
verbose: i8,
/// Quiet mode, implies no verbosity, and also no error explanations
#[structopt(short = "q", long = "quiet")]
quiet: bool,
2019-02-24 09:24:32 +00:00
/// Don't ask for confirmation before removing
#[structopt(long = "no-confirm")]
no_confirm: bool,
/// Music file to remove images from
#[structopt(name = "music-file.mp3", required = true, parse(from_os_str))]
music_filename: PathBuf,
}
fn main() {
let opt = Opt::from_args();
2021-02-16 16:43:01 +00:00
let verbosity = if opt.quiet { -1 } else { opt.verbose };
let confirm = !opt.no_confirm;
2019-02-24 09:24:32 +00:00
2021-02-16 16:43:01 +00:00
if verbosity >= 0 && confirm {
2019-02-24 09:24:32 +00:00
print_prompt();
let mut input = String::new();
2021-02-16 16:44:00 +00:00
while io::stdin().read_line(&mut input).is_err() {
2021-02-16 16:43:01 +00:00
if verbosity >= 0 {
eprintln!("Could not read your input, please try again.");
}
2019-02-24 09:24:32 +00:00
print_prompt();
}
let choice = input.to_lowercase().trim().chars().next().unwrap_or('y');
if choice != 'y' {
2021-02-16 16:43:01 +00:00
if verbosity >= 1 {
2019-02-24 09:24:32 +00:00
println!("Exiting without removing images");
}
2021-02-16 16:43:01 +00:00
return;
2019-02-24 09:24:32 +00:00
}
}
if let Err(e) = remove_images(&opt.music_filename) {
2021-02-16 16:43:01 +00:00
if verbosity >= 0 {
eprintln!("{}", e);
}
2019-02-24 09:24:32 +00:00
process::exit(1);
}
2021-02-16 16:43:01 +00:00
if verbosity >= 1 {
2019-02-24 09:24:32 +00:00
println!("Removed images on {:?}", opt.music_filename);
}
}
fn print_prompt() {
print!("Are you sure you'd like to clear all embedded images? [Y/n] ");
io::stdout().flush().unwrap();
}