use clap::{crate_version, Clap};
use plm_rs::{config, establish_connection, prompt, tables::*, Application};
use std::io;
#[derive(Clap)]
#[clap(version = crate_version!())]
struct Opts {
#[clap(subcommand)]
subcmd: SubCommand,
#[clap(short, long)]
config_path: Option<String>,
}
#[derive(Clap)]
#[clap(version = crate_version!())]
enum SubCommand {
Install(Install),
Parts(Parts),
Build(Build),
Inventory(Inventory),
Bom(Bom),
}
#[derive(Clap)]
struct Install {}
#[derive(Clap)]
struct Bom {
#[clap(subcommand)]
subcmd: BomSubCommand,
}
#[derive(Clap)]
#[clap(version = crate_version!())]
enum BomSubCommand {
Import(ImportBom),
Show(ShowBom),
Export(ExportBom),
}
#[derive(Clap)]
struct ExportBom {
name: String,
#[clap(short, long)]
version: Option<i32>,
}
#[derive(Clap)]
struct ImportBom {
filename: String,
}
#[derive(Clap)]
struct ShowBom {
part_number: String,
#[clap(short, long)]
version: Option<i32>,
}
#[derive(Clap)]
struct Parts {
#[clap(subcommand)]
subcmd: PartsSubCommand,
}
#[derive(Clap)]
#[clap(version = crate_version!())]
enum PartsSubCommand {
Create(CreateParts),
Delete(DeleteParts),
Show(ShowParts),
Rename(RenamePart),
}
#[derive(Clap)]
struct CreateParts {
#[clap(short, long)]
filename: Option<String>,
}
#[derive(Clap)]
struct DeleteParts {}
#[derive(Clap)]
struct ShowParts {}
#[derive(Clap)]
struct RenamePart {}
#[derive(Clap)]
#[clap(version = crate_version!())]
enum BuildSubCommand {
Create(CreateBuild),
Delete(DeleteBuild),
Show(ShowBuilds),
Complete(CompleteBuild),
Export(ExportBuild),
}
#[derive(Clap)]
struct CreateBuild {}
#[derive(Clap)]
struct DeleteBuild {
build_id: i32,
}
#[derive(Clap)]
struct ShowBuilds {
#[clap(short, long)]
all: bool,
}
#[derive(Clap)]
struct CompleteBuild {
build_id: i32,
}
#[derive(Clap)]
struct ExportBuild {
build_id: i32,
}
#[derive(Clap)]
struct Build {
#[clap(subcommand)]
subcmd: BuildSubCommand,
}
#[derive(Clap)]
struct Inventory {
#[clap(subcommand)]
subcmd: InventorySubCommand,
}
#[derive(Clap)]
#[clap(version = crate_version!())]
enum InventorySubCommand {
Create(CreateInventory),
Import(ImportInventory),
Update(UpdateInventory),
Export(ExportInventory),
Shortages(ExportInventoryShortages),
Delete(DeleteInventory),
Show(ShowInventory),
}
#[derive(Clap)]
struct CreateInventory {}
#[derive(Clap)]
struct ImportInventory {
filename: String,
}
#[derive(Clap)]
struct UpdateInventory {
filename: String,
}
#[derive(Clap)]
struct ExportInventory {
#[clap(short, long)]
export_all: bool,
filename: String,
}
#[derive(Clap)]
struct ExportInventoryShortages {
filename: String,
}
#[derive(Clap)]
struct DeleteInventory {}
#[derive(Clap)]
struct ShowInventory {
#[clap(short, long)]
show_shortage: bool,
#[clap(short, long)]
all_entries: bool,
}
fn main() {
let opts: Opts = Opts::parse();
let config_path = match config::get_config_path(&opts.config_path) {
Ok(c) => c,
Err(e) => {
eprintln!("Unable to get a valid config path. Error: {}", e);
std::process::exit(1);
}
};
let config: config::Config;
match &opts.subcmd {
SubCommand::Install(_s) => {
let db_name = "database.db".to_string();
config = config::Config {
database_name: db_name,
library_name: "your-library".to_string(),
attrition_config: config::AttritionConfig {
entries: Vec::new(),
},
part_number_ignore_list: Vec::new(),
};
if config::save_config(&config, &config_path).is_err() {
eprintln!("Unable to install database and config");
std::process::exit(1);
};
println!("Config installed to {}", config_path.to_string_lossy());
std::process::exit(0);
}
_ => {
config = match config::load_config(&config_path) {
Ok(c) => c,
Err(e) => {
if e.to_string().contains("No such file or directory") {
eprintln!("Unable to get config. Run `eagle-plm install` first.");
} else {
eprintln!("Error loading configuration: {}", e);
}
std::process::exit(1);
}
};
}
};
let conn = establish_connection(&config.database_name);
let stdio = io::stdin();
let input = stdio.lock();
let output = io::stdout();
let prompt = prompt::Prompt {
reader: input,
writer: output,
};
let mut app = Application {
config,
config_path,
prompt,
conn,
};
match opts.subcmd {
SubCommand::Build(s) => match s.subcmd {
BuildSubCommand::Create(_) => {
builds::create(&mut app);
}
BuildSubCommand::Delete(a) => {
builds::delete(&mut app, a.build_id);
}
BuildSubCommand::Show(a) => {
builds::show(&mut app, a.all);
}
BuildSubCommand::Complete(a) => {
builds::complete(&mut app, a.build_id);
}
BuildSubCommand::Export(a) => {
builds::export(&mut app, a.build_id);
}
},
SubCommand::Inventory(s) => match s.subcmd {
InventorySubCommand::Create(_) => {
inventory::create(&mut app);
}
InventorySubCommand::Import(a) => {
inventory::create_from_file(&mut app, &a.filename);
}
InventorySubCommand::Update(a) => {
inventory::update_from_file(&mut app, &a.filename);
}
InventorySubCommand::Export(a) => {
inventory::export_to_file(&mut app, &a.filename, a.export_all);
}
InventorySubCommand::Shortages(a) => {
inventory::export_shortages_to_file(&mut app, &a.filename);
}
InventorySubCommand::Delete(_) => {
println!("Not implemented!");
}
InventorySubCommand::Show(a) => {
if a.show_shortage {
inventory::show_shortage(&mut app, a.all_entries);
} else {
inventory::show(&mut app, a.all_entries);
}
}
},
SubCommand::Parts(s) => match s.subcmd {
PartsSubCommand::Create(a) => match a.filename {
Some(x) => parts::create_by_csv(&mut app, &x),
None => parts::create(&mut app),
},
PartsSubCommand::Delete(_) => {
parts::delete(&mut app);
}
PartsSubCommand::Show(_) => {
parts::show(&mut app);
}
PartsSubCommand::Rename(_) => {
parts::rename(&mut app);
}
},
SubCommand::Bom(s) => match s.subcmd {
BomSubCommand::Import(a) => {
bom::import(&mut app, &a.filename);
}
BomSubCommand::Export(a) => {
bom::export(&mut app, &a.name, &a.version);
}
BomSubCommand::Show(a) => {
bom::show(&mut app, &a.part_number, &a.version);
}
},
SubCommand::Install(_) => {}
}
}