feat(config): implement config init/get/set/path commands

This commit is contained in:
Ciphercat 2026-03-05 18:28:24 +00:00
commit d2fa081266
4 changed files with 134 additions and 7 deletions

View file

@ -1,5 +1,8 @@
use clap::{Args, Subcommand};
use crate::config::{config_path, ensure_default_config, get_config_value, set_config_value};
use crate::error::IdxError;
#[derive(Debug, Args)]
pub struct ConfigCmd {
#[command(subcommand)]
@ -13,3 +16,25 @@ pub enum ConfigSubcommand {
Set { key: String, value: String },
Path,
}
pub fn handle(cmd: &ConfigCmd) -> Result<(), IdxError> {
match &cmd.command {
ConfigSubcommand::Init => {
let path = ensure_default_config()?;
println!("{}", path.display());
}
ConfigSubcommand::Get { key } => {
let value = get_config_value(key)?
.ok_or_else(|| IdxError::ConfigError(format!("key not found: {key}")))?;
println!("{value}");
}
ConfigSubcommand::Set { key, value } => {
set_config_value(key, value)?;
println!("ok");
}
ConfigSubcommand::Path => {
println!("{}", config_path()?.display());
}
}
Ok(())
}

View file

@ -16,14 +16,18 @@ pub enum Shell {
#[derive(Debug, Parser)]
#[command(name = "idx", about = "Indonesian stock analysis CLI")]
pub struct Cli {
#[arg(short, long, value_enum, global = true, default_value_t = OutputFormat::Table)]
pub output: OutputFormat,
#[arg(short, long, value_enum, global = true)]
pub output: Option<OutputFormat>,
#[arg(long, global = true)]
pub no_color: bool,
#[arg(short, long, global = true)]
pub quiet: bool,
#[arg(short, long, global = true, action = clap::ArgAction::Count)]
pub verbose: u8,
#[arg(long, global = true)]
pub offline: bool,
#[arg(long, global = true)]
pub no_cache: bool,
#[command(subcommand)]
pub command: Commands,
}