use anyhow::{Context, Result};
use clap::Parser;
use wx_uploader::{Config, WxUploader, cli};
#[tokio::main]
async fn main() -> Result<()> {
if std::env::args().any(|arg| arg == "--help" || arg == "-h") {
cli::print_colored_help();
std::process::exit(0);
}
let args = cli::Args::parse();
if let Err(error_msg) = cli::validate_args(&args) {
eprintln!("Error: {}", error_msg);
std::process::exit(1);
}
cli::init_logging(args.verbose);
cli::display_banner(&args);
let config = Config::from_env()
.context("Failed to load configuration from environment variables")?
.with_verbose(args.verbose);
let uploader = WxUploader::new(config)
.await
.context("Failed to initialize WeChat uploader")?;
if args.clear_cache {
if args.verbose {
println!("Refreshing WeChat access token...");
}
uploader
.refresh_token()
.await
.context("Failed to refresh WeChat token")?;
if !args.verbose {
println!("WeChat access token refreshed");
}
}
if args.path.is_file() {
uploader
.upload_file(&args.path, true)
.await
.context("Failed to upload file")?;
} else if args.path.is_dir() {
uploader
.process_directory(&args.path)
.await
.context("Failed to process directory")?;
} else {
anyhow::bail!("Path must be a file or directory");
}
Ok(())
}