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 reload_manifest(&mut self) -> Result<()>
pub fn reload_manifest(&mut self) -> Result<()>
Reload the manifest from disk.
This should be called after any operation that modifies the manifest file, such as migration updating the tools configuration.
§Errors
Returns an error if the manifest file cannot be loaded or parsed.
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),
}Trait Implementations§
Auto Trait Implementations§
impl Freeze for CommandContext
impl RefUnwindSafe for CommandContext
impl Send for CommandContext
impl Sync for CommandContext
impl Unpin for CommandContext
impl UnwindSafe for CommandContext
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