refactor: 引入 clap 处理环境变量和命令行参数 (#119)

This commit is contained in:
ᴀᴍᴛᴏᴀᴇʀ
2024-06-08 10:57:08 +08:00
committed by GitHub
parent 1744f8647b
commit 4818e62414
7 changed files with 77 additions and 59 deletions
Generated
+23 -22
View File
@@ -426,6 +426,7 @@ dependencies = [
"bili_sync_entity", "bili_sync_entity",
"bili_sync_migration", "bili_sync_migration",
"chrono", "chrono",
"clap",
"cookie 0.18.1", "cookie 0.18.1",
"dirs", "dirs",
"filenamify", "filenamify",
@@ -606,9 +607,9 @@ checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e"
[[package]] [[package]]
name = "chrono" name = "chrono"
version = "0.4.37" version = "0.4.38"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a0d04d43504c61aa6c7531f1871dd0d418d91130162063b789da00fd7057a5e" checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401"
dependencies = [ dependencies = [
"android-tzdata", "android-tzdata",
"iana-time-zone", "iana-time-zone",
@@ -621,9 +622,9 @@ dependencies = [
[[package]] [[package]]
name = "clap" name = "clap"
version = "4.5.4" version = "4.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90bc066a67923782aa8515dbaea16946c5bcc5addbd668bb80af688e53e548a0" checksum = "a9689a29b593160de5bc4aacab7b5d54fb52231de70122626c178e6a368994c7"
dependencies = [ dependencies = [
"clap_builder", "clap_builder",
"clap_derive", "clap_derive",
@@ -631,9 +632,9 @@ dependencies = [
[[package]] [[package]]
name = "clap_builder" name = "clap_builder"
version = "4.5.2" version = "4.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4" checksum = "2e5387378c84f6faa26890ebf9f0a92989f8873d4d380467bcd0d8d8620424df"
dependencies = [ dependencies = [
"anstream", "anstream",
"anstyle", "anstyle",
@@ -643,9 +644,9 @@ dependencies = [
[[package]] [[package]]
name = "clap_derive" name = "clap_derive"
version = "4.5.4" version = "4.5.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "528131438037fd55894f62d6e9f068b8f45ac57ffa77517819645d10aed04f64" checksum = "c780290ccf4fb26629baa7a1081e68ced113f1d3ec302fa5948f1c381ebf06c6"
dependencies = [ dependencies = [
"heck 0.5.0", "heck 0.5.0",
"proc-macro2", "proc-macro2",
@@ -2681,18 +2682,18 @@ checksum = "1c107b6f4780854c8b126e228ea8869f4d7b71260f962fefb57b996b8959ba6b"
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.197" version = "1.0.203"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fb1c873e1b9b056a4dc4c0c198b24c3ffa059243875552b2bd0933b1aee4ce2" checksum = "7253ab4de971e72fb7be983802300c30b5a7f0c2e56fab8abfc6a214307c0094"
dependencies = [ dependencies = [
"serde_derive", "serde_derive",
] ]
[[package]] [[package]]
name = "serde_derive" name = "serde_derive"
version = "1.0.197" version = "1.0.203"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" checksum = "500cbc0ebeb6f46627f50f3f5811ccf6bf00643be300b4c3eabc0ef55dc5b5ba"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@@ -2701,9 +2702,9 @@ dependencies = [
[[package]] [[package]]
name = "serde_json" name = "serde_json"
version = "1.0.115" version = "1.0.117"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12dc5c46daa8e9fdf4f5e71b6cf9a53f2487da0e86e55808e2d35539666497dd" checksum = "455182ea6142b14f93f4bc5320a2b31c1f266b66a4a5c858b013302a5d8cbfc3"
dependencies = [ dependencies = [
"itoa", "itoa",
"ryu", "ryu",
@@ -3191,18 +3192,18 @@ dependencies = [
[[package]] [[package]]
name = "thiserror" name = "thiserror"
version = "1.0.58" version = "1.0.61"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03468839009160513471e86a034bb2c5c0e4baae3b43f79ffc55c4a5427b3297" checksum = "c546c80d6be4bc6a00c0f01730c08df82eaa7a7a61f11d656526506112cc1709"
dependencies = [ dependencies = [
"thiserror-impl", "thiserror-impl",
] ]
[[package]] [[package]]
name = "thiserror-impl" name = "thiserror-impl"
version = "1.0.58" version = "1.0.61"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c61f3ba182994efc43764a46c018c347bc492c79f024e705f46567b418f6d4f7" checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@@ -3267,9 +3268,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
[[package]] [[package]]
name = "tokio" name = "tokio"
version = "1.37.0" version = "1.38.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1adbebffeca75fcfd058afa480fb6c0b81e165a0323f9c9d39c9697e37c46787" checksum = "ba4f4a02a7a80d6f274636f0aa95c7e383b912d41fe721a31f29e29698585a4a"
dependencies = [ dependencies = [
"backtrace", "backtrace",
"bytes", "bytes",
@@ -3286,9 +3287,9 @@ dependencies = [
[[package]] [[package]]
name = "tokio-macros" name = "tokio-macros"
version = "2.2.0" version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" checksum = "5f5ae998a069d4b5aba8ee9dad856af7d520c3699e6159b185c2acd48155d39a"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
+19 -18
View File
@@ -15,12 +15,13 @@ publish = false
bili_sync_entity = { path = "crates/bili_sync_entity" } bili_sync_entity = { path = "crates/bili_sync_entity" }
bili_sync_migration = { path = "crates/bili_sync_migration" } bili_sync_migration = { path = "crates/bili_sync_migration" }
anyhow = { version = "1.0.81", features = ["backtrace"] } anyhow = { version = "1.0.86", features = ["backtrace"] }
arc-swap = { version = "1.7", features = ["serde"] } arc-swap = { version = "1.7.1", features = ["serde"] }
async-std = { version = "1", features = ["attributes", "tokio1"] } async-std = { version = "1.12.0", features = ["attributes", "tokio1"] }
async-stream = "0.3.5" async-stream = "0.3.5"
chrono = { version = "0.4.35", features = ["serde"] } chrono = { version = "0.4.38", features = ["serde"] }
cookie = "0.18.0" clap = { version = "4.5.6", features = ["env"] }
cookie = "0.18.1"
dirs = "5.0.1" dirs = "5.0.1"
filenamify = "0.1.0" filenamify = "0.1.0"
float-ord = "0.3.2" float-ord = "0.3.2"
@@ -28,12 +29,12 @@ futures = "0.3.30"
handlebars = "5.1.2" handlebars = "5.1.2"
hex = "0.4.3" hex = "0.4.3"
log = "0.4.21" log = "0.4.21"
memchr = "2.5.0" memchr = "2.7.2"
once_cell = "1.19.0" once_cell = "1.19.0"
prost = "0.12.4" prost = "0.12.6"
quick-xml = { version = "0.31.0", features = ["async-tokio"] } quick-xml = { version = "0.31.0", features = ["async-tokio"] }
rand = "0.8.5" rand = "0.8.5"
regex = "1.10.3" regex = "1.10.4"
reqwest = { version = "0.12.4", features = [ reqwest = { version = "0.12.4", features = [
"charset", "charset",
"cookies", "cookies",
@@ -44,20 +45,20 @@ reqwest = { version = "0.12.4", features = [
"stream", "stream",
], default-features = false } ], default-features = false }
rsa = { version = "0.9.6", features = ["sha2"] } rsa = { version = "0.9.6", features = ["sha2"] }
sea-orm = { version = "0.12", features = [ sea-orm = { version = "0.12.15", features = [
"macros", "macros",
"runtime-tokio-rustls", "runtime-tokio-rustls",
"sqlx-sqlite", "sqlx-sqlite",
] } ] }
sea-orm-migration = { version = "0.12.0", features = [] } sea-orm-migration = { version = "0.12.15", features = [] }
serde = { version = "1.0.197", features = ["derive"] } serde = { version = "1.0.203", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0.117"
strum = { version = "0.26", features = ["derive"] } strum = { version = "0.26.2", features = ["derive"] }
thiserror = "1.0.58" thiserror = "1.0.61"
tokio = { version = "1", features = ["full"] } tokio = { version = "1.38.0", features = ["full"] }
toml = "0.8.12" toml = "0.8.14"
tracing = "0.1" tracing = "0.1.40"
tracing-subscriber = { version = "0.3", features = ["chrono"] } tracing-subscriber = { version = "0.3.18", features = ["chrono"] }
[profile.release] [profile.release]
strip = true strip = true
+1
View File
@@ -15,6 +15,7 @@ async-stream = { workspace = true }
bili_sync_entity = { workspace = true } bili_sync_entity = { workspace = true }
bili_sync_migration = { workspace = true } bili_sync_migration = { workspace = true }
chrono = { workspace = true } chrono = { workspace = true }
clap = { workspace = true }
cookie = { workspace = true } cookie = { workspace = true }
dirs = { workspace = true } dirs = { workspace = true }
filenamify = { workspace = true } filenamify = { workspace = true }
+14
View File
@@ -30,6 +30,8 @@ pub static CONFIG: Lazy<Config> = Lazy::new(|| {
config config
}); });
pub static ARGS: Lazy<Args> = Lazy::new(Args::parse);
pub static CONFIG_DIR: Lazy<PathBuf> = pub static CONFIG_DIR: Lazy<PathBuf> =
Lazy::new(|| dirs::config_dir().expect("No config path found").join("bili-sync")); Lazy::new(|| dirs::config_dir().expect("No config path found").join("bili-sync"));
@@ -138,3 +140,15 @@ impl Config {
Ok(()) Ok(())
} }
} }
use clap::Parser;
#[derive(Parser)]
#[command(version, about, long_about = None)]
pub struct Args {
#[arg(short, long, env = "SCAN_ONLY")]
pub scan_only: bool,
#[arg(short, long, default_value = "None,bili_sync=info", env = "RUST_LOG")]
pub log_level: String,
}
+2 -6
View File
@@ -1,5 +1,4 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::env::{args, var};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::pin::Pin; use std::pin::Pin;
@@ -8,7 +7,6 @@ use bili_sync_entity::{favorite, page, video};
use filenamify::filenamify; use filenamify::filenamify;
use futures::stream::{FuturesOrdered, FuturesUnordered}; use futures::stream::{FuturesOrdered, FuturesUnordered};
use futures::{pin_mut, Future, StreamExt}; use futures::{pin_mut, Future, StreamExt};
use once_cell::sync::Lazy;
use sea_orm::entity::prelude::*; use sea_orm::entity::prelude::*;
use sea_orm::ActiveValue::Set; use sea_orm::ActiveValue::Set;
use sea_orm::TransactionTrait; use sea_orm::TransactionTrait;
@@ -17,7 +15,7 @@ use tokio::fs;
use tokio::sync::{Mutex, Semaphore}; use tokio::sync::{Mutex, Semaphore};
use crate::bilibili::{BestStream, BiliClient, BiliError, Dimension, FavoriteList, PageInfo, Video}; use crate::bilibili::{BestStream, BiliClient, BiliError, Dimension, FavoriteList, PageInfo, Video};
use crate::config::CONFIG; use crate::config::{ARGS, CONFIG};
use crate::core::status::{PageStatus, VideoStatus}; use crate::core::status::{PageStatus, VideoStatus};
use crate::core::utils::{ use crate::core::utils::{
create_video_pages, create_videos, exist_labels, filter_unfilled_videos, handle_favorite_info, total_video_count, create_video_pages, create_videos, exist_labels, filter_unfilled_videos, handle_favorite_info, total_video_count,
@@ -26,8 +24,6 @@ use crate::core::utils::{
use crate::downloader::Downloader; use crate::downloader::Downloader;
use crate::error::{DownloadAbortError, ProcessPageError}; use crate::error::{DownloadAbortError, ProcessPageError};
pub static SCAN_ONLY: Lazy<bool> = Lazy::new(|| var("SCAN_ONLY").is_ok() || args().any(|arg| arg == "--scan-only"));
/// 处理某个收藏夹,首先刷新收藏夹信息,然后下载收藏夹中未下载成功的视频 /// 处理某个收藏夹,首先刷新收藏夹信息,然后下载收藏夹中未下载成功的视频
pub async fn process_favorite_list( pub async fn process_favorite_list(
bili_client: &BiliClient, bili_client: &BiliClient,
@@ -37,7 +33,7 @@ pub async fn process_favorite_list(
) -> Result<()> { ) -> Result<()> {
let favorite_model = refresh_favorite_list(bili_client, fid, path, connection).await?; let favorite_model = refresh_favorite_list(bili_client, fid, path, connection).await?;
let favorite_model = fetch_video_details(bili_client, favorite_model, connection).await?; let favorite_model = fetch_video_details(bili_client, favorite_model, connection).await?;
if *SCAN_ONLY { if ARGS.scan_only {
warn!("已开启仅扫描模式,跳过视频下载..."); warn!("已开启仅扫描模式,跳过视频下载...");
return Ok(()); return Ok(());
} }
+12
View File
@@ -15,6 +15,7 @@ use sea_orm::ActiveValue::Set;
use sea_orm::QuerySelect; use sea_orm::QuerySelect;
use serde_json::json; use serde_json::json;
use tokio::io::AsyncWriteExt; use tokio::io::AsyncWriteExt;
use tracing_subscriber::util::SubscriberInitExt;
use crate::bilibili::{FavoriteListInfo, PageInfo, VideoInfo}; use crate::bilibili::{FavoriteListInfo, PageInfo, VideoInfo};
use crate::config::{NFOTimeType, CONFIG}; use crate::config::{NFOTimeType, CONFIG};
@@ -482,6 +483,17 @@ impl<'a> NFOSerializer<'a> {
} }
} }
pub fn init_logger(log_level: &str) {
tracing_subscriber::fmt::Subscriber::builder()
.with_env_filter(tracing_subscriber::EnvFilter::builder().parse_lossy(log_level))
.with_timer(tracing_subscriber::fmt::time::ChronoLocal::new(
"%Y-%m-%d %H:%M:%S%.3f".to_owned(),
))
.finish()
.try_init()
.expect("初始化日志失败");
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
+6 -13
View File
@@ -10,27 +10,20 @@ mod error;
use std::time::Duration; use std::time::Duration;
use config::ARGS;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use tokio::time; use tokio::time;
use tracing_subscriber::util::SubscriberInitExt;
use crate::bilibili::BiliClient; use crate::bilibili::BiliClient;
use crate::config::CONFIG; use crate::config::CONFIG;
use crate::core::command::{process_favorite_list, SCAN_ONLY}; use crate::core::command::process_favorite_list;
use crate::core::utils::init_logger;
use crate::database::{database_connection, migrate_database}; use crate::database::{database_connection, migrate_database};
#[tokio::main] #[tokio::main]
async fn main() -> ! { async fn main() {
let default_log_level = std::env::var("RUST_LOG").unwrap_or("None,bili_sync=info".to_owned()); Lazy::force(&ARGS);
tracing_subscriber::fmt::Subscriber::builder() init_logger(&ARGS.log_level);
.with_env_filter(tracing_subscriber::EnvFilter::builder().parse_lossy(default_log_level))
.with_timer(tracing_subscriber::fmt::time::ChronoLocal::new(
"%Y-%m-%d %H:%M:%S%.3f".to_owned(),
))
.finish()
.try_init()
.expect("初始化日志失败");
Lazy::force(&SCAN_ONLY);
Lazy::force(&CONFIG); Lazy::force(&CONFIG);
let mut anchor = chrono::Local::now().date_naive(); let mut anchor = chrono::Local::now().date_naive();
let bili_client = BiliClient::new(); let bili_client = BiliClient::new();