Skip to content

Commit

Permalink
fixup! editoast: add cli commands to list / delete electrical profile…
Browse files Browse the repository at this point in the history
… sets
  • Loading branch information
Baptiste Prevot committed Oct 2, 2023
1 parent b197f61 commit 2f8e1fe
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
18 changes: 12 additions & 6 deletions editoast/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ pub enum Commands {
long_about = "Commands related to electrical profile sets"
)]
ElectricalProfiles(ElectricalProfilesCommands),
ImportProfileSet(ImportProfileSetArgs),
ImportRollingStock(ImportRollingStockArgs),
OsmToRailjson(OsmToRailjsonArgs),
#[command(about, long_about = "Prints the OpenApi of the service")]
Expand All @@ -52,11 +51,7 @@ pub enum Commands {
pub enum ElectricalProfilesCommands {
Import(ImportProfileSetArgs),
Delete(DeleteProfileSetArgs),
#[command(
about,
long_about = "List electrical profile sets present in the database"
)]
List,
List(ListProfileSetArgs),
}

#[derive(Args, Debug, Derivative, Clone)]
Expand Down Expand Up @@ -145,6 +140,17 @@ pub struct DeleteProfileSetArgs {
pub profile_set_ids: Vec<i64>,
}

#[derive(Args, Debug)]
#[command(
about,
long_about = "List electrical profile sets in the database, <id> - <name>"
)]
pub struct ListProfileSetArgs {
// Wether to display the list in a ready to parse format
#[arg(long, default_value_t = false)]
pub quiet: bool,
}

#[derive(Args, Debug)]
#[command(about, long_about = "Import a rolling stock given a json file")]
pub struct ImportRollingStockArgs {
Expand Down
33 changes: 21 additions & 12 deletions editoast/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ use chashmap::CHashMap;
use clap::Parser;
use client::{
ClearArgs, Client, Color, Commands, DeleteProfileSetArgs, ElectricalProfilesCommands,
GenerateArgs, ImportProfileSetArgs, ImportRailjsonArgs, ImportRollingStockArgs, PostgresConfig,
RedisConfig, RunserverArgs,
GenerateArgs, ImportProfileSetArgs, ImportRailjsonArgs, ImportRollingStockArgs,
ListProfileSetArgs, PostgresConfig, RedisConfig, RunserverArgs,
};
use colored::*;
use diesel::{ConnectionError, ConnectionResult};
Expand Down Expand Up @@ -89,7 +89,6 @@ async fn run() -> Result<(), Box<dyn Error + Send + Sync>> {
Commands::Generate(args) => generate(args, pg_config, redis_config).await,
Commands::Clear(args) => clear(args, pg_config, redis_config).await,
Commands::ImportRailjson(args) => import_railjson(args, pg_config).await,
Commands::ImportProfileSet(args) => electrical_profile_set_import(args, pg_config).await,
Commands::ImportRollingStock(args) => import_rolling_stock(args, pg_config).await,
Commands::OsmToRailjson(args) => {
converters::osm_to_railjson(args.osm_pbf_in, args.railjson_out)
Expand All @@ -102,7 +101,9 @@ async fn run() -> Result<(), Box<dyn Error + Send + Sync>> {
ElectricalProfilesCommands::Import(args) => {
electrical_profile_set_import(args, pg_config).await
}
ElectricalProfilesCommands::List => electrical_profile_set_list(pg_config).await,
ElectricalProfilesCommands::List(args) => {
electrical_profile_set_list(args, pg_config).await
}
ElectricalProfilesCommands::Delete(args) => {
electrical_profile_set_delete(args, pg_config).await
}
Expand Down Expand Up @@ -423,11 +424,14 @@ async fn electrical_profile_set_import(
}

async fn electrical_profile_set_list(
args: ListProfileSetArgs,
pg_config: PostgresConfig,
) -> Result<(), Box<dyn Error + Send + Sync>> {
let mut conn = PgConnection::establish(&pg_config.url()).await?;
let electrical_profile_sets = ElectricalProfileSet::list_light(&mut conn).await.unwrap();
println!("Electrical profile sets:\nID - Name");
if !args.quiet {
println!("Electrical profile sets:\nID - Name");
}
for electrical_profile_set in electrical_profile_sets {
println!(
"{:<2} - {}",
Expand All @@ -445,10 +449,14 @@ async fn electrical_profile_set_delete(
let manager = ConnectionManager::<PgConnection>::new(pg_config.url());
let pool = Data::new(Pool::builder(manager).max_size(1).build().unwrap());
for profile_set_id in args.profile_set_ids {
ElectricalProfileSet::delete(pool.clone(), profile_set_id)
let deleted = ElectricalProfileSet::delete(pool.clone(), profile_set_id)
.await
.unwrap();
println!("Electrical profile set {} deleted", profile_set_id);
if !deleted {
println!("Electrical profile set {} not found", profile_set_id);
} else {
println!("Electrical profile set {} deleted", profile_set_id);
}
}
Ok(())
}
Expand Down Expand Up @@ -621,11 +629,12 @@ mod tests {
async fn test_electrical_profile_set_list_doesnt_fail(
#[future] electrical_profile_set: TestFixture<ElectricalProfileSet>,
) {
let pg_config = PostgresConfig::default();
let _electrical_profile_set = electrical_profile_set.await;

electrical_profile_set_list(pg_config.clone())
.await
.unwrap();
for quiet in vec![true, false] {
let args = ListProfileSetArgs { quiet };
electrical_profile_set_list(args, PostgresConfig::default())
.await
.unwrap();
}
}
}

0 comments on commit 2f8e1fe

Please sign in to comment.