From 0998ddb836ed76d74ff1e7cd0a00ac86c8d4b1f4 Mon Sep 17 00:00:00 2001 From: Alexander Weiss Date: Sun, 26 Mar 2023 22:28:33 +0200 Subject: [PATCH] repository: Use location in log --- src/backend/cache.rs | 2 +- src/backend/choose.rs | 2 +- src/backend/decrypt.rs | 2 +- src/backend/dry_run.rs | 2 +- src/backend/hotcold.rs | 2 +- src/backend/local.rs | 6 ++++-- src/backend/mod.rs | 2 +- src/backend/rclone.rs | 17 +++++++++++------ src/backend/rest.rs | 10 ++++++++-- src/repository/mod.rs | 6 +++--- 10 files changed, 32 insertions(+), 19 deletions(-) diff --git a/src/backend/cache.rs b/src/backend/cache.rs index 39f4fff..24d15ab 100644 --- a/src/backend/cache.rs +++ b/src/backend/cache.rs @@ -24,7 +24,7 @@ impl CachedBackend { } impl ReadBackend for CachedBackend { - fn location(&self) -> &str { + fn location(&self) -> String { self.be.location() } diff --git a/src/backend/choose.rs b/src/backend/choose.rs index 5a67227..2c152a7 100644 --- a/src/backend/choose.rs +++ b/src/backend/choose.rs @@ -28,7 +28,7 @@ impl ChooseBackend { } impl ReadBackend for ChooseBackend { - fn location(&self) -> &str { + fn location(&self) -> String { match self { Local(local) => local.location(), Rest(rest) => rest.location(), diff --git a/src/backend/decrypt.rs b/src/backend/decrypt.rs index 5d542b0..6014786 100644 --- a/src/backend/decrypt.rs +++ b/src/backend/decrypt.rs @@ -168,7 +168,7 @@ impl DecryptReadBackend for DecryptBackend { } impl ReadBackend for DecryptBackend { - fn location(&self) -> &str { + fn location(&self) -> String { self.backend.location() } diff --git a/src/backend/dry_run.rs b/src/backend/dry_run.rs index 24b8efd..0c1e511 100644 --- a/src/backend/dry_run.rs +++ b/src/backend/dry_run.rs @@ -25,7 +25,7 @@ impl DecryptReadBackend for DryRunBackend { } impl ReadBackend for DryRunBackend { - fn location(&self) -> &str { + fn location(&self) -> String { self.be.location() } diff --git a/src/backend/hotcold.rs b/src/backend/hotcold.rs index 26125e6..2d06b44 100644 --- a/src/backend/hotcold.rs +++ b/src/backend/hotcold.rs @@ -16,7 +16,7 @@ impl HotColdBackend { } impl ReadBackend for HotColdBackend { - fn location(&self) -> &str { + fn location(&self) -> String { self.be.location() } diff --git a/src/backend/local.rs b/src/backend/local.rs index 36a8980..2ce4e37 100644 --- a/src/backend/local.rs +++ b/src/backend/local.rs @@ -74,8 +74,10 @@ impl LocalBackend { } impl ReadBackend for LocalBackend { - fn location(&self) -> &str { - self.path.to_str().unwrap() + fn location(&self) -> String { + let mut location = "local:".to_string(); + location.push_str(&self.path.to_string_lossy()); + location } fn set_option(&mut self, option: &str, value: &str) -> Result<()> { diff --git a/src/backend/mod.rs b/src/backend/mod.rs index 7aebcbb..1d03690 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -72,7 +72,7 @@ pub trait RepoFile: Serialize + DeserializeOwned + Sized + Send + Sync + 'static } pub trait ReadBackend: Clone + Send + Sync + 'static { - fn location(&self) -> &str; + fn location(&self) -> String; fn set_option(&mut self, option: &str, value: &str) -> Result<()>; diff --git a/src/backend/rclone.rs b/src/backend/rclone.rs index 4a6ff64..48a2cf6 100644 --- a/src/backend/rclone.rs +++ b/src/backend/rclone.rs @@ -22,6 +22,7 @@ impl Drop for ChildToKill { #[derive(Clone)] pub struct RcloneBackend { rest: RestBackend, + url: String, _child_data: Arc, } @@ -80,7 +81,7 @@ impl RcloneBackend { .take() .ok_or_else(|| anyhow!("cannot get stdout of rclone"))?, ); - let url = loop { + let rest_url = loop { if let Some(status) = child.try_wait()? { bail!("rclone exited with {status}"); } @@ -110,24 +111,28 @@ impl RcloneBackend { } }); - if !url.starts_with("http://") { + if !rest_url.starts_with("http://") { bail!("url must start with http://! url: {url}"); } - let url = "http://".to_string() + user.as_str() + ":" + password.as_str() + "@" + &url[7..]; + let rest_url = + "http://".to_string() + user.as_str() + ":" + password.as_str() + "@" + &rest_url[7..]; debug!("using REST backend with url {url}."); - let rest = RestBackend::new(&url)?; + let rest = RestBackend::new(&rest_url)?; Ok(Self { _child_data: Arc::new(ChildToKill(child)), + url: url.to_string(), rest, }) } } impl ReadBackend for RcloneBackend { - fn location(&self) -> &str { - self.rest.location() + fn location(&self) -> String { + let mut location = "rclone:".to_string(); + location.push_str(&self.url); + location } fn set_option(&mut self, option: &str, value: &str) -> Result<()> { diff --git a/src/backend/rest.rs b/src/backend/rest.rs index aa83ffe..12b3b49 100644 --- a/src/backend/rest.rs +++ b/src/backend/rest.rs @@ -101,8 +101,14 @@ impl RestBackend { } impl ReadBackend for RestBackend { - fn location(&self) -> &str { - self.url.as_str() + fn location(&self) -> String { + let mut location = "rest:".to_string(); + let mut url = self.url.clone(); + if url.password().is_some() { + url.set_password(Some("***")).unwrap(); + } + location.push_str(url.as_str()); + location } fn set_option(&mut self, option: &str, value: &str) -> Result<()> { diff --git a/src/repository/mod.rs b/src/repository/mod.rs index c31b84b..3d50448 100644 --- a/src/repository/mod.rs +++ b/src/repository/mod.rs @@ -159,10 +159,10 @@ impl Repository { for (opt, value) in &opts.options { be.set_option(opt, value)?; } - let mut name = opts.repository.as_ref().unwrap().clone(); - if let Some(repo_hot) = &opts.repo_hot { + let mut name = be.location(); + if let Some(be_hot) = &be_hot { name.push('#'); - name.push_str(repo_hot); + name.push_str(&be_hot.location()); } Ok(Self {