wick_component_cli/
options.rs

1use std::net::Ipv4Addr;
2use std::path::PathBuf;
3
4use clap::Args;
5use serde::{Deserialize, Serialize};
6
7#[derive(Debug)]
8/// Server configuration options.
9#[non_exhaustive]
10pub struct Options {
11  /// RPC server options.
12  pub rpc: Option<ServerOptions>,
13  /// The ID of the server.
14  pub id: String,
15}
16
17impl Options {
18  /// Create a new [Options] with the specified ID.
19  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/// Configuration used to connect to the mesh
38#[non_exhaustive]
39pub struct MeshOptions {
40  /// Enable/disable the mesh connection.
41  pub enabled: bool,
42
43  /// The address of the NATS server.
44  pub address: String,
45
46  /// The path to the NATS credsfile.
47  pub creds_path: Option<PathBuf>,
48
49  /// The NATS token.
50  pub token: Option<String>,
51}
52
53#[allow(missing_copy_implementations)]
54#[derive(Debug, Clone, Default, derive_builder::Builder)]
55#[builder(default)]
56/// Options to use when starting an RPC or HTTP server.
57#[non_exhaustive]
58pub struct ServerOptions {
59  /// Enable/disable the server.
60  pub enabled: bool,
61
62  /// The port to bind to.
63  pub port: Option<u16>,
64
65  /// The address to bind to.
66  pub address: Option<Ipv4Addr>,
67
68  /// Path to pem file for TLS.
69  #[cfg(feature = "grpc")]
70  pub pem: Option<wick_config::AssetReference>,
71
72  /// Path to key file for TLS.
73  #[cfg(feature = "grpc")]
74  pub key: Option<wick_config::AssetReference>,
75
76  /// Path to CA file.
77  #[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
104/// Names of the environment variables used for fallback values.
105pub mod env {
106  macro_rules! env_var {
107    (  $x:ident  ) => {
108      /// Environment variable fallback for CLI options
109      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/// Command line options for s.
130#[non_exhaustive]
131pub struct DefaultCliOptions {
132  /// The unique ID of this client.
133  #[clap(long = "id", env = env::WICK_COLLECTION_ID, action)]
134  pub id: Option<String>,
135
136  /// The timeout for operations in ms.
137  #[clap(long = "timeout", env = env::WICK_TIMEOUT, action)]
138  pub timeout: Option<u64>,
139
140  /// Enable the rpc server.
141  #[clap(long = "rpc",  env = env::WICK_RPC_ENABLED, action)]
142  pub rpc_enabled: bool,
143
144  /// Port to listen on for GRPC server.
145  #[clap(long = "rpc-port", env = env::WICK_RPC_PORT, action)]
146  pub rpc_port: Option<u16>,
147
148  /// IP address to bind to for GRPC server.
149  #[clap(long = "rpc-address", env = env::WICK_RPC_ADDRESS, action)]
150  pub rpc_address: Option<Ipv4Addr>,
151
152  /// Path to pem file for TLS for GRPC server.
153  #[clap(long = "rpc-pem", env = env::WICK_RPC_PEM, action)]
154  pub rpc_pem: Option<String>,
155
156  /// Path to key file for TLS for GRPC server.
157  #[clap(long = "rpc-key", env = env::WICK_RPC_KEY, action)]
158  pub rpc_key: Option<String>,
159
160  /// Path to certificate authority for GRPC server.
161  #[clap(long = "rpc-ca", env = env::WICK_RPC_CA, action)]
162  pub rpc_ca: Option<String>,
163}