cli.rs (2210B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2025, 2026 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 compact_str::CompactString; 20 21 use crate::config::Config; 22 23 /// Inspect the configuration 24 #[derive(clap::Subcommand, Debug)] 25 pub enum ConfigCmd { 26 /// Lookup config value 27 Get { 28 section: CompactString, 29 option: CompactString, 30 /// Interpret value as path with dollar-expansion 31 #[arg(short, long)] 32 filename: bool, 33 }, 34 /// Substitute variables in a path 35 Pathsub { path_expr: CompactString }, 36 /// Dump the configuration 37 Dump { 38 /// output extra diagnostics 39 #[arg(short, long)] 40 diagnostics: bool, 41 }, 42 } 43 44 impl ConfigCmd { 45 pub fn run(self, cfg: &Config) -> anyhow::Result<()> { 46 let mut out = std::io::stdout().lock(); 47 match self { 48 ConfigCmd::Get { 49 section, 50 option, 51 filename, 52 } => { 53 let sect = cfg.section(§ion); 54 let value = if filename { 55 sect.path(&option).require()? 56 } else { 57 sect.str(&option).require()? 58 }; 59 writeln!(&mut out, "{value}")?; 60 } 61 ConfigCmd::Pathsub { path_expr } => { 62 let path = cfg.pathsub(&path_expr, 0)?; 63 writeln!(&mut out, "{path}")?; 64 } 65 ConfigCmd::Dump { diagnostics } => { 66 cfg.print(&mut out, diagnostics)?; 67 } 68 } 69 Ok(()) 70 } 71 }