Skip to content

Commit

Permalink
feat: add AuthenticationStorage::from_file() (#645)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xbe7a committed May 7, 2024
1 parent f2c4c19 commit 4080304
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions crates/rattler_networking/src/authentication_storage/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +57,34 @@ impl AuthenticationStorage {
/// with the path specified in the variable
pub fn from_env() -> Result<Self> {
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!(
"\"RATTLER_AUTH_FILE\" environment variable set, using file storage at {}",
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<Self> {
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<dyn StorageBackend + Send + Sync>) {
Expand Down

0 comments on commit 4080304

Please sign in to comment.