diff --git a/src/config.rs b/src/config.rs index d923f3140..8331c4455 100644 --- a/src/config.rs +++ b/src/config.rs @@ -20,7 +20,7 @@ use crate::eth::follower::importer::ImporterConfig; use crate::eth::miner::MinerConfig; use crate::eth::primitives::Address; use crate::eth::rpc::RpcServerConfig; -use crate::eth::storage::StratusStorageConfig; +use crate::eth::storage::StorageConfig; use crate::ext::parse_duration; use crate::infra::build_info; use crate::infra::kafka::KafkaConfig; @@ -190,7 +190,7 @@ pub struct StratusConfig { pub rpc_server: RpcServerConfig, #[clap(flatten)] - pub storage: StratusStorageConfig, + pub storage: StorageConfig, #[clap(flatten)] pub executor: ExecutorConfig, @@ -290,7 +290,7 @@ pub struct ImporterOfflineConfig { pub miner: MinerConfig, #[clap(flatten)] - pub storage: StratusStorageConfig, + pub storage: StorageConfig, #[clap(flatten)] pub rpc_storage: ExternalRpcConfig, @@ -347,7 +347,7 @@ pub struct IntegrationTestConfig { pub miner: MinerConfig, #[clap(flatten)] - pub storage: StratusStorageConfig, + pub storage: StorageConfig, #[clap(flatten)] pub rpc_storage: ExternalRpcConfig, @@ -391,7 +391,7 @@ impl FromStr for Environment { "staging" | "test" => Ok(Self::Staging), "production" | "prod" => Ok(Self::Production), "canary" => Ok(Self::Canary), - s => Err(anyhow!("unknown environment: \"{}\" - valid values are {:?}", s, Environment::VARIANTS)), + s => Err(anyhow!("unknown environment: \"{}\" - valid values are {:?}", s, Self::VARIANTS)), } } } diff --git a/src/eth/storage/mod.rs b/src/eth/storage/mod.rs index 31345fb42..861a66f52 100644 --- a/src/eth/storage/mod.rs +++ b/src/eth/storage/mod.rs @@ -5,6 +5,7 @@ pub use permanent::PermanentStorage; pub use permanent::PermanentStorageConfig; pub use permanent::PermanentStorageKind; pub use stratus_storage::StratusStorage; +use strum::VariantNames; pub use temporary::InMemoryTemporaryStorage; pub use temporary::TemporaryStorage; pub use temporary::TemporaryStorageConfig; @@ -14,8 +15,10 @@ pub mod permanent; mod stratus_storage; mod temporary; +use std::str::FromStr; use std::sync::Arc; +use anyhow::anyhow; use clap::Parser; use display_json::DebugAsJson; @@ -104,7 +107,10 @@ pub trait Storage: Sized { /// Configuration that can be used by any binary that interacts with Stratus storage. #[derive(Parser, DebugAsJson, Clone, serde::Serialize)] -pub struct StratusStorageConfig { +pub struct StorageConfig { + #[arg(long = "storage-kind", env = "STORAGE_KIND", default_value = "stratus-storage")] + pub storage_kind: StorageKind, + #[clap(flatten)] pub temp_storage: TemporaryStorageConfig, @@ -112,13 +118,33 @@ pub struct StratusStorageConfig { pub perm_storage: PermanentStorageConfig, } -impl StratusStorageConfig { +impl StorageConfig { /// Initializes Stratus storage. pub fn init(&self) -> Result, StratusError> { let temp_storage = self.temp_storage.init()?; let perm_storage = self.perm_storage.init()?; + let StorageKind::StratusStorage = self.storage_kind; let storage = StratusStorage::new(temp_storage, perm_storage)?; Ok(Arc::new(storage)) } } + +#[derive(DebugAsJson, strum::Display, strum::VariantNames, Parser, Clone, serde::Serialize)] +pub enum StorageKind { + #[serde(rename = "stratus-storage")] + #[strum(to_string = "stratus-storage")] + StratusStorage, +} + +impl FromStr for StorageKind { + type Err = anyhow::Error; + + fn from_str(s: &str) -> anyhow::Result { + let s = s.trim().to_lowercase(); + match s.as_ref() { + "stratus-storage" | "stratus_storage" => Ok(Self::StratusStorage), + s => Err(anyhow!("unknown storage kind: \"{}\" - valid values are {:?}", s, Self::VARIANTS)), + } + } +}