From 30aef244278104f9f1652009dee1e0d72a676f48 Mon Sep 17 00:00:00 2001 From: Maria Kuklina <101095419+kmd-fl@users.noreply.github.com> Date: Thu, 9 Nov 2023 13:36:11 +0100 Subject: [PATCH] feat(nox): add new flags for system services [NET-547, NET-548] (#1888) * add new flags to set wallet-key and enable system services * throw error when wallet key doesn't set --- crates/server-config/src/args.rs | 111 +++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) diff --git a/crates/server-config/src/args.rs b/crates/server-config/src/args.rs index f94b5853ea..a20ba96634 100644 --- a/crates/server-config/src/args.rs +++ b/crates/server-config/src/args.rs @@ -14,7 +14,9 @@ * limitations under the License. */ +use crate::system_services_config::ServiceKey; use crate::LogFormat; +use clap::error::ErrorKind; use clap::{Args, Parser}; use config::{ConfigError, Map, Source, Value}; use serde::ser::SerializeStruct; @@ -150,6 +152,112 @@ pub enum TracingType { Otlp, } +#[derive(Debug, Clone, Serialize)] +pub enum EnabledSystemServices { + All, + Some(Vec), + None, +} + +// Parse either: +// - "all" to EnabledSystemServices::All +// - "none" to EnabledSystemServices::None +// - "service1,service2" to EnabledSystemServices::Some(vec!["service1", "service2"]) +#[derive(Debug, Clone)] +struct EnabledSystemServicesValueParser; +impl clap::builder::TypedValueParser for EnabledSystemServicesValueParser { + type Value = EnabledSystemServices; + fn parse_ref( + &self, + _cmd: &clap::Command, + _arg: Option<&clap::Arg>, + value: &std::ffi::OsStr, + ) -> Result { + let value = value + .to_str() + .ok_or_else(|| clap::Error::new(ErrorKind::InvalidUtf8))?; + + match value { + "all" => Ok(EnabledSystemServices::All), + "none" => Ok(EnabledSystemServices::None), + _ => { + let services = value.split(',').map(|s| s.to_string()).collect::>(); + Ok(EnabledSystemServices::Some(services)) + } + } + } +} + +#[derive(Parser, Debug, Clone)] +pub(crate) struct SystemServicesArgs { + // TODO: how to provide the list of available system services automatically + #[arg( + long, + id = "SERVICES", + help = "List of enabled system services. Can be: all, none or comma-separated list of services (serivce1,service2)", + help_heading = "System services configuration", + value_parser = EnabledSystemServicesValueParser + )] + enable_system_services: Option, + + #[arg( + long, + id = "WALLET_KEY", + help = "The private wallet key for signing transactions for joining deals", + help_heading = "System services configuration" + )] + wallet_key: Option, +} + +impl Serialize for SystemServicesArgs { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + let mut struct_serializer = serializer.serialize_struct("SystemServicesConfig", 5)?; + + if let Some(enable_system_services) = &self.enable_system_services { + match enable_system_services { + EnabledSystemServices::All => { + let all = ServiceKey::all_values(); + struct_serializer.serialize_field("enable", &all)?; + } + EnabledSystemServices::Some(services) => { + let services: Vec = services + .iter() + .map(|service| match ServiceKey::from_string(service) { + Some(service) => Ok(service), + None => Err(serde::ser::Error::custom(format!( + "unknown service: {}", + service + ))), + }) + .collect::>()?; + + struct_serializer.serialize_field("enable", &services)?; + } + EnabledSystemServices::None => { + struct_serializer.serialize_field::>("enable", &vec![])?; + } + } + } + if let Some(wallet_key) = &self.wallet_key { + #[derive(Serialize)] + struct DeciderConfig { + wallet_key: String, + } + struct_serializer.serialize_field( + "decider", + &DeciderConfig { + wallet_key: wallet_key.clone(), + }, + )?; + } + + struct_serializer.end() + } +} + #[derive(Parser, Debug, Serialize, Clone)] pub(crate) struct DerivedArgs { #[arg( @@ -330,6 +438,9 @@ pub(crate) struct DerivedArgs { )] pub(crate) no_banner: Option, + #[command(flatten)] + system_services: Option, + #[command(flatten)] log: Option,