1use std::path::PathBuf;
40
41use clap::Parser;
42use hoprd::config::HoprdConfig;
43use validator::Validate;
44
45#[derive(Parser, Default)]
46#[clap(author, version, about, long_about = None)]
47struct CliArgs {
48 #[clap(short = 'd', long, conflicts_with = "validate")]
50 default: bool,
51 #[clap(short, long, conflicts_with = "default")]
53 validate: Option<PathBuf>,
54}
55
56fn main() -> Result<(), hoprd::errors::HoprdError> {
57 let args = CliArgs::parse();
58
59 if args.default {
60 println!(
61 "{}",
62 serde_yaml::to_string(&hoprd::config::HoprdConfig::default())
63 .map_err(|e| hoprd::errors::HoprdError::ConfigError(e.to_string()))?
64 );
65 } else if let Some(cfg_path) = args.validate {
66 let cfg_path = cfg_path
67 .into_os_string()
68 .into_string()
69 .map_err(|_| hoprd::errors::HoprdError::ConfigError("file path not convertible".into()))?;
70
71 let yaml_configuration = hopr_platform::file::native::read_to_string(&cfg_path)
72 .map_err(|e| hoprd::errors::HoprdError::ConfigError(e.to_string()))?;
73
74 let cfg: HoprdConfig = serde_yaml::from_str(&yaml_configuration)
75 .map_err(|e| hoprd::errors::HoprdError::SerializationError(e.to_string()))?;
76
77 if !cfg
78 .hopr
79 .chain
80 .protocols
81 .supported_networks(hopr_lib::constants::APP_VERSION_COERCED)
82 .iter()
83 .any(|network| network == &cfg.hopr.chain.network)
84 {
85 return Err(hoprd::errors::HoprdError::ValidationError(format!(
86 "The specified network '{}' is not listed as supported ({:?})",
87 cfg.hopr.chain.network,
88 cfg.hopr
89 .chain
90 .protocols
91 .supported_networks(hopr_lib::constants::APP_VERSION_COERCED)
92 )));
93 };
94
95 if let Err(e) = cfg.validate() {
96 return Err(hoprd::errors::HoprdError::ValidationError(e.to_string()));
97 };
98 }
99
100 Ok(())
101}