better error message ( #756), use channels argument for import/export

This commit is contained in:
jb-alvarado 2024-09-22 16:57:42 +02:00
parent f1565b5d32
commit 9e005aab1b
5 changed files with 86 additions and 61 deletions

1
.gitignore vendored
View File

@ -28,3 +28,4 @@ tmp/
assets/playlist_template.json
advanced*.toml
ffplayout*.toml
template.json

View File

@ -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 {

View File

@ -252,10 +252,7 @@ impl AdvancedConfig {
Ok(())
}
pub async fn import(pool: &Pool<Sqlite>, import: Vec<String>) -> Result<(), ServiceError> {
let id = import[0].parse::<i32>()?;
let path = Path::new(&import[1]);
pub async fn import(pool: &Pool<Sqlite>, id: i32, path: &Path) -> Result<(), ServiceError> {
if path.is_file() {
let mut file = tokio::fs::File::open(path).await?;
let mut contents = String::new();

View File

@ -64,24 +64,20 @@ pub struct Args {
long,
help = "Dump advanced channel configuration to advanced_{channel}.toml"
)]
pub dump_advanced: Option<i32>,
pub dump_advanced: bool,
#[clap(long, help = "Dump channel configuration to ffplayout_{channel}.toml")]
pub dump_config: Option<i32>,
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<Vec<String>>,
pub import_advanced: Option<PathBuf>,
#[clap(
long,
help = "import channel configuration from file. Input must be `{channel id} {path to toml}`",
num_args = 2
)]
pub import_config: Option<Vec<String>>,
#[clap(long, help = "import channel configuration from file.", num_args = 2)]
pub import_config: Option<PathBuf>,
#[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<Sqlite>) -> 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,21 +503,10 @@ pub async fn run_args(pool: &Pool<Sqlite>) -> 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;
}
Err(e) => {
eprintln!("Dump config: {e}");
error_code = 1;
}
};
}
if let Some(id) = ARGS.dump_advanced {
match AdvancedConfig::dump(pool, id).await {
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;
@ -532,9 +517,36 @@ pub async fn run_args(pool: &Pool<Sqlite>) -> Result<(), i32> {
}
};
}
} 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 {
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;
}
};
}
} else {
eprintln!("Channel ID(s) needed! Use `--channels 1 ...`");
error_code = 1;
}
}
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;
@ -545,9 +557,16 @@ pub async fn run_args(pool: &Pool<Sqlite>) -> Result<(), i32> {
}
};
}
} 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 {
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;
@ -558,6 +577,11 @@ pub async fn run_args(pool: &Pool<Sqlite>) -> Result<(), i32> {
}
};
}
} else {
eprintln!("Channel ID(s) needed! Use `--channels 1 ...`");
error_code = 1;
}
}
if error_code > -1 {
Err(error_code)

View File

@ -756,10 +756,7 @@ impl PlayoutConfig {
Ok(())
}
pub async fn import(pool: &Pool<Sqlite>, import: Vec<String>) -> Result<(), ServiceError> {
let id = import[0].parse::<i32>()?;
let path = Path::new(&import[1]);
pub async fn import(pool: &Pool<Sqlite>, id: i32, path: &Path) -> Result<(), ServiceError> {
if path.is_file() {
let mut file = tokio::fs::File::open(path).await?;
let mut contents = String::new();