use anyhow::{Context, Result};
use clap::Parser;
use hk_parser::{load_hk_file, parse_hk, resolve_interpolations, HkError};
use std::fs;
use std::path::PathBuf;
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
#[arg(short, long)]
input: PathBuf,
#[arg(short, long)]
validate: bool,
#[arg(short, long)]
resolve: bool,
#[arg(short, long)]
output: Option<PathBuf>,
#[arg(long, default_value_t = true)]
color: bool,
}
fn main() -> Result<()> {
let args = Args::parse();
let contents = fs::read_to_string(&args.input)
.with_context(|| format!("Failed to read input file: {}", args.input.display()))?;
let parse_result = parse_hk(&contents);
match parse_result {
Ok(mut config) => {
if args.resolve {
if let Err(e) = resolve_interpolations(&mut config) {
e.pretty_print(&contents);
std::process::exit(1);
}
}
if let Some(output_path) = args.output {
hk_parser::write_hk_file(output_path, &config)?;
} else {
println!("{}", hk_parser::serialize_hk(&config));
}
}
Err(e) => {
e.pretty_print(&contents);
std::process::exit(1);
}
}
Ok(())
}