Skip to main content

handle_config_command

Function handle_config_command 

Source
pub fn handle_config_command<C, S>(
    command: ConfigCommand,
    config_path: &Path,
) -> ConfigResult<()>
Expand description

Handles a built-in config subcommand for a consumer CLI.

C is the clap parser type used to generate completion metadata. S is the application config schema used for template and JSON Schema generation.

§Type Parameters

  • C: The consumer CLI parser type that implements CommandFactory.
  • S: The consumer config schema used when rendering config templates and JSON Schema files.

§Arguments

  • command: Built-in subcommand selected by the consumer CLI.
  • config_path: Root config path used as the template source when handling generate-template.

§Returns

Returns Ok(()) after the selected subcommand completes.

§Examples

use clap::{Parser, Subcommand};
use confique::Config;
use rust_config_tree::cli::{ConfigCommand, handle_config_command};
use schemars::JsonSchema;

#[derive(Parser)]
struct Cli {
    #[command(subcommand)]
    command: Command,
}

#[derive(Subcommand)]
enum Command {
    #[command(flatten)]
    Config(ConfigCommand),
}

#[derive(Config, JsonSchema, rust_config_tree::ConfigSchema)]
struct AppConfig {
    #[config(default = [])]
    include: Vec<std::path::PathBuf>,
}

handle_config_command::<Cli, AppConfig>(
    ConfigCommand::ValidateConfig { config: None },
    std::path::Path::new("config.yaml"),
)?;
Examples found in repository?
examples/config_commands.rs (line 76)
59fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
60    let cli = Cli::parse();
61    let config_path = match &cli.config {
62        Some(path) => path.clone(),
63        None => write_demo_config()?,
64    };
65
66    match cli.command.unwrap_or(Command::Run) {
67        Command::Run => {
68            let config = load_config::<AppConfig>(&config_path)?;
69            println!("config path: {}", config_path.display());
70            println!("include count: {}", config.include.len());
71            println!("mode: {}", config.mode);
72            println!("server bind: {}", config.server.bind);
73            println!("server port: {}", config.server.port);
74        }
75        Command::Config(command) => {
76            handle_config_command::<Cli, AppConfig>(command, &config_path)?;
77        }
78    }
79
80    Ok(())
81}