1pub(crate) mod hooks;
8pub(crate) mod logging;
9pub(crate) mod progress_options;
10
11use std::{
12 collections::BTreeMap,
13 fmt::{self, Display, Formatter},
14 path::PathBuf,
15};
16
17use abscissa_core::{FrameworkError, FrameworkErrorKind, config::Config, path::AbsPathBuf};
18use anyhow::{Result, anyhow};
19use clap::{Parser, ValueHint};
20use conflate::Merge;
21use directories::ProjectDirs;
22use itertools::Itertools;
23use jiff::{Timestamp, Zoned, tz::TimeZone};
24use log::Level;
25use reqwest::Url;
26use rustic_core::SnapshotGroupCriterion;
27use serde::{Deserialize, Serialize};
28use serde_with::{DisplayFromStr, serde_as};
29#[cfg(not(all(feature = "mount", feature = "webdav")))]
30use toml::Value;
31
32#[cfg(feature = "mount")]
33use crate::commands::mount::MountCmd;
34#[cfg(feature = "webdav")]
35use crate::commands::webdav::WebDavCmd;
36
37use crate::{
38 commands::{backup::BackupCmd, copy::CopyCmd, forget::ForgetOptions},
39 config::{hooks::Hooks, logging::LoggingOptions, progress_options::ProgressOptions},
40 filtering::SnapshotFilter,
41 repository::AllRepositoryOptions,
42};
43
44#[derive(Clone, Default, Debug, Parser, Deserialize, Serialize, Merge)]
51#[serde(default, rename_all = "kebab-case", deny_unknown_fields)]
52pub struct RusticConfig {
53 #[clap(flatten, next_help_heading = "Global options")]
55 pub global: GlobalOptions,
56
57 #[clap(flatten, next_help_heading = "Repository options")]
59 pub repository: AllRepositoryOptions,
60
61 #[clap(flatten, next_help_heading = "Snapshot filter options")]
63 pub snapshot_filter: SnapshotFilter,
64
65 #[clap(skip)]
67 pub backup: BackupCmd,
68
69 #[clap(skip)]
71 pub copy: CopyCmd,
72
73 #[clap(skip)]
75 pub forget: ForgetOptions,
76
77 #[cfg(feature = "mount")]
79 #[clap(skip)]
80 pub mount: MountCmd,
81 #[cfg(not(feature = "mount"))]
82 #[clap(skip)]
83 #[merge(skip)]
84 pub mount: Option<Value>,
85
86 #[cfg(feature = "webdav")]
88 #[clap(skip)]
89 pub webdav: WebDavCmd,
90 #[cfg(not(feature = "webdav"))]
91 #[clap(skip)]
92 #[merge(skip)]
93 pub webdav: Option<Value>,
94}
95
96impl Display for RusticConfig {
97 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
98 let config = toml::to_string_pretty(self)
99 .unwrap_or_else(|_| "<Error serializing config>".to_string());
100
101 write!(f, "{config}",)
102 }
103}
104
105impl RusticConfig {
106 pub fn merge_profile(
115 &mut self,
116 profile: &str,
117 merge_logs: &mut Vec<(Level, String)>,
118 level_missing: Level,
119 ) -> Result<(), FrameworkError> {
120 let profile_filename = profile.to_string() + ".toml";
121 let paths = get_config_paths(&profile_filename);
122
123 if let Some(path) = paths.iter().find(|path| path.exists()) {
124 merge_logs.push((Level::Info, format!("using config {}", path.display())));
125 let config_content = std::fs::read_to_string(AbsPathBuf::canonicalize(path)?)?;
126 let config_content = if self.global.profile_substitute_env {
127 subst::substitute(&config_content, &subst::Env).map_err(|e| {
128 abscissa_core::error::context::Context::new(
129 FrameworkErrorKind::ParseError,
130 Some(Box::new(e)),
131 )
132 })?
133 } else {
134 config_content
135 };
136 let mut config = Self::load_toml(config_content)?;
137 for profile in &config.global.use_profiles.clone() {
139 config.merge_profile(profile, merge_logs, Level::Warn)?;
140 }
141 self.merge(config);
142 } else {
143 let paths_string = paths.iter().map(|path| path.display()).join(", ");
144 merge_logs.push((
145 level_missing,
146 format!(
147 "using no config file, none of these exist: {}",
148 &paths_string
149 ),
150 ));
151 };
152 Ok(())
153 }
154}
155
156#[serde_as]
160#[derive(Default, Debug, Parser, Clone, Deserialize, Serialize, Merge)]
161#[serde(default, rename_all = "kebab-case", deny_unknown_fields)]
162pub struct GlobalOptions {
163 #[clap(long, global = true, env = "RUSTIC_PROFILE_SUBSTITUTE_ENV")]
165 #[merge(strategy=conflate::bool::overwrite_false)]
166 pub profile_substitute_env: bool,
167
168 #[clap(
171 short = 'P',
172 long = "use-profile",
173 global = true,
174 value_name = "PROFILE",
175 env = "RUSTIC_USE_PROFILE"
176 )]
177 #[merge(strategy=conflate::vec::append)]
178 pub use_profiles: Vec<String>,
179
180 #[clap(
182 long,
183 short = 'g',
184 global = true,
185 value_name = "CRITERION",
186 env = "RUSTIC_GROUP_BY"
187 )]
188 #[serde_as(as = "Option<DisplayFromStr>")]
189 #[merge(strategy=conflate::option::overwrite_none)]
190 pub group_by: Option<SnapshotGroupCriterion>,
191
192 #[clap(long, short = 'n', global = true, env = "RUSTIC_DRY_RUN")]
194 #[merge(strategy=conflate::bool::overwrite_false)]
195 pub dry_run: bool,
196
197 #[clap(long, global = true, env = "RUSTIC_DRY_RUN_WARMUP")]
199 #[merge(strategy=conflate::bool::overwrite_false)]
200 pub dry_run_warmup: bool,
201
202 #[clap(long, global = true, env = "RUSTIC_CHECK_INDEX")]
204 #[merge(strategy=conflate::bool::overwrite_false)]
205 pub check_index: bool,
206
207 #[clap(flatten)]
209 #[serde(flatten)]
210 pub logging_options: LoggingOptions,
211
212 #[clap(flatten)]
214 #[serde(flatten)]
215 pub progress_options: ProgressOptions,
216
217 #[clap(skip)]
219 pub hooks: Hooks,
220
221 #[clap(skip)]
223 #[merge(strategy = conflate::btreemap::append_or_ignore)]
224 pub env: BTreeMap<String, String>,
225
226 #[serde_as(as = "Option<DisplayFromStr>")]
228 #[clap(long, global = true, env = "RUSTIC_PROMETHEUS", value_name = "PUSHGATEWAY_URL", value_hint = ValueHint::Url)]
229 #[merge(strategy=conflate::option::overwrite_none)]
230 pub prometheus: Option<Url>,
231
232 #[clap(long, value_name = "USER", env = "RUSTIC_PROMETHEUS_USER")]
234 #[merge(strategy=conflate::option::overwrite_none)]
235 pub prometheus_user: Option<String>,
236
237 #[clap(long, value_name = "PASSWORD", env = "RUSTIC_PROMETHEUS_PASS")]
239 #[merge(strategy=conflate::option::overwrite_none)]
240 pub prometheus_pass: Option<String>,
241
242 #[clap(skip)]
244 #[merge(strategy=conflate::btreemap::append_or_ignore)]
245 pub metrics_labels: BTreeMap<String, String>,
246
247 #[serde_as(as = "Option<DisplayFromStr>")]
249 #[clap(long, global = true, env = "RUSTIC_OTEL", value_name = "ENDPOINT_URL", value_hint = ValueHint::Url)]
250 #[merge(strategy=conflate::option::overwrite_none)]
251 pub opentelemetry: Option<Url>,
252
253 #[clap(long, global = true, env = "RUSTIC_SHOW_TIME_OFFSET")]
255 #[merge(strategy=conflate::bool::overwrite_false)]
256 pub show_time_offset: bool,
257}
258
259pub fn parse_labels(s: &str) -> Result<BTreeMap<String, String>> {
260 s.split(',')
261 .filter_map(|s| {
262 let s = s.trim();
263 (!s.is_empty()).then_some(s)
264 })
265 .map(|s| -> Result<_> {
266 let pos = s.find('=').ok_or_else(|| {
267 anyhow!("invalid prometheus label definition: no `=` found in `{s}`")
268 })?;
269 Ok((s[..pos].to_owned(), s[pos + 1..].to_owned()))
270 })
271 .try_collect()
272}
273
274impl GlobalOptions {
275 pub fn is_metrics_configured(&self) -> bool {
276 self.prometheus.is_some() || self.opentelemetry.is_some()
277 }
278
279 pub fn format_timestamp(&self, timestamp: Timestamp) -> String {
280 self.format_time(×tamp.to_zoned(TimeZone::UTC))
281 .to_string()
282 }
283
284 pub fn format_time(&self, time: &Zoned) -> impl Display {
285 if self.show_time_offset {
286 time.strftime("%Y-%m-%d %H:%M:%S%z")
287 } else {
288 let tz = TimeZone::system();
289 if time.offset() == tz.to_offset(time.timestamp()) {
290 time.strftime("%Y-%m-%d %H:%M:%S")
291 } else {
292 time.with_time_zone(tz).strftime("%Y-%m-%d %H:%M:%S*")
293 }
294 }
295 }
296}
297
298fn get_config_paths(filename: &str) -> Vec<PathBuf> {
308 [
309 ProjectDirs::from("", "", "rustic")
310 .map(|project_dirs| project_dirs.config_dir().to_path_buf()),
311 get_global_config_path(),
312 Some(PathBuf::from(".")),
313 ]
314 .into_iter()
315 .filter_map(|path| {
316 path.map(|mut p| {
317 p.push(filename);
318 p
319 })
320 })
321 .collect()
322}
323
324#[cfg(target_os = "windows")]
331fn get_global_config_path() -> Option<PathBuf> {
332 std::env::var_os("PROGRAMDATA").map(|program_data| {
333 let mut path = PathBuf::from(program_data);
334 path.push(r"rustic\config");
335 path
336 })
337}
338
339#[cfg(any(target_os = "ios", target_arch = "wasm32"))]
345fn get_global_config_path() -> Option<PathBuf> {
346 None
347}
348
349#[cfg(not(any(target_os = "windows", target_os = "ios", target_arch = "wasm32")))]
356fn get_global_config_path() -> Option<PathBuf> {
357 Some(PathBuf::from("/etc/rustic"))
358}
359
360#[cfg(test)]
361mod tests {
362 use super::*;
363 use insta::{assert_debug_snapshot, assert_snapshot};
364
365 #[test]
366 fn test_default_config_passes() {
367 let config = RusticConfig::default();
368
369 assert_debug_snapshot!(config);
370 }
371
372 #[test]
373 fn test_default_config_display_passes() {
374 let config = RusticConfig::default();
375
376 assert_snapshot!(config);
377 }
378
379 #[test]
380 fn test_global_env_roundtrip_passes() {
381 let mut config = RusticConfig::default();
382
383 for i in 0..10 {
384 let _ = config
385 .global
386 .env
387 .insert(format!("KEY{i}"), format!("VALUE{i}"));
388 }
389
390 let serialized = toml::to_string(&config).unwrap();
391
392 assert_snapshot!(serialized);
394
395 let deserialized: RusticConfig = toml::from_str(&serialized).unwrap();
396 assert_snapshot!(deserialized);
398
399 assert_debug_snapshot!(deserialized);
401 }
402}