wick_component_cli/
options.rs1use std::net::Ipv4Addr;
2use std::path::PathBuf;
3
4use clap::Args;
5use serde::{Deserialize, Serialize};
6
7#[derive(Debug)]
8#[non_exhaustive]
10pub struct Options {
11 pub rpc: Option<ServerOptions>,
13 pub id: String,
15}
16
17impl Options {
18 pub fn new<T: Into<String>>(id: T, rpc_options: Option<ServerOptions>) -> Self {
20 Self {
21 id: id.into(),
22 rpc: rpc_options,
23 }
24 }
25}
26
27impl Default for Options {
28 fn default() -> Self {
29 Self {
30 id: uuid::Uuid::new_v4().as_hyphenated().to_string(),
31 rpc: Default::default(),
32 }
33 }
34}
35
36#[derive(Debug, Default, Clone)]
37#[non_exhaustive]
39pub struct MeshOptions {
40 pub enabled: bool,
42
43 pub address: String,
45
46 pub creds_path: Option<PathBuf>,
48
49 pub token: Option<String>,
51}
52
53#[allow(missing_copy_implementations)]
54#[derive(Debug, Clone, Default, derive_builder::Builder)]
55#[builder(default)]
56#[non_exhaustive]
58pub struct ServerOptions {
59 pub enabled: bool,
61
62 pub port: Option<u16>,
64
65 pub address: Option<Ipv4Addr>,
67
68 #[cfg(feature = "grpc")]
70 pub pem: Option<wick_config::AssetReference>,
71
72 #[cfg(feature = "grpc")]
74 pub key: Option<wick_config::AssetReference>,
75
76 #[cfg(feature = "grpc")]
78 pub ca: Option<wick_config::AssetReference>,
79}
80
81#[allow(clippy::expect_used)]
82impl From<DefaultCliOptions> for Options {
83 fn from(opts: DefaultCliOptions) -> Self {
84 let rpc = Some(ServerOptions {
85 enabled: opts.rpc_enabled,
86 port: opts.rpc_port,
87 address: opts.rpc_address,
88 #[cfg(feature = "grpc")]
89 pem: opts.rpc_pem.map(wick_config::AssetReference::new),
90 #[cfg(feature = "grpc")]
91 key: opts.rpc_key.map(wick_config::AssetReference::new),
92 #[cfg(feature = "grpc")]
93 ca: opts.rpc_ca.map(wick_config::AssetReference::new),
94 });
95
96 let id = opts
97 .id
98 .unwrap_or_else(|| uuid::Uuid::new_v4().as_hyphenated().to_string());
99
100 Options { rpc, id }
101 }
102}
103
104pub mod env {
106 macro_rules! env_var {
107 ( $x:ident ) => {
108 pub const $x: &str = stringify!($x);
110 };
111 }
112
113 env_var!(WICK_COLLECTION_ID);
114 env_var!(WICK_TIMEOUT);
115
116 env_var!(WICK_RPC_ENABLED);
117 env_var!(WICK_RPC_PORT);
118 env_var!(WICK_RPC_ADDRESS);
119 env_var!(WICK_RPC_KEY);
120 env_var!(WICK_RPC_PEM);
121 env_var!(WICK_RPC_CA);
122
123 env_var!(NATS_URL);
124 env_var!(NATS_CREDSFILE);
125 env_var!(NATS_TOKEN);
126}
127
128#[derive(Debug, Clone, Default, Args, Serialize, Deserialize)]
129#[non_exhaustive]
131pub struct DefaultCliOptions {
132 #[clap(long = "id", env = env::WICK_COLLECTION_ID, action)]
134 pub id: Option<String>,
135
136 #[clap(long = "timeout", env = env::WICK_TIMEOUT, action)]
138 pub timeout: Option<u64>,
139
140 #[clap(long = "rpc", env = env::WICK_RPC_ENABLED, action)]
142 pub rpc_enabled: bool,
143
144 #[clap(long = "rpc-port", env = env::WICK_RPC_PORT, action)]
146 pub rpc_port: Option<u16>,
147
148 #[clap(long = "rpc-address", env = env::WICK_RPC_ADDRESS, action)]
150 pub rpc_address: Option<Ipv4Addr>,
151
152 #[clap(long = "rpc-pem", env = env::WICK_RPC_PEM, action)]
154 pub rpc_pem: Option<String>,
155
156 #[clap(long = "rpc-key", env = env::WICK_RPC_KEY, action)]
158 pub rpc_key: Option<String>,
159
160 #[clap(long = "rpc-ca", env = env::WICK_RPC_CA, action)]
162 pub rpc_ca: Option<String>,
163}