use clap::{Parser, Subcommand};
use std::path::PathBuf;
use jkconfig::{
data::{AppState, ConfigDocument},
ui::run_tui as launch_tui,
};
#[derive(Parser)]
#[command(name = "jkconfig")]
#[command(author = "周睿 <zrufo747@outlook.com>")]
#[command(about = "配置编辑器", long_about = None)]
struct Cli {
#[arg(short = 'c', long = "config", default_value = ".config.toml")]
config: PathBuf,
#[arg(short = 's', long = "schema")]
schema: Option<PathBuf>,
#[command(subcommand)]
command: Option<Commands>,
}
#[derive(Subcommand)]
enum Commands {
Tui,
Web {
#[arg(short = 'p', long = "port", default_value = "3000")]
port: u16,
},
}
fn main() -> anyhow::Result<()> {
let cli = Cli::parse();
let config_path = cli.config.to_string_lossy().to_string();
let schema_path = cli.schema.as_ref().map(|p| p.to_string_lossy().to_string());
let config_file = Some(config_path.as_str());
let schema_file = schema_path.as_deref();
let document = ConfigDocument::new(config_file, schema_file)?;
let app_state = AppState::new(document);
match cli.command {
Some(Commands::Web { port }) => {
tokio::runtime::Runtime::new()?.block_on(jkconfig::web::run_server(app_state, port))?;
}
Some(Commands::Tui) | None => {
run_tui(app_state)?;
}
}
Ok(())
}
fn run_tui(app_state: AppState) -> anyhow::Result<()> {
let mut app = launch_tui(app_state)?;
app.persist_if_needed()?;
Ok(())
}