execute

Function execute 

Source
pub async fn execute(args: UpgradeArgs) -> Result<()>
Expand description

Execute the upgrade command with the provided arguments.

This is the main entry point for all upgrade-related operations. It handles the various upgrade modes (check, status, upgrade, rollback) and coordinates the different components (updater, backup manager, version checker) to provide a safe and reliable upgrade experience.

§Arguments

  • args - The parsed command-line arguments containing upgrade options

§Command Flow

  1. Initialization: Load global config and set up cache directories
  2. Mode Detection: Determine operation mode based on flags
  3. Component Setup: Initialize updater, backup manager, and version checker
  4. Operation Execution: Perform the requested operation
  5. Result Handling: Provide user feedback and cleanup

§Operation Modes

§Rollback Mode (--rollback)

  • Validates backup existence
  • Restores previous version from backup
  • Provides rollback status feedback

§Status Mode (--status)

  • Shows current version information
  • Checks for latest version (cached or fresh)
  • Displays formatted version comparison

§Check Mode (--check)

  • Fetches latest version from GitHub
  • Compares with current version
  • Shows update availability

§Upgrade Mode (default)

  • Creates backup (unless --no-backup)
  • Downloads and installs new version
  • Handles success/failure scenarios
  • Cleans up or restores as appropriate

§Returns

  • Ok(()) - Command completed successfully
  • Err(anyhow::Error) - Command failed with detailed error information

§Errors

This function can fail for various reasons:

§Network Errors

  • GitHub API unreachable or rate limited
  • Download failures for binary assets
  • Connectivity issues during version checks

§File System Errors

  • Insufficient permissions to write binary or backups
  • Disk space exhaustion during download or backup
  • File locking issues (especially on Windows)

§Version Errors

  • Target version doesn’t exist on GitHub
  • Invalid version string format
  • No binary assets available for target version

§Configuration Errors

  • Unable to load global configuration
  • Cache directory creation failures
  • Invalid executable path detection

§Examples

use agpm_cli::cli::upgrade::{UpgradeArgs, execute};
use clap::Parser;

// Parse command line arguments
let args = UpgradeArgs::parse();

// Execute the upgrade command
execute(args).await?;

§Safety Features

  • Automatic Backups: Created before modifications unless disabled
  • Rollback Support: Automatic restoration on upgrade failure
  • Version Validation: Ensures target versions exist and are accessible
  • Permission Checks: Validates file system access before attempting changes
  • Atomic Operations: Uses safe file operations to minimize corruption risk

§User Experience

The function provides comprehensive user feedback:

  • Colored output for different message types (success, warning, error)
  • Progress indicators for long-running operations
  • Clear error messages with suggested resolution steps