use std::path::PathBuf;
use clap::{Parser, Subcommand};
use zaino_common::xdg::resolve_path_with_xdg_config_defaults;
pub fn default_config_path() -> PathBuf {
resolve_path_with_xdg_config_defaults("zaino/zainod.toml")
}
#[derive(Parser, Debug)]
#[command(
name = "zainod",
version,
about = "Zaino - The Zcash Indexing Service",
long_about = None
)]
pub struct Cli {
#[command(subcommand)]
pub command: Command,
}
#[derive(Subcommand, Debug, Clone)]
pub enum Command {
Start {
#[arg(short, long, value_name = "FILE")]
config: Option<PathBuf>,
},
GenerateConfig {
#[arg(short, long, value_name = "FILE")]
output: Option<PathBuf>,
},
}
impl Command {
pub fn generate_config(output: Option<PathBuf>) {
let path = output.unwrap_or_else(default_config_path);
let content = match crate::config::generate_default_config() {
Ok(content) => content,
Err(e) => {
eprintln!("Error generating config: {}", e);
std::process::exit(1);
}
};
if let Some(parent) = path.parent() {
if !parent.exists() {
if let Err(e) = std::fs::create_dir_all(parent) {
eprintln!("Error creating directory {}: {}", parent.display(), e);
std::process::exit(1);
}
}
}
if let Err(e) = std::fs::write(&path, &content) {
eprintln!("Error writing to {}: {}", path.display(), e);
std::process::exit(1);
}
eprintln!("Generated config file: {}", path.display());
}
}