basic_usage_demo/
basic_usage_demo.rs

1//! Basic Usage Demo - Corresponds to README Quick Start examples
2//!
3//! This example demonstrates basic usage patterns documented in the README:
4//! 1. Extract tar file and cache locally
5//! 2. Push from cache to registry
6//! 3. Pull from registry and cache
7//! 4. Complete pull-to-push workflow
8//!
9//! Usage:
10//! ```bash
11//! # Set environment variables
12//! export REGISTRY_USERNAME=your_username
13//! export REGISTRY_PASSWORD=your_password
14//! export TARGET_REGISTRY=registry.example.com
15//! 
16//! # Run the demo
17//! cargo run --example basic_usage_demo
18//! ```
19
20use docker_image_pusher::{
21    error::Result,
22    image::image_manager::ImageManager,
23    registry::RegistryClientBuilder,
24    logging::Logger,
25};
26use std::env;
27
28#[tokio::main]
29async fn main() -> Result<()> {
30    println!("🚀 Docker Image Pusher - Basic Usage Demo");
31    println!("==========================================");
32    println!("This demo corresponds to README Quick Start examples");
33    println!();
34
35    // Initialize logger for verbose output
36    let logger = Logger::new(true);
37    
38    // Configuration from environment
39    let registry_username = env::var("REGISTRY_USERNAME").unwrap_or_else(|_| {
40        println!("⚠️  REGISTRY_USERNAME not set, using demo credentials");
41        "demo_user".to_string()
42    });
43    
44    let registry_password = env::var("REGISTRY_PASSWORD").unwrap_or_else(|_| {
45        println!("⚠️  REGISTRY_PASSWORD not set, using demo credentials");
46        "demo_pass".to_string()
47    });
48    
49    let target_registry = env::var("TARGET_REGISTRY").unwrap_or_else(|_| {
50        "registry.example.com".to_string()
51    });
52
53    let cache_dir = ".cache_basic_demo";
54    
55    // Clean up previous demo
56    let _ = std::fs::remove_dir_all(cache_dir);
57    
58    logger.section("Demo 1: Basic Extract and Push Workflow");
59    println!("📝 Scenario: Extract tar file → Cache → Push to registry");
60    println!("📄 Command equivalent:");
61    println!("   docker-image-pusher extract --file image.tar --verbose");
62    println!("   docker-image-pusher push --source image:tag --target {}/app:v1.0", target_registry);
63    println!();
64    
65    // Demo the basic workflow
66    demo_extract_and_push(&logger, cache_dir, &target_registry, &registry_username, &registry_password).await?;
67    
68    logger.section("Demo 2: Pull and Cache Workflow");
69    println!("📝 Scenario: Pull from registry → Cache locally");
70    println!("📄 Command equivalent:");
71    println!("   docker-image-pusher pull --image nginx:latest --verbose");
72    println!();
73    
74    demo_pull_and_cache(&logger, cache_dir, &registry_username, &registry_password).await?;
75    
76    logger.section("Demo 3: Complete Pull-to-Push Migration");
77    println!("📝 Scenario: Pull from source → Cache → Push to target");
78    println!("📄 Command equivalent:");
79    println!("   docker-image-pusher pull --image alpine:latest");
80    println!("   docker-image-pusher push --source alpine:latest --target {}/alpine:migrated", target_registry);
81    println!();
82    
83    demo_complete_migration(&logger, cache_dir, &target_registry, &registry_username, &registry_password).await?;
84    
85    println!("✅ Basic usage demo completed successfully!");
86    println!("📚 These examples correspond to the README Quick Start section");
87    
88    Ok(())
89}
90
91async fn demo_extract_and_push(
92    logger: &Logger,
93    _cache_dir: &str,
94    target_registry: &str,
95    username: &str,
96    _password: &str,
97) -> Result<()> {
98    logger.info("Creating demo tar file (simulated)...");
99    
100    // In a real scenario, you would have an actual tar file
101    // For demo purposes, we'll simulate the operation
102    println!("📦 Would extract tar file and cache locally");
103    println!("🚀 Would push cached image to: {}/project/app:v1.0", target_registry);
104    
105    // Create registry client for demonstration
106    let registry_url = format!("https://{}", target_registry);
107    let _client = RegistryClientBuilder::new(registry_url)
108        .with_verbose(true)
109        .build()?;
110    
111    logger.info(&format!("Registry client created for: {}", target_registry));
112    logger.info(&format!("Would authenticate with username: {}", username));
113    
114    // Note: In production, you would:
115    // 1. Use ExtractAndCache operation mode with actual tar file
116    // 2. Use PushFromCacheUsingManifest operation mode
117    
118    Ok(())
119}
120
121async fn demo_pull_and_cache(
122    logger: &Logger,
123    cache_dir: &str,
124    _username: &str,
125    _password: &str,
126) -> Result<()> {
127    logger.info("Demonstrating pull and cache workflow...");
128    
129    // Create image manager
130    let mut _image_manager = ImageManager::new(Some(cache_dir), true)?;
131    
132    println!("🔽 Would pull nginx:latest from Docker Hub");
133    println!("💾 Would cache image locally in: {}", cache_dir);
134    
135    // In production, you would use:
136    // let operation = OperationMode::PullAndCache {
137    //     registry_url: "https://registry-1.docker.io".to_string(),
138    //     repository: "library/nginx".to_string(),
139    //     reference: "latest".to_string(),
140    //     cache_dir: cache_dir.to_string(),
141    //     auth_config: Some(auth_config),
142    // };
143    
144    logger.info("Pull and cache operation configured (demo mode)");
145    
146    Ok(())
147}
148
149async fn demo_complete_migration(
150    logger: &Logger,
151    _cache_dir: &str,
152    target_registry: &str,
153    _username: &str,
154    _password: &str,
155) -> Result<()> {
156    logger.info("Demonstrating complete migration workflow...");
157    
158    println!("🔄 Complete migration workflow:");
159    println!("   1. Pull alpine:latest from Docker Hub");
160    println!("   2. Cache locally");
161    println!("   3. Push to target registry: {}/alpine:migrated", target_registry);
162    
163    // This would involve:
164    // 1. PullAndCache operation
165    // 2. PushFromCacheUsingManifest operation
166    
167    logger.info("Migration workflow completed (demo mode)");
168    
169    Ok(())
170}