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;
32#[cfg(feature = "webdav")]
33pub(crate) mod webdav;
34
35use std::fmt::Debug;
36use std::path::PathBuf;
37use std::sync::mpsc::channel;
38
39#[cfg(feature = "mount")]
40use crate::commands::mount::MountCmd;
41#[cfg(feature = "webdav")]
42use crate::commands::webdav::WebDavCmd;
43use crate::{
44 Application, RUSTIC_APP,
45 commands::{
46 backup::BackupCmd, cat::CatCmd, check::CheckCmd, completions::CompletionsCmd,
47 config::ConfigCmd, copy::CopyCmd, diff::DiffCmd, docs::DocsCmd, dump::DumpCmd,
48 forget::ForgetCmd, init::InitCmd, key::KeyCmd, list::ListCmd, ls::LsCmd, merge::MergeCmd,
49 prune::PruneCmd, repair::RepairCmd, repoinfo::RepoInfoCmd, restore::RestoreCmd,
50 rewrite::RewriteCmd, self_update::SelfUpdateCmd, show_config::ShowConfigCmd,
51 snapshots::SnapshotCmd, tag::TagCmd,
52 },
53 config::RusticConfig,
54};
55
56use abscissa_core::{
57 Command, Configurable, FrameworkError, FrameworkErrorKind, Runnable, Shutdown, config::Override,
58};
59use anyhow::Result;
60use clap::builder::{
61 Styles,
62 styling::{AnsiColor, Effects},
63};
64use convert_case::{Case, Casing};
65use human_panic::setup_panic;
66use log::{Level, info, log};
67use reqwest::Url;
68
69use self::find::FindCmd;
70
71#[derive(clap::Parser, Command, Debug, Runnable)]
74enum RusticCmd {
75 Backup(Box<BackupCmd>),
77
78 Cat(Box<CatCmd>),
80
81 Config(Box<ConfigCmd>),
83
84 Completions(Box<CompletionsCmd>),
86
87 Check(Box<CheckCmd>),
89
90 Copy(Box<CopyCmd>),
92
93 Diff(Box<DiffCmd>),
95
96 Docs(Box<DocsCmd>),
98
99 Dump(Box<DumpCmd>),
101
102 Find(Box<FindCmd>),
104
105 Forget(Box<ForgetCmd>),
107
108 Init(Box<InitCmd>),
110
111 Key(Box<KeyCmd>),
113
114 List(Box<ListCmd>),
116
117 #[cfg(feature = "mount")]
118 Mount(Box<MountCmd>),
120
121 Ls(Box<LsCmd>),
123
124 Merge(Box<MergeCmd>),
126
127 Snapshots(Box<SnapshotCmd>),
129
130 ShowConfig(Box<ShowConfigCmd>),
132
133 #[cfg_attr(not(feature = "self-update"), clap(hide = true))]
135 SelfUpdate(Box<SelfUpdateCmd>),
136
137 Prune(Box<PruneCmd>),
139
140 Restore(Box<RestoreCmd>),
142
143 Rewrite(Box<RewriteCmd>),
145
146 Repair(Box<RepairCmd>),
148
149 Repoinfo(Box<RepoInfoCmd>),
151
152 Tag(Box<TagCmd>),
154
155 #[cfg(feature = "webdav")]
157 Webdav(Box<WebDavCmd>),
158}
159
160fn styles() -> Styles {
161 Styles::styled()
162 .header(AnsiColor::Red.on_default() | Effects::BOLD)
163 .usage(AnsiColor::Red.on_default() | Effects::BOLD)
164 .literal(AnsiColor::Blue.on_default() | Effects::BOLD)
165 .placeholder(AnsiColor::Green.on_default())
166}
167
168fn version() -> &'static str {
169 option_env!("PROJECT_VERSION").unwrap_or(env!("CARGO_PKG_VERSION"))
170}
171
172#[derive(clap::Parser, Command, Debug)]
174#[command(author, about, name="rustic", styles=styles(), version=version())]
175pub struct EntryPoint {
176 #[command(flatten)]
177 pub config: RusticConfig,
178
179 #[command(subcommand)]
180 commands: RusticCmd,
181}
182
183impl Runnable for EntryPoint {
184 fn run(&self) {
185 setup_panic!();
187
188 let (tx, rx) = channel();
190
191 ctrlc::set_handler(move || tx.send(()).expect("Could not send signal on channel."))
192 .expect("Error setting Ctrl-C handler");
193
194 _ = std::thread::spawn(move || {
195 rx.recv().expect("Could not receive from channel.");
197 info!("Ctrl-C received, shutting down...");
198 RUSTIC_APP.shutdown(Shutdown::Graceful)
199 });
200
201 self.commands.run();
203 RUSTIC_APP.shutdown(Shutdown::Graceful)
204 }
205}
206
207impl Configurable<RusticConfig> for EntryPoint {
209 fn config_path(&self) -> Option<PathBuf> {
211 None
214 }
215
216 fn process_config(&self, _config: RusticConfig) -> Result<RusticConfig, FrameworkError> {
219 let mut config = self.config.clone();
223
224 for (var, value) in std::env::vars() {
228 if let Some(var) = var.strip_prefix("RUSTIC_REPO_OPT_") {
229 let var = var.from_case(Case::UpperSnake).to_case(Case::Kebab);
230 _ = config.repository.be.options.insert(var, value);
231 } else if let Some(var) = var.strip_prefix("OPENDAL_") {
232 let var = var.from_case(Case::UpperSnake).to_case(Case::Snake);
233 _ = config.repository.be.options.insert(var, value);
234 } else if let Some(var) = var.strip_prefix("RUSTIC_REPO_OPTHOT_") {
235 let var = var.from_case(Case::UpperSnake).to_case(Case::Kebab);
236 _ = config.repository.be.options_hot.insert(var, value);
237 } else if let Some(var) = var.strip_prefix("RUSTIC_REPO_OPTCOLD_") {
238 let var = var.from_case(Case::UpperSnake).to_case(Case::Kebab);
239 _ = config.repository.be.options_cold.insert(var, value);
240 } else if let Some(var) = var.strip_prefix("OPENDALHOT_") {
241 let var = var.from_case(Case::UpperSnake).to_case(Case::Snake);
242 _ = config.repository.be.options_hot.insert(var, value);
243 } else if let Some(var) = var.strip_prefix("OPENDALCOLD_") {
244 let var = var.from_case(Case::UpperSnake).to_case(Case::Snake);
245 _ = config.repository.be.options_cold.insert(var, value);
246 } else if var == "OTEL_EXPORTER_OTLP_METRICS_ENDPOINT" {
247 #[cfg(feature = "opentelemetry")]
248 if let Ok(url) = Url::parse(&value) {
249 _ = config.global.opentelemetry.insert(url);
250 }
251 } else if var == "OTEL_SERVICE_NAME" && cfg!(feature = "opentelemetry") {
252 _ = config.backup.metrics_job.insert(value);
253 }
254 }
255
256 let mut merge_logs = Vec::new();
258
259 if config.global.use_profiles.is_empty() {
261 config.merge_profile("rustic", &mut merge_logs, Level::Info)?;
262 } else {
263 for profile in &config.global.use_profiles.clone() {
264 config.merge_profile(profile, &mut merge_logs, Level::Warn)?;
265 }
266 }
267
268 config
270 .global
271 .logging_options
272 .start_logger(config.global.dry_run)
273 .map_err(|e| FrameworkErrorKind::ConfigError.context(e))?;
274
275 if config.global.logging_options.log_file.is_some() {
276 info!("rustic {}", version());
277 info!("command: {:?}", std::env::args_os().collect::<Vec<_>>());
278 }
279
280 for (level, merge_log) in merge_logs {
282 log!(level, "{merge_log}");
283 }
284
285 match &self.commands {
286 RusticCmd::Forget(cmd) => cmd.override_config(config),
287 RusticCmd::Copy(cmd) => cmd.override_config(config),
288 #[cfg(feature = "webdav")]
289 RusticCmd::Webdav(cmd) => cmd.override_config(config),
290 #[cfg(feature = "mount")]
291 RusticCmd::Mount(cmd) => cmd.override_config(config),
292
293 _ => Ok(config),
295 }
296 }
297}
298
299#[cfg(test)]
300mod tests {
301 use crate::commands::EntryPoint;
302 use clap::CommandFactory;
303
304 #[test]
305 fn verify_cli() {
306 EntryPoint::command().debug_assert();
307 }
308}