feat: initial commit
This commit is contained in:
60
src/cli.rs
Normal file
60
src/cli.rs
Normal file
@@ -0,0 +1,60 @@
|
||||
use clap::{Parser, Args, Subcommand, crate_authors, crate_description, crate_name, crate_version};
|
||||
use clap_complete::Shell;
|
||||
|
||||
#[derive(Parser, Debug, PartialEq)]
|
||||
#[command(name = crate_name!(), author = crate_authors!(), version = crate_version!(), about = crate_description!())]
|
||||
pub struct Cli {
|
||||
#[arg(long = "generate", hide = true, value_enum)]
|
||||
pub generator: Option<Shell>,
|
||||
|
||||
#[command(subcommand)]
|
||||
pub command: Commands,
|
||||
}
|
||||
|
||||
#[derive(Subcommand, Debug, PartialEq)]
|
||||
pub enum Commands {
|
||||
#[command(about = "A set of commands to configure Fedora")]
|
||||
Config(ConfigCommand),
|
||||
|
||||
#[command(about = "A set of commands to manage packages")]
|
||||
Package(PackageCommand),
|
||||
}
|
||||
|
||||
#[derive(Args, Debug, PartialEq)]
|
||||
pub struct ConfigCommand {
|
||||
#[arg(long, value_name = "CONFIG_FILE", default_value = "config.toml")]
|
||||
pub config_file: String,
|
||||
|
||||
#[command(subcommand)]
|
||||
pub subcommand: ConfigSubcommand,
|
||||
}
|
||||
|
||||
#[derive(Args, Debug, PartialEq)]
|
||||
pub struct PackageCommand {
|
||||
#[arg(long, value_name = "PACKAGE_FILE", default_value = "packages.toml")]
|
||||
pub package_file: String,
|
||||
|
||||
#[command(subcommand)]
|
||||
pub subcommand: PackageSubcommand,
|
||||
}
|
||||
|
||||
#[derive(Subcommand, Debug, PartialEq)]
|
||||
pub enum ConfigSubcommand {
|
||||
#[command(about = "Generate a configuration file")]
|
||||
Generate,
|
||||
|
||||
#[command(about = "Validate a configuration file")]
|
||||
Validate,
|
||||
}
|
||||
|
||||
#[derive(Subcommand, Debug, PartialEq)]
|
||||
pub enum PackageSubcommand {
|
||||
#[command(about = "Install packages")]
|
||||
Install,
|
||||
|
||||
#[command(about = "Remove packages")]
|
||||
Remove,
|
||||
|
||||
#[command(about = "List installed packages")]
|
||||
List,
|
||||
}
|
||||
95
src/main.rs
Normal file
95
src/main.rs
Normal file
@@ -0,0 +1,95 @@
|
||||
mod cli;
|
||||
|
||||
use clap::Parser;
|
||||
use cli::Cli;
|
||||
use env_logger::fmt::style;
|
||||
|
||||
use std::io::Write;
|
||||
|
||||
fn main() {
|
||||
let log_level = std::env::var("RUST_LOG").unwrap_or("info".to_string());
|
||||
env_logger::Builder::new()
|
||||
.format(|buf, record| {
|
||||
let level = record.level();
|
||||
let style = buf.default_level_style(level);
|
||||
let dimmed = style::AnsiColor::White.on_default().dimmed();
|
||||
let message_style = match level {
|
||||
log::Level::Error => style::AnsiColor::Red.on_default().bold(),
|
||||
log::Level::Warn => style::AnsiColor::Yellow.on_default().bold(),
|
||||
log::Level::Info => style::AnsiColor::White.on_default(),
|
||||
log::Level::Debug => dimmed,
|
||||
log::Level::Trace => dimmed,
|
||||
};
|
||||
writeln!(
|
||||
buf,
|
||||
"{style}{level:<5}{style:#} {dimmed}>{dimmed:#} {message_style}{}{message_style:#}",
|
||||
record.args()
|
||||
)
|
||||
})
|
||||
.parse_filters(log_level.as_str())
|
||||
.init();
|
||||
|
||||
let cli = Cli::parse();
|
||||
log::debug!("Parsed CLI arguments: {:?}", cli);
|
||||
match cli.command {
|
||||
cli::Commands::Config(config) => {
|
||||
log::info!("Config command selected with config file: {}", config.config_file);
|
||||
handle_config_command(config);
|
||||
}
|
||||
cli::Commands::Package(package) => {
|
||||
log::info!("Package command selected with package file: {}", package.package_file);
|
||||
handle_package_command(package);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_config_command(config: cli::ConfigCommand) {
|
||||
match config.subcommand {
|
||||
cli::ConfigSubcommand::Generate => {
|
||||
log::info!("Generating configuration file: {}", config.config_file);
|
||||
handle_generate_config(&config.config_file);
|
||||
}
|
||||
cli::ConfigSubcommand::Validate => {
|
||||
log::info!("Validating configuration file: {}", config.config_file);
|
||||
handle_validate_config(&config.config_file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_package_command(package: cli::PackageCommand) {
|
||||
match package.subcommand {
|
||||
cli::PackageSubcommand::Install => {
|
||||
log::info!("Installing packages from file: {}", package.package_file);
|
||||
handle_install_packages(&package.package_file);
|
||||
}
|
||||
cli::PackageSubcommand::Remove => {
|
||||
log::info!("Removing packages from file: {}", package.package_file);
|
||||
handle_remove_packages(&package.package_file);
|
||||
}
|
||||
cli::PackageSubcommand::List => {
|
||||
log::info!("Listing installed packages from file: {}", package.package_file);
|
||||
handle_list_packages(&package.package_file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_generate_config(config_file: &str) {
|
||||
log::info!("Generating configuration file: {}", config_file);
|
||||
// Implement the logic to generate a configuration file
|
||||
}
|
||||
fn handle_validate_config(config_file: &str) {
|
||||
log::info!("Validating configuration file: {}", config_file);
|
||||
// Implement the logic to validate a configuration file
|
||||
}
|
||||
fn handle_install_packages(package_file: &str) {
|
||||
log::info!("Installing packages from file: {}", package_file);
|
||||
// Implement the logic to install packages
|
||||
}
|
||||
fn handle_remove_packages(package_file: &str) {
|
||||
log::info!("Removing packages from file: {}", package_file);
|
||||
// Implement the logic to remove packages
|
||||
}
|
||||
fn handle_list_packages(package_file: &str) {
|
||||
log::info!("Listing installed packages from file: {}", package_file);
|
||||
// Implement the logic to list installed packages
|
||||
}
|
||||
Reference in New Issue
Block a user