pub mod program;
pub mod distro;
pub mod parser;
pub mod executor;
pub mod types;
pub mod display;
use std::env;
use types::Shell;
use clap::Parser;
use crate::types::{Command, FileType};
use std::{path::Path, error::Error};
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
pub struct Arguments {
#[command(subcommand)]
command: Option<Command>,
file: String,
#[arg(long, group = "cli")]
count: bool,
#[arg(long, group = "cli")]
count_missing: bool,
#[arg(long, group = "cli")]
check: bool,
#[arg(value_enum)]
shell: Option<Shell>,
}
fn main() -> Result<(), Box<dyn Error>> {
if cfg!(debug_assertions) {
env::set_var("RUST_BACKTRACE", "1");
}
let args = Arguments::parse();
let file = &args.file;
let programs: Vec<program::Program>= parser::get_programs_from_file(file);
if args.count {
println!("{}", program::count(&programs));
} else if args.count_missing {
println!("{}", program::count_missing(&programs));
} else if args.check {
println!("File looks good!");
} else if let Some(command) = args.command {
match &command {
Command::Install { all } => {
if *all {
program::install_missing(&programs);
}
},
Command::Configure { all } => {
if *all {
program::configure_all(&programs);
}
},
Command::Export { filetype } => {
match filetype {
FileType::Json => {
let string = serde_json::to_string_pretty(&programs).unwrap();
let new_file = Path::new(file).with_extension("json");
std::fs::write(new_file, string).unwrap();
},
FileType::Yaml => {
let string = serde_yaml::to_string(&programs).unwrap();
let new_file = Path::new(file).with_extension("yml");
std::fs::write(new_file, string).unwrap();
},
}
},
Command::List { status } => {
if *status {
program::print_status(&programs);
} else {
program::print_name(&programs);
}
}
}
} else {
display::run_ui(display::UI::TUI, programs);
}
Ok(())
}