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 config-template.

§Returns

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

§Examples

use clap::{Parser, Subcommand};
use confique::Config;
use rust_config_tree::{ConfigCommand, ConfigSchema, 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)]
struct AppConfig {
    #[config(default = [])]
    include: Vec<std::path::PathBuf>,
}

impl ConfigSchema for AppConfig {
    fn include_paths(layer: &<Self as Config>::Layer) -> Vec<std::path::PathBuf> {
        layer.include.clone().unwrap_or_default()
    }
}

handle_config_command::<Cli, AppConfig>(
    ConfigCommand::ConfigValidate,
    std::path::Path::new("config.yaml"),
)?;