1pub(crate) mod backup;
4pub(crate) mod cat;
5pub(crate) mod check;
6pub(crate) mod completions;
7pub(crate) mod config;
8pub(crate) mod copy;
9pub(crate) mod diff;
10pub(crate) mod docs;
11pub(crate) mod dump;
12pub(crate) mod find;
13pub(crate) mod forget;
14pub(crate) mod init;
15pub(crate) mod key;
16pub(crate) mod list;
17pub(crate) mod ls;
18pub(crate) mod merge;
19#[cfg(feature = "mount")]
20pub(crate) mod mount;
21pub(crate) mod prune;
22pub(crate) mod repair;
23pub(crate) mod repoinfo;
24pub(crate) mod restore;
25pub(crate) mod rewrite;
26pub(crate) mod self_update;
27pub(crate) mod show_config;
28pub(crate) mod snapshots;
29pub(crate) mod tag;
30#[cfg(feature = "tui")]
31pub(crate) mod tui;
32pub(crate) mod version;
33#[cfg(feature = "webdav")]
34pub(crate) mod webdav;
35
36use std::fmt::Debug;
37use std::path::PathBuf;
38use std::sync::mpsc::channel;
39
40#[cfg(feature = "mount")]
41use crate::commands::mount::MountCmd;
42#[cfg(feature = "webdav")]
43use crate::commands::webdav::WebDavCmd;
44use crate::{
45 Application, RUSTIC_APP,
46 commands::{
47 backup::BackupCmd, cat::CatCmd, check::CheckCmd, completions::CompletionsCmd,
48 config::ConfigCmd, copy::CopyCmd, diff::DiffCmd, docs::DocsCmd, dump::DumpCmd,
49 forget::ForgetCmd, init::InitCmd, key::KeyCmd, list::ListCmd, ls::LsCmd, merge::MergeCmd,
50 prune::PruneCmd, repair::RepairCmd, repoinfo::RepoInfoCmd, restore::RestoreCmd,
51 rewrite::RewriteCmd, self_update::SelfUpdateCmd, show_config::ShowConfigCmd,
52 snapshots::SnapshotCmd, tag::TagCmd,
53 },
54 config::RusticConfig,
55};
56
57use abscissa_core::{
58 Command, Configurable, FrameworkError, FrameworkErrorKind, Runnable, Shutdown, config::Override,
59};
60use anyhow::Result;
61use clap::builder::{
62 Styles,
63 styling::{AnsiColor, Effects},
64};
65use convert_case::{Case, Casing};
66use human_panic::setup_panic;
67use log::{Level, info, log};
68use reqwest::Url;
69
70use self::find::FindCmd;
71
72#[derive(clap::Parser, Command, Debug, Runnable)]
75enum RusticCmd {
76 Backup(Box<BackupCmd>),
78
79 Cat(Box<CatCmd>),
81
82 Config(Box<ConfigCmd>),
84
85 Completions(Box<CompletionsCmd>),
87
88 Check(Box<CheckCmd>),
90
91 Copy(Box<CopyCmd>),
93
94 Diff(Box<DiffCmd>),
96
97 Docs(Box<DocsCmd>),
99
100 Dump(Box<DumpCmd>),
102
103 Find(Box<FindCmd>),
105
106 Forget(Box<ForgetCmd>),
108
109 Init(Box<InitCmd>),
111
112 Key(Box<KeyCmd>),
114
115 List(Box<ListCmd>),
117
118 #[cfg(feature = "mount")]
119 Mount(Box<MountCmd>),
121
122 Ls(Box<LsCmd>),
124
125 Merge(Box<MergeCmd>),
127
128 Snapshots(Box<SnapshotCmd>),
130
131 ShowConfig(Box<ShowConfigCmd>),
133
134 #[cfg_attr(not(feature = "self-update"), clap(hide = true))]
136 SelfUpdate(Box<SelfUpdateCmd>),
137
138 Prune(Box<PruneCmd>),
140
141 Restore(Box<RestoreCmd>),
143
144 Rewrite(Box<RewriteCmd>),
146
147 Repair(Box<RepairCmd>),
149
150 Repoinfo(Box<RepoInfoCmd>),
152
153 Tag(Box<TagCmd>),
155
156 #[cfg(feature = "webdav")]
158 Webdav(Box<WebDavCmd>),
159
160 Version(Box<version::VersionCmd>),
162}
163
164fn styles() -> Styles {
165 Styles::styled()
166 .header(AnsiColor::Red.on_default() | Effects::BOLD)
167 .usage(AnsiColor::Red.on_default() | Effects::BOLD)
168 .literal(AnsiColor::Blue.on_default() | Effects::BOLD)
169 .placeholder(AnsiColor::Green.on_default())
170}
171
172fn version() -> &'static str {
173 option_env!("PROJECT_VERSION").unwrap_or(env!("CARGO_PKG_VERSION"))
174}
175
176#[derive(clap::Parser, Command, Debug)]
178#[command(author, about, name="rustic", styles=styles(), version=version())]
179pub struct EntryPoint {
180 #[command(flatten)]
181 pub config: RusticConfig,
182
183 #[command(subcommand)]
184 commands: RusticCmd,
185}
186
187impl Runnable for EntryPoint {
188 fn run(&self) {
189 setup_panic!();
191
192 let (tx, rx) = channel();
194
195 ctrlc::set_handler(move || tx.send(()).expect("Could not send signal on channel."))
196 .expect("Error setting Ctrl-C handler");
197
198 _ = std::thread::spawn(move || {
199 rx.recv().expect("Could not receive from channel.");
201 info!("Ctrl-C received, shutting down...");
202 RUSTIC_APP.shutdown(Shutdown::Graceful)
203 });
204
205 self.commands.run();
207 RUSTIC_APP.shutdown(Shutdown::Graceful)
208 }
209}
210
211impl Configurable<RusticConfig> for EntryPoint {
213 fn config_path(&self) -> Option<PathBuf> {
215 None
218 }
219
220 fn process_config(&self, _config: RusticConfig) -> Result<RusticConfig, FrameworkError> {
223 let mut config = self.config.clone();
227
228 for (var, value) in std::env::vars() {
232 if let Some(var) = var.strip_prefix("RUSTIC_REPO_OPT_") {
233 let var = var.from_case(Case::UpperSnake).to_case(Case::Kebab);
234 _ = config.repository.be.options.insert(var, value);
235 } else if let Some(var) = var.strip_prefix("OPENDAL_") {
236 let var = var.from_case(Case::UpperSnake).to_case(Case::Snake);
237 _ = config.repository.be.options.insert(var, value);
238 } else if let Some(var) = var.strip_prefix("RUSTIC_REPO_OPTHOT_") {
239 let var = var.from_case(Case::UpperSnake).to_case(Case::Kebab);
240 _ = config.repository.be.options_hot.insert(var, value);
241 } else if let Some(var) = var.strip_prefix("RUSTIC_REPO_OPTCOLD_") {
242 let var = var.from_case(Case::UpperSnake).to_case(Case::Kebab);
243 _ = config.repository.be.options_cold.insert(var, value);
244 } else if let Some(var) = var.strip_prefix("OPENDALHOT_") {
245 let var = var.from_case(Case::UpperSnake).to_case(Case::Snake);
246 _ = config.repository.be.options_hot.insert(var, value);
247 } else if let Some(var) = var.strip_prefix("OPENDALCOLD_") {
248 let var = var.from_case(Case::UpperSnake).to_case(Case::Snake);
249 _ = config.repository.be.options_cold.insert(var, value);
250 } else if var == "OTEL_EXPORTER_OTLP_METRICS_ENDPOINT" {
251 #[cfg(feature = "opentelemetry")]
252 if let Ok(url) = Url::parse(&value) {
253 _ = config.global.opentelemetry.insert(url);
254 }
255 } else if var == "OTEL_SERVICE_NAME" && cfg!(feature = "opentelemetry") {
256 _ = config.backup.metrics_job.insert(value);
257 }
258 }
259
260 let mut merge_logs = Vec::new();
262
263 if config.global.use_profiles.is_empty() {
265 config.merge_profile("rustic", &mut merge_logs, Level::Info)?;
266 } else {
267 for profile in &config.global.use_profiles.clone() {
268 config.merge_profile(profile, &mut merge_logs, Level::Warn)?;
269 }
270 }
271
272 if !matches!(self.commands, RusticCmd::Version(_)) {
275 config
276 .global
277 .logging_options
278 .start_logger(config.global.dry_run)
279 .map_err(|e| FrameworkErrorKind::ConfigError.context(e))?;
280
281 if config.global.logging_options.log_file.is_some() {
282 info!("rustic {}", version());
283 info!("command: {:?}", std::env::args_os().collect::<Vec<_>>());
284 }
285
286 for (level, merge_log) in merge_logs {
288 log!(level, "{merge_log}");
289 }
290 }
291
292 match &self.commands {
293 RusticCmd::Forget(cmd) => cmd.override_config(config),
294 RusticCmd::Copy(cmd) => cmd.override_config(config),
295 #[cfg(feature = "webdav")]
296 RusticCmd::Webdav(cmd) => cmd.override_config(config),
297 #[cfg(feature = "mount")]
298 RusticCmd::Mount(cmd) => cmd.override_config(config),
299
300 _ => Ok(config),
302 }
303 }
304}
305
306#[cfg(test)]
307mod tests {
308 use crate::commands::EntryPoint;
309 use clap::CommandFactory;
310
311 #[test]
312 fn verify_cli() {
313 EntryPoint::command().debug_assert();
314 }
315}