mod library; mod convert; mod places; mod cli; mod generation; mod generation_boilerplate; mod config; mod dir; mod obj_print; mod obj_print_boilerplate; mod package_management; mod system; mod pkg_managers; mod pkg_managers_boilerplate; mod hook;
use clap::Parser;
use std::io;
use library::*;
use config::ConfigSide;
use colored::Colorize;
use piglog::prelude::*;
use piglog::*;
#[derive(PartialEq)]
enum ExitCode {
Success,
Fail,
}
fn test_code() {
}
fn main() {
if app() == ExitCode::Fail {
std::process::exit(1);
}
}
fn app() -> ExitCode {
test_code();
match is_root_user() {
true => {
error!("Cannot run as root! Please run as the normal user!");
return ExitCode::Fail;
},
false => {},
};
let args = cli::Cli::parse();
match &args.command {
cli::Commands::Setup => {},
_ => {
if places::base().exists() == false {
error!("It seems that the program is not set up!");
return ExitCode::Fail;
}
},
}
#[allow(unreachable_patterns)]
match &args.command {
cli::Commands::Gen { command } => {
match command {
cli::GenCommands::Commit(c) => {
info!("Committing user generation...");
match generation::commit(c.msg.as_str()) {
Ok(_o) => info!("Committed generation successfully! (\"{}\")", c.msg),
Err(_e) => return ExitCode::Fail,
};
},
cli::GenCommands::List => {
match generation::list_print() {
Ok(_o) => {},
Err(_e) => return ExitCode::Fail,
};
},
cli::GenCommands::Info => {
let generation = match generation::gen(ConfigSide::User) {
Ok(o) => o,
Err(_e) => return ExitCode::Fail,
};
obj_print::generation(&generation);
},
cli::GenCommands::Latest => {
info!("Latest generation number is: {}", match generation::latest_number() {
Ok(o) => o,
Err(_e) => return ExitCode::Fail,
});
},
cli::GenCommands::DeleteOld(h) => {
info!("Deleting old generations...");
match generation::delete_old(h.how_many) {
Ok(_o) => info!("Successfully deleted {} generations!", h.how_many),
Err(_e) => return ExitCode::Fail,
};
},
cli::GenCommands::Delete(g) => {
match generation::delete(g.generation) {
Ok(_o) => {}, Err(_e) => return ExitCode::Fail,
};
},
cli::GenCommands::Diff { old, new } => {
if generation::gen_exists(*old) == false || generation::gen_exists(*new) == false {
fatal!("Generation not found!");
return ExitCode::Fail;
}
let gen_1 = generation::get_gen_from_usize(*old).unwrap();
let gen_2 = generation::get_gen_from_usize(*new).unwrap();
let commit_1 = generation::get_gen_commit_from_usize(*old).unwrap();
let commit_2 = generation::get_gen_commit_from_usize(*new).unwrap();
let history = library::history_gen(&gen_1, &gen_2);
let history: Vec<&Vec<History>> = history.iter().collect();
println!(
"{} {} {}",
commit_1.bright_cyan().bold(),
"->".bright_black().bold(),
commit_2.bright_cyan().bold()
);
println!("");
library::print_history_gen(&history);
},
cli::GenCommands::Current { command } => {
match command {
cli::CurrentCommands::Build => {
info!("Building 'current' generation...");
match generation::build() {
Ok(_o) => info!("Built generation successfully!"),
Err(_e) => return ExitCode::Fail,
};
},
cli::CurrentCommands::Rollback(r) => {
info!("Rolling back by {} generations...", r.by);
match generation::rollback(r.by) {
Ok(_o) => info!("Rolled back successfully!"),
Err(_e) => return ExitCode::Fail,
};
},
cli::CurrentCommands::ToLatest => {
info!("Jumping to latest generation...");
match generation::latest() {
Ok(_o) => info!("Jumped to latest successfully!"),
Err(_e) => return ExitCode::Fail,
};
},
cli::CurrentCommands::Set(s) => {
info!("Jumping to generation {}...", s.to);
match generation::set_current(s.to) {
Ok(_o) => info!("Jumped to generation {} successfully!", s.to),
Err(_e) => return ExitCode::Fail,
};
},
_ => {
error!("Command not usable yet!");
return ExitCode::Fail;
},
};
},
_ => {
error!("Command not usable yet!");
return ExitCode::Fail;
},
};
},
cli::Commands::Setup => {
info!("Beginning setup...");
match setup() {
Ok(_o) => info!("Set up the program successfully!"),
Err(_e) => return ExitCode::Fail,
};
},
cli::Commands::InitConfig => {
info!("Creating user configuration...");
match config::init_user_config() {
Ok(_o) => info!("Created user configuration successfully!"),
Err(_e) => return ExitCode::Fail,
};
},
cli::Commands::API { command } => {
match command {
cli::APICommands::Echo { log_mode, message } => {
piglog::log_core_print(message.to_string(), *log_mode);
},
cli::APICommands::EchoGeneric { message } => {
piglog::log_generic_print(message.to_string());
},
};
},
_ => {
error!("Command not usable yet!");
return ExitCode::Fail;
},
};
return ExitCode::Success;
}
fn setup() -> Result<(), io::Error> {
match places::setup() {
Ok(_o) => info!("Core directories verified successfully!"),
Err(e) => return Err(e),
};
return Ok(());
}