pub struct CommandContext {
pub manifest: Manifest,
pub manifest_path: PathBuf,
pub project_dir: PathBuf,
pub lockfile_path: PathBuf,
}Expand description
Common context for CLI commands that need manifest and project information
Fields§
§manifest: ManifestParsed project manifest (agpm.toml)
manifest_path: PathBufPath to the manifest file
project_dir: PathBufProject root directory (containing agpm.toml)
lockfile_path: PathBufPath to the lockfile (agpm.lock)
Implementations§
Source§impl CommandContext
impl CommandContext
Sourcepub fn new(manifest: Manifest, project_dir: PathBuf) -> Result<Self>
pub fn new(manifest: Manifest, project_dir: PathBuf) -> Result<Self>
Create a new command context from a manifest and project directory
Sourcepub fn from_manifest_path(manifest_path: impl AsRef<Path>) -> Result<Self>
pub fn from_manifest_path(manifest_path: impl AsRef<Path>) -> Result<Self>
Create a new command context from a manifest path
§Errors
Returns an error if the manifest file doesn’t exist or cannot be read
Sourcepub fn load_lockfile(&self) -> Result<Option<LockFile>>
pub fn load_lockfile(&self) -> Result<Option<LockFile>>
Load an existing lockfile if it exists
§Errors
Returns an error if the lockfile exists but cannot be parsed
Sourcepub fn load_lockfile_with_regeneration(
&self,
can_regenerate: bool,
operation_name: &str,
) -> Result<Option<LockFile>>
pub fn load_lockfile_with_regeneration( &self, can_regenerate: bool, operation_name: &str, ) -> Result<Option<LockFile>>
Load an existing lockfile with automatic regeneration for invalid files
If the lockfile exists but is invalid or corrupted, this method will offer to automatically regenerate it. This provides a better user experience by recovering from common lockfile issues.
§Arguments
can_regenerate- Whether automatic regeneration should be offeredoperation_name- Name of the operation for error messages (e.g., “list”)
§Returns
Ok(Some(lockfile))- Successfully loaded or regenerated lockfileOk(None)- No lockfile exists (not an error)Err- Critical error that cannot be recovered from
§Behavior
- Interactive mode (TTY): Prompts user with Y/n confirmation
- Non-interactive mode (CI/CD): Fails with helpful error message
- Backup strategy: Copies invalid lockfile to
agpm.lock.invalidbefore regeneration
§Examples
let manifest = Manifest::load(&PathBuf::from("agpm.toml"))?;
let project_dir = PathBuf::from(".");
let ctx = CommandContext::new(manifest, project_dir)?;
match ctx.load_lockfile_with_regeneration(true, "list") {
Ok(Some(lockfile)) => println!("Loaded lockfile"),
Ok(None) => println!("No lockfile found"),
Err(e) => eprintln!("Error: {}", e),
}