mod config_cmd;
use std::path::PathBuf;
use anyhow::Result;
use clap::{Parser, Subcommand};
use crate::config;
#[derive(Parser, Debug)]
#[command(version, about)]
pub struct Cli {
#[arg(short = 'f', long = "config-file", global = true)]
pub config_file: Option<PathBuf>,
#[arg(short, long)]
pub log_level: Option<String>,
#[arg(long)]
pub validate: bool,
#[command(subcommand)]
pub command: Option<Command>,
}
#[derive(Subcommand, Debug)]
pub enum Command {
Config {
#[command(subcommand)]
action: config_cmd::ConfigAction,
},
}
pub fn run_command(cli: &Cli) -> Result<bool> {
let config_path = cli.config_file.clone().unwrap_or_else(config::dirs_path);
if cli.validate {
config::load_from_path(&config_path)?;
println!("configuration is valid: {}", config_path.display());
return Ok(true);
}
match &cli.command {
Some(Command::Config { action }) => {
config_cmd::run(action, &config_path)?;
Ok(true)
}
None => Ok(false),
}
}