use clap::Parser; use std::{error::Error, path::PathBuf, process::Command}; use tempfile::TempPath; use walkdir::WalkDir; use crate::{ art::{ArtArgs, add_art, is_flac}, convert::{ToAacArgs, convert_to_aac}, }; mod art; mod convert; type Rail = Result<(), Box>; #[derive(Parser, Debug)] #[command(name = "podtools")] #[command(bin_name = "podtools")] enum AllCli { /// Convert FLACs to AAC ToAac(ToAacArgs), /// Apply iPod-safe album art to AAC files Art(ArtArgs), /// FLAC to AAC, Album Art, Import TheWorks(TheWorksArgs), } #[derive(clap::Args, Debug)] #[command(version, about, long_about = None)] pub struct TheWorksArgs { #[arg(long)] cover: PathBuf, path: PathBuf, } fn main() -> Rail { match AllCli::parse() { AllCli::ToAac(to_aac_args) => convert_to_aac(to_aac_args)?, AllCli::Art(args) => add_art(args)?, AllCli::TheWorks(args) => { println!("The works #1: Convert to AAC."); convert_to_aac(ToAacArgs { replace: false, no_metadata: false, recursive: true, path: args.path.clone(), })?; println!("The works #2: Add art."); add_art(ArtArgs { recursive: true, cover: args.cover, path: args.path.clone(), })?; println!("The works #3: Import AACs into iTunes."); let walker = WalkDir::new(&args.path); let files: Result, _> = walker.into_iter().filter_entry(is_flac).collect(); let files = files?; if files.is_empty() { return Err("No files".into()); } for file in files { if file.file_type().is_file() { Command::new("open") .args(["-a", "Music"]) .arg(file.path()) .output()?; } } println!("Job done."); } } Ok(()) } pub fn temp_file(suffix: Option<&str>) -> TempPath { tempfile::Builder::new() .suffix(suffix.unwrap_or("")) .tempfile() .unwrap() .into_temp_path() }