use std::net::Ipv4Addr;
use std::path::PathBuf;
use clap::Args;
use serde::{Deserialize, Serialize};
#[derive(Debug)]
#[non_exhaustive]
pub struct Options {
pub rpc: Option<ServerOptions>,
pub id: String,
}
impl Options {
pub fn new<T: Into<String>>(id: T, rpc_options: Option<ServerOptions>) -> Self {
Self {
id: id.into(),
rpc: rpc_options,
}
}
}
impl Default for Options {
fn default() -> Self {
Self {
id: uuid::Uuid::new_v4().as_hyphenated().to_string(),
rpc: Default::default(),
}
}
}
#[derive(Debug, Default, Clone)]
#[non_exhaustive]
pub struct MeshOptions {
pub enabled: bool,
pub address: String,
pub creds_path: Option<PathBuf>,
pub token: Option<String>,
}
#[allow(missing_copy_implementations)]
#[derive(Debug, Clone, Default, derive_builder::Builder)]
#[builder(default)]
#[non_exhaustive]
pub struct ServerOptions {
pub enabled: bool,
pub port: Option<u16>,
pub address: Option<Ipv4Addr>,
#[cfg(feature = "grpc")]
pub pem: Option<wick_config::AssetReference>,
#[cfg(feature = "grpc")]
pub key: Option<wick_config::AssetReference>,
#[cfg(feature = "grpc")]
pub ca: Option<wick_config::AssetReference>,
}
#[allow(clippy::expect_used)]
impl From<DefaultCliOptions> for Options {
fn from(opts: DefaultCliOptions) -> Self {
let rpc = Some(ServerOptions {
enabled: opts.rpc_enabled,
port: opts.rpc_port,
address: opts.rpc_address,
#[cfg(feature = "grpc")]
pem: opts.rpc_pem.map(wick_config::AssetReference::new),
#[cfg(feature = "grpc")]
key: opts.rpc_key.map(wick_config::AssetReference::new),
#[cfg(feature = "grpc")]
ca: opts.rpc_ca.map(wick_config::AssetReference::new),
});
let id = opts
.id
.unwrap_or_else(|| uuid::Uuid::new_v4().as_hyphenated().to_string());
Options { rpc, id }
}
}
pub mod env {
macro_rules! env_var {
( $x:ident ) => {
pub const $x: &str = stringify!($x);
};
}
env_var!(WICK_COLLECTION_ID);
env_var!(WICK_TIMEOUT);
env_var!(WICK_RPC_ENABLED);
env_var!(WICK_RPC_PORT);
env_var!(WICK_RPC_ADDRESS);
env_var!(WICK_RPC_KEY);
env_var!(WICK_RPC_PEM);
env_var!(WICK_RPC_CA);
env_var!(NATS_URL);
env_var!(NATS_CREDSFILE);
env_var!(NATS_TOKEN);
}
#[derive(Debug, Clone, Default, Args, Serialize, Deserialize)]
#[non_exhaustive]
pub struct DefaultCliOptions {
#[clap(long = "id", env = env::WICK_COLLECTION_ID, action)]
pub id: Option<String>,
#[clap(long = "timeout", env = env::WICK_TIMEOUT, action)]
pub timeout: Option<u64>,
#[clap(long = "rpc", env = env::WICK_RPC_ENABLED, action)]
pub rpc_enabled: bool,
#[clap(long = "rpc-port", env = env::WICK_RPC_PORT, action)]
pub rpc_port: Option<u16>,
#[clap(long = "rpc-address", env = env::WICK_RPC_ADDRESS, action)]
pub rpc_address: Option<Ipv4Addr>,
#[clap(long = "rpc-pem", env = env::WICK_RPC_PEM, action)]
pub rpc_pem: Option<String>,
#[clap(long = "rpc-key", env = env::WICK_RPC_KEY, action)]
pub rpc_key: Option<String>,
#[clap(long = "rpc-ca", env = env::WICK_RPC_CA, action)]
pub rpc_ca: Option<String>,
}