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