cli.rs (2201B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2025 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or modify it under the 6 terms of the GNU Affero General Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 TALER is distributed in the hope that it will be useful, but WITHOUT ANY 10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. 12 13 You should have received a copy of the GNU Affero General Public License along with 14 TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 17 use std::io::Write as _; 18 19 use crate::config::Config; 20 21 /// Inspect the configuration 22 #[derive(clap::Subcommand, Debug)] 23 pub enum ConfigCmd { 24 /// Lookup config value 25 Get { 26 section: String, 27 option: String, 28 /// Interpret value as path with dollar-expansion 29 #[clap(short, long, default_value_t = false)] 30 filename: bool, 31 }, 32 /// Substitute variables in a path 33 Pathsub { path_expr: String }, 34 /// Dump the configuration 35 Dump { 36 /// output extra diagnostics 37 #[clap(short, long, default_value_t = false)] 38 diagnostics: bool, 39 }, 40 } 41 42 impl ConfigCmd { 43 pub fn run(&self, cfg: &Config) -> anyhow::Result<()> { 44 let mut out = std::io::stdout().lock(); 45 match self { 46 ConfigCmd::Get { 47 section, 48 option, 49 filename, 50 } => { 51 let sect = cfg.section(section); 52 let value = if *filename { 53 sect.path(option).require()? 54 } else { 55 sect.str(option).require()? 56 }; 57 writeln!(&mut out, "{value}")?; 58 } 59 ConfigCmd::Pathsub { path_expr } => { 60 let path = cfg.pathsub(path_expr, 0)?; 61 writeln!(&mut out, "{path}")?; 62 } 63 ConfigCmd::Dump { diagnostics } => { 64 cfg.print(&mut out, *diagnostics)?; 65 } 66 } 67 Ok(()) 68 } 69 }