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 implementsCommandFactory.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 handlinggenerate-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 rust_config_tree::config::ConfigSchema;
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::ValidateConfig { config: None },
std::path::Path::new("config.yaml"),
)?;Examples found in repository?
examples/config_commands.rs (line 82)
65fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
66 let cli = Cli::parse();
67 let config_path = match &cli.config {
68 Some(path) => path.clone(),
69 None => write_demo_config()?,
70 };
71
72 match cli.command.unwrap_or(Command::Run) {
73 Command::Run => {
74 let config = load_config::<AppConfig>(&config_path)?;
75 println!("config path: {}", config_path.display());
76 println!("include count: {}", config.include.len());
77 println!("mode: {}", config.mode);
78 println!("server bind: {}", config.server.bind);
79 println!("server port: {}", config.server.port);
80 }
81 Command::Config(command) => {
82 handle_config_command::<Cli, AppConfig>(command, &config_path)?;
83 }
84 }
85
86 Ok(())
87}