pub struct GlobalConfigManager { /* private fields */ }Expand description
Configuration manager with caching for global configuration.
Provides a higher-level interface for working with global configuration that includes caching to avoid repeated file I/O operations. This is particularly useful in command-line applications that may access configuration multiple times.
§Features
- Lazy Loading: Configuration is loaded only when first accessed
- Caching: Subsequent accesses use the cached configuration
- Reload Support: Can reload from disk when needed
- Custom Paths: Supports custom configuration file paths for testing
§Examples
§Basic Usage
use agpm_cli::config::GlobalConfigManager;
let mut manager = GlobalConfigManager::new()?;
// First access loads from disk
let config = manager.get().await?;
println!("Found {} sources", config.sources.len());
// Subsequent accesses use cache
let config2 = manager.get().await?;§Modifying Configuration
use agpm_cli::config::GlobalConfigManager;
let mut manager = GlobalConfigManager::new()?;
// Get mutable reference
let config = manager.get_mut().await?;
config.add_source(
"new".to_string(),
"https://github.com/owner/repo.git".to_string()
);
// Save changes to disk
manager.save().await?;Implementations§
Source§impl GlobalConfigManager
impl GlobalConfigManager
Sourcepub fn new() -> Result<Self>
pub fn new() -> Result<Self>
Create a new configuration manager using the default global config path.
The manager will use the platform-appropriate default location for the global configuration file.
§Examples
use agpm_cli::config::GlobalConfigManager;
let manager = GlobalConfigManager::new()?;§Errors
Returns an error if the default configuration path cannot be determined.
Sourcepub const fn with_path(path: PathBuf) -> Self
pub const fn with_path(path: PathBuf) -> Self
Create a configuration manager with a custom file path.
This method is primarily useful for testing or when you need to use a non-standard location for the configuration file.
§Parameters
path: Custom path for the configuration file
§Examples
use agpm_cli::config::GlobalConfigManager;
use std::path::PathBuf;
let manager = GlobalConfigManager::with_path(PathBuf::from("/tmp/test.toml"));Sourcepub async fn get(&mut self) -> Result<&GlobalConfig>
pub async fn get(&mut self) -> Result<&GlobalConfig>
Get a reference to the global configuration, loading it if necessary.
If the configuration hasn’t been loaded yet, this method will load it from disk. Subsequent calls will return the cached configuration.
§Returns
A reference to the cached GlobalConfig.
§Examples
use agpm_cli::config::GlobalConfigManager;
let mut manager = GlobalConfigManager::new()?;
let config = manager.get().await?;
println!("Global config has {} sources", config.sources.len());§Errors
Returns an error if:
- The configuration file exists but cannot be read
- The configuration file contains invalid TOML syntax
Sourcepub async fn get_mut(&mut self) -> Result<&mut GlobalConfig>
pub async fn get_mut(&mut self) -> Result<&mut GlobalConfig>
Get a mutable reference to the global configuration, loading it if necessary.
Similar to get, but returns a mutable reference allowing
modification of the configuration.
§Returns
A mutable reference to the cached GlobalConfig.
§Examples
use agpm_cli::config::GlobalConfigManager;
let mut manager = GlobalConfigManager::new()?;
let config = manager.get_mut().await?;
config.add_source(
"new".to_string(),
"https://github.com/owner/repo.git".to_string()
);§Errors
Returns an error if:
- The configuration file exists but cannot be read
- The configuration file contains invalid TOML syntax
Sourcepub async fn save(&self) -> Result<()>
pub async fn save(&self) -> Result<()>
Save the current cached configuration to disk.
Writes the current configuration state to the file system. If no configuration has been loaded, this method does nothing.
§Examples
use agpm_cli::config::GlobalConfigManager;
let mut manager = GlobalConfigManager::new()?;
// Modify configuration
let config = manager.get_mut().await?;
config.add_source("test".to_string(), "https://test.com/repo.git".to_string());
// Save changes
manager.save().await?;§Errors
Returns an error if:
- The file cannot be written (permissions, disk space, etc.)
- Parent directories cannot be created
Sourcepub async fn reload(&mut self) -> Result<()>
pub async fn reload(&mut self) -> Result<()>
Reload the configuration from disk, discarding cached data.
Forces a reload of the configuration from the file system, discarding any cached data. Useful when the configuration file may have been modified externally.
§Examples
use agpm_cli::config::GlobalConfigManager;
let mut manager = GlobalConfigManager::new()?;
// Load initial configuration
let config1 = manager.get().await?;
let count1 = config1.sources.len();
// Configuration file modified externally...
// Reload to pick up external changes
manager.reload().await?;
let config2 = manager.get().await?;
let count2 = config2.sources.len();§Errors
Returns an error if:
- The configuration file exists but cannot be read
- The configuration file contains invalid TOML syntax
Auto Trait Implementations§
impl Freeze for GlobalConfigManager
impl RefUnwindSafe for GlobalConfigManager
impl Send for GlobalConfigManager
impl Sync for GlobalConfigManager
impl Unpin for GlobalConfigManager
impl UnwindSafe for GlobalConfigManager
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more