From 9e005aab1b2e5c09012afc82f60dd9b684cf69c5 Mon Sep 17 00:00:00 2001 From: jb-alvarado Date: Sun, 22 Sep 2024 16:57:42 +0200 Subject: [PATCH] better error message ( #756), use channels argument for import/export --- .gitignore | 1 + ffplayout/src/main.rs | 10 +- ffplayout/src/utils/advanced_config.rs | 5 +- ffplayout/src/utils/args_parse.rs | 126 +++++++++++++++---------- ffplayout/src/utils/config.rs | 5 +- 5 files changed, 86 insertions(+), 61 deletions(-) diff --git a/.gitignore b/.gitignore index 6aa3fdbb..5dd690c4 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,4 @@ tmp/ assets/playlist_template.json advanced*.toml ffplayout*.toml +template.json diff --git a/ffplayout/src/main.rs b/ffplayout/src/main.rs index 50f13979..bcfc712c 100644 --- a/ffplayout/src/main.rs +++ b/ffplayout/src/main.rs @@ -197,10 +197,16 @@ async fn main() -> std::io::Result<()> { let channels = ARGS.channels.clone().unwrap_or_else(|| vec![1]); for (index, channel_id) in channels.iter().enumerate() { - let config = get_config(&pool, *channel_id) + let config = match get_config(&pool, *channel_id).await { + Ok(c) => c, + Err(e) => { + eprint!("No config found, channel may not exists!\nOriginal error message: "); + return Err(io::Error::new(io::ErrorKind::Other, e.to_string())); + } + }; + let channel = handles::select_channel(&pool, channel_id) .await .map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))?; - let channel = handles::select_channel(&pool, channel_id).await.unwrap(); let manager = ChannelManager::new(Some(pool.clone()), channel.clone(), config.clone()); if ARGS.foreground { diff --git a/ffplayout/src/utils/advanced_config.rs b/ffplayout/src/utils/advanced_config.rs index b23bb0ce..9301486f 100644 --- a/ffplayout/src/utils/advanced_config.rs +++ b/ffplayout/src/utils/advanced_config.rs @@ -252,10 +252,7 @@ impl AdvancedConfig { Ok(()) } - pub async fn import(pool: &Pool, import: Vec) -> Result<(), ServiceError> { - let id = import[0].parse::()?; - let path = Path::new(&import[1]); - + pub async fn import(pool: &Pool, id: i32, path: &Path) -> Result<(), ServiceError> { if path.is_file() { let mut file = tokio::fs::File::open(path).await?; let mut contents = String::new(); diff --git a/ffplayout/src/utils/args_parse.rs b/ffplayout/src/utils/args_parse.rs index 1707be3b..5d7d7908 100644 --- a/ffplayout/src/utils/args_parse.rs +++ b/ffplayout/src/utils/args_parse.rs @@ -64,24 +64,20 @@ pub struct Args { long, help = "Dump advanced channel configuration to advanced_{channel}.toml" )] - pub dump_advanced: Option, + pub dump_advanced: bool, #[clap(long, help = "Dump channel configuration to ffplayout_{channel}.toml")] - pub dump_config: Option, + pub dump_config: bool, #[clap( long, - help = "import advanced channel configuration from file. Input must be `{channel id} {path to toml}`", + help = "import advanced channel configuration from file.", num_args = 2 )] - pub import_advanced: Option>, + pub import_advanced: Option, - #[clap( - long, - help = "import channel configuration from file. Input must be `{channel id} {path to toml}`", - num_args = 2 - )] - pub import_config: Option>, + #[clap(long, help = "import channel configuration from file.", num_args = 2)] + pub import_config: Option, #[clap(long, help = "List available channel ids")] pub list_channels: bool, @@ -203,7 +199,7 @@ fn global_user(args: &mut Args) { pub async fn run_args(pool: &Pool) -> Result<(), i32> { let mut args = ARGS.clone(); - if args.dump_advanced.is_none() && args.dump_config.is_none() && !args.drop_db { + if !args.dump_advanced && !args.dump_config && !args.drop_db { if let Err(e) = handles::db_migrate(pool).await { panic!("{e}"); }; @@ -507,56 +503,84 @@ pub async fn run_args(pool: &Pool) -> Result<(), i32> { error_code = 0; } - if let Some(id) = ARGS.dump_config { - match PlayoutConfig::dump(pool, id).await { - Ok(_) => { - println!("Dump config to: ffplayout_{id}.toml"); - error_code = 0; + if ARGS.dump_advanced { + if let Some(channels) = &ARGS.channels { + for id in channels { + match AdvancedConfig::dump(pool, *id).await { + Ok(_) => { + println!("Dump config to: advanced_{id}.toml"); + error_code = 0; + } + Err(e) => { + eprintln!("Dump config: {e}"); + error_code = 1; + } + }; } - Err(e) => { - eprintln!("Dump config: {e}"); - error_code = 1; - } - }; + } else { + eprintln!("Channel ID(s) needed! Use `--channels 1 ...`"); + error_code = 1; + } } - if let Some(id) = ARGS.dump_advanced { - match AdvancedConfig::dump(pool, id).await { - Ok(_) => { - println!("Dump config to: advanced_{id}.toml"); - error_code = 0; + if ARGS.dump_config { + if let Some(channels) = &ARGS.channels { + for id in channels { + match PlayoutConfig::dump(pool, *id).await { + Ok(_) => { + println!("Dump config to: ffplayout_{id}.toml"); + error_code = 0; + } + Err(e) => { + eprintln!("Dump config: {e}"); + error_code = 1; + } + }; } - Err(e) => { - eprintln!("Dump config: {e}"); - error_code = 1; - } - }; + } else { + eprintln!("Channel ID(s) needed! Use `--channels 1 ...`"); + error_code = 1; + } } - if let Some(import) = &ARGS.import_config { - match PlayoutConfig::import(pool, import.clone()).await { - Ok(_) => { - println!("Import config done..."); - error_code = 0; + if let Some(path) = &ARGS.import_advanced { + if let Some(channels) = &ARGS.channels { + for id in channels { + match AdvancedConfig::import(pool, *id, path).await { + Ok(_) => { + println!("Import config done..."); + error_code = 0; + } + Err(e) => { + eprintln!("{e}"); + error_code = 1; + } + }; } - Err(e) => { - eprintln!("{e}"); - error_code = 1; - } - }; + } else { + eprintln!("Channel ID(s) needed! Use `--channels 1 ...`"); + error_code = 1; + } } - if let Some(import) = &ARGS.import_advanced { - match AdvancedConfig::import(pool, import.clone()).await { - Ok(_) => { - println!("Import config done..."); - error_code = 0; + if let Some(path) = &ARGS.import_config { + if let Some(channels) = &ARGS.channels { + for id in channels { + match PlayoutConfig::import(pool, *id, path).await { + Ok(_) => { + println!("Import config done..."); + error_code = 0; + } + Err(e) => { + eprintln!("{e}"); + error_code = 1; + } + }; } - Err(e) => { - eprintln!("{e}"); - error_code = 1; - } - }; + } else { + eprintln!("Channel ID(s) needed! Use `--channels 1 ...`"); + error_code = 1; + } } if error_code > -1 { diff --git a/ffplayout/src/utils/config.rs b/ffplayout/src/utils/config.rs index 33269699..03e21427 100644 --- a/ffplayout/src/utils/config.rs +++ b/ffplayout/src/utils/config.rs @@ -756,10 +756,7 @@ impl PlayoutConfig { Ok(()) } - pub async fn import(pool: &Pool, import: Vec) -> Result<(), ServiceError> { - let id = import[0].parse::()?; - let path = Path::new(&import[1]); - + pub async fn import(pool: &Pool, id: i32, path: &Path) -> Result<(), ServiceError> { if path.is_file() { let mut file = tokio::fs::File::open(path).await?; let mut contents = String::new();