From 40803042b829346ed7522f797d70fd4da20fc12e Mon Sep 17 00:00:00 2001 From: Bela Stoyan Date: Tue, 7 May 2024 07:08:36 +0200 Subject: [PATCH] feat: add AuthenticationStorage::from_file() (#645) --- .../src/authentication_storage/storage.rs | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/crates/rattler_networking/src/authentication_storage/storage.rs b/crates/rattler_networking/src/authentication_storage/storage.rs index 0dc57e8a6..12a983ced 100644 --- a/crates/rattler_networking/src/authentication_storage/storage.rs +++ b/crates/rattler_networking/src/authentication_storage/storage.rs @@ -57,7 +57,6 @@ impl AuthenticationStorage { /// with the path specified in the variable pub fn from_env() -> Result { if let Ok(auth_file) = std::env::var("RATTLER_AUTH_FILE") { - let mut storage = Self::new(); let path = std::path::Path::new(&auth_file); tracing::info!( @@ -65,21 +64,27 @@ impl AuthenticationStorage { auth_file ); - let backend = FileStorage::new(path.to_path_buf()).map_err(|e| { - anyhow!( - "Error creating file storage backend for RATTLER_AUTH_FILE ({}): {}", - auth_file, - e - ) - })?; - - storage.add_backend(Arc::from(backend)); - Ok(storage) + Ok(Self::from_file(path)?) } else { Ok(Self::default()) } } + /// Create a new authentication storage with just a file storage backend + pub fn from_file(path: &std::path::Path) -> Result { + let mut storage = Self::new(); + let backend = FileStorage::new(path.to_path_buf()).map_err(|e| { + anyhow!( + "Error creating file storage backend from file ({}): {}", + path.display(), + e + ) + })?; + + storage.add_backend(Arc::from(backend)); + Ok(storage) + } + /// Add a new storage backend to the authentication storage /// (backends are tried in the order they are added) pub fn add_backend(&mut self, backend: Arc) {