added example
This commit is contained in:
		| @@ -80,7 +80,7 @@ impl Error for GitError { | ||||
|  */ | ||||
| pub fn git_clone(url: &str) -> Result<String, GitError> { | ||||
|     // Check if git is installed | ||||
|     let git_check = Command::new("git") | ||||
|     let _git_check = Command::new("git") | ||||
|         .arg("--version") | ||||
|         .output() | ||||
|         .map_err(GitError::GitNotInstalled)?; | ||||
|   | ||||
| @@ -293,17 +293,6 @@ impl GitExecutor { | ||||
|  | ||||
|     // Execute git command with username/password | ||||
|     fn execute_with_credentials(&self, args: &[&str], username: &str, password: &str) -> Result<Output, GitExecutorError> { | ||||
|         // Helper method to execute a command and handle the result | ||||
|         fn execute_command(command: &mut Command) -> Result<Output, GitExecutorError> { | ||||
|             let output = command.output()?; | ||||
|              | ||||
|             if output.status.success() { | ||||
|                 Ok(output) | ||||
|             } else { | ||||
|                 let error = String::from_utf8_lossy(&output.stderr); | ||||
|                 Err(GitExecutorError::GitCommandFailed(error.to_string())) | ||||
|             } | ||||
|         } | ||||
|         // For HTTPS authentication, we need to modify the URL to include credentials | ||||
|         // Create a new vector to hold our modified arguments | ||||
|         let modified_args: Vec<String> = args.iter().map(|&arg| { | ||||
|   | ||||
| @@ -76,7 +76,7 @@ pub struct CommandResult { | ||||
|  | ||||
| impl CommandResult { | ||||
|     /// Create a default failed result with an error message | ||||
|     fn error(message: &str) -> Self { | ||||
|     fn _error(message: &str) -> Self { | ||||
|         Self { | ||||
|             stdout: String::new(), | ||||
|             stderr: message.to_string(), | ||||
| @@ -132,10 +132,6 @@ fn handle_child_output(mut child: Child, silent: bool) -> Result<CommandResult, | ||||
|     let stdout = child.stdout.take(); | ||||
|     let stderr = child.stderr.take(); | ||||
|      | ||||
|     // Buffers for captured output | ||||
|     let mut captured_stdout = String::new(); | ||||
|     let mut captured_stderr = String::new(); | ||||
|      | ||||
|     // Process stdout | ||||
|     let stdout_handle = if let Some(out) = stdout { | ||||
|         let reader = BufReader::new(out); | ||||
| @@ -191,18 +187,18 @@ fn handle_child_output(mut child: Child, silent: bool) -> Result<CommandResult, | ||||
|         .map_err(|e| RunError::ChildProcessError(format!("Failed to wait on child process: {}", e)))?; | ||||
|      | ||||
|     // Join our stdout thread if it exists | ||||
|     if let Some(handle) = stdout_handle { | ||||
|         captured_stdout = handle.join().unwrap_or_default(); | ||||
|     let captured_stdout = if let Some(handle) = stdout_handle { | ||||
|         handle.join().unwrap_or_default() | ||||
|     } else { | ||||
|         captured_stdout = "Failed to capture stdout".to_string(); | ||||
|     } | ||||
|         "Failed to capture stdout".to_string() | ||||
|     }; | ||||
|      | ||||
|     // Join our stderr thread if it exists | ||||
|     if let Some(handle) = stderr_handle { | ||||
|         captured_stderr = handle.join().unwrap_or_default(); | ||||
|     let captured_stderr = if let Some(handle) = stderr_handle { | ||||
|         handle.join().unwrap_or_default() | ||||
|     } else { | ||||
|         captured_stderr = "Failed to capture stderr".to_string(); | ||||
|     } | ||||
|         "Failed to capture stderr".to_string() | ||||
|     }; | ||||
|      | ||||
|     // Return the command result | ||||
|     Ok(CommandResult { | ||||
|   | ||||
| @@ -1,4 +1,4 @@ | ||||
| use redis::{Client, Connection, Commands, RedisError, RedisResult, Cmd}; | ||||
| use redis::{Client, Connection, RedisError, RedisResult, Cmd}; | ||||
| use std::env; | ||||
| use std::path::Path; | ||||
| use std::sync::{Arc, Mutex, Once}; | ||||
|   | ||||
| @@ -1,125 +0,0 @@ | ||||
| //! Example usage of the buildah module | ||||
| //!  | ||||
| //! This file demonstrates how to use the buildah module to perform | ||||
| //! common container operations like creating containers, running commands, | ||||
| //! and managing images. | ||||
|  | ||||
| use crate::virt::buildah::{self, BuildahError}; | ||||
| use std::collections::HashMap; | ||||
|  | ||||
| /// Run a complete buildah workflow example | ||||
| pub fn run_buildah_example() -> Result<(), BuildahError> { | ||||
|     println!("Starting buildah example workflow..."); | ||||
|      | ||||
|     // Step 1: Create a container from an image | ||||
|     println!("\n=== Creating container from fedora:latest ==="); | ||||
|     let result = buildah::from("fedora:latest")?; | ||||
|     let container_id = result.stdout.trim(); | ||||
|     println!("Created container: {}", container_id); | ||||
|      | ||||
|     // Step 2: Run a command in the container | ||||
|     println!("\n=== Installing nginx in container ==="); | ||||
|     let install_result = buildah::run(container_id, "dnf install -y nginx")?; | ||||
|     println!("Installation output: {}", install_result.stdout); | ||||
|      | ||||
|     // Step 3: Copy a file into the container | ||||
|     println!("\n=== Copying configuration file to container ==="); | ||||
|     // Note: This would require an actual file to exist | ||||
|     // buildah::copy(container_id, "./nginx.conf", "/etc/nginx/nginx.conf")?; | ||||
|     println!("For a real example, you would copy a configuration file here"); | ||||
|      | ||||
|     // Step 4: Configure container metadata | ||||
|     println!("\n=== Configuring container metadata ==="); | ||||
|     let mut config_options = HashMap::new(); | ||||
|     config_options.insert("port".to_string(), "80".to_string()); | ||||
|     config_options.insert("label".to_string(), "maintainer=example@example.com".to_string()); | ||||
|     config_options.insert("entrypoint".to_string(), "/usr/sbin/nginx".to_string()); | ||||
|      | ||||
|     buildah::config(container_id, config_options)?; | ||||
|     println!("Container configured"); | ||||
|      | ||||
|     // Step 5: Commit the container to create a new image | ||||
|     println!("\n=== Committing container to create image ==="); | ||||
|     let image_name = "my-nginx:latest"; | ||||
|     buildah::image_commit(container_id, image_name, Some("docker"), true, true)?; | ||||
|     println!("Created image: {}", image_name); | ||||
|      | ||||
|     // Step 6: List images to verify our new image exists | ||||
|     println!("\n=== Listing images ==="); | ||||
|     let images = buildah::images()?; | ||||
|     println!("Found {} images:", images.len()); | ||||
|     for image in images { | ||||
|         println!("  ID: {}", image.id); | ||||
|         println!("  Names: {}", image.names.join(", ")); | ||||
|         println!("  Size: {}", image.size); | ||||
|         println!("  Created: {}", image.created); | ||||
|         println!(); | ||||
|     } | ||||
|      | ||||
|     // Step 7: Clean up (optional in a real workflow) | ||||
|     println!("\n=== Cleaning up ==="); | ||||
|     println!("In a real workflow, you might want to keep the image"); | ||||
|     println!("To remove the image, you would run:"); | ||||
|     println!("buildah::image_remove(\"{}\")", image_name); | ||||
|      | ||||
|     println!("\nBuildah example workflow completed successfully!"); | ||||
|     Ok(()) | ||||
| } | ||||
|  | ||||
| /// Demonstrate how to build an image from a Containerfile/Dockerfile | ||||
| pub fn build_image_example() -> Result<(), BuildahError> { | ||||
|     println!("Building an image from a Containerfile..."); | ||||
|      | ||||
|     // This would typically use a command like: | ||||
|     // buildah build -t my-app:latest . | ||||
|      | ||||
|     // For our example, we'll just show the command structure | ||||
|     let build_args = &["build", "-t", "my-app:latest", "."]; | ||||
|     let result = buildah::execute_buildah_command(build_args)?; | ||||
|      | ||||
|     println!("Build output: {}", result.stdout); | ||||
|     println!("Image built successfully!"); | ||||
|      | ||||
|     Ok(()) | ||||
| } | ||||
|  | ||||
| /// Example of pulling and pushing images | ||||
| pub fn registry_operations_example() -> Result<(), BuildahError> { | ||||
|     println!("Demonstrating registry operations..."); | ||||
|      | ||||
|     // Pull an image | ||||
|     println!("\n=== Pulling an image ==="); | ||||
|     buildah::image_pull("docker.io/library/alpine:latest", true)?; | ||||
|     println!("Image pulled successfully"); | ||||
|      | ||||
|     // Tag the image | ||||
|     println!("\n=== Tagging the image ==="); | ||||
|     buildah::image_tag("alpine:latest", "my-alpine:v1.0")?; | ||||
|     println!("Image tagged successfully"); | ||||
|      | ||||
|     // Push an image (this would typically go to a real registry) | ||||
|     println!("\n=== Pushing an image (example only) ==="); | ||||
|     println!("In a real scenario, you would push to a registry with:"); | ||||
|     println!("buildah::image_push(\"my-alpine:v1.0\", \"docker://registry.example.com/my-alpine:v1.0\", true)"); | ||||
|      | ||||
|     Ok(()) | ||||
| } | ||||
|  | ||||
| /// Main function to run all examples | ||||
| pub fn run_all_examples() -> Result<(), BuildahError> { | ||||
|     println!("=== BUILDAH MODULE EXAMPLES ===\n"); | ||||
|      | ||||
|     // Note: In a real application, you might want to run these | ||||
|     // examples individually or have proper error handling | ||||
|      | ||||
|     // Uncomment these to run the examples | ||||
|     run_buildah_example()?; | ||||
|     build_image_example()?; | ||||
|     registry_operations_example()?; | ||||
|      | ||||
|     println!("\nTo run these examples, uncomment the function calls in run_all_examples()"); | ||||
|     println!("Note that these examples require buildah to be installed on your system"); | ||||
|     println!("and may require root/sudo privileges depending on your setup."); | ||||
|      | ||||
|     Ok(()) | ||||
| } | ||||
| @@ -1,4 +1,3 @@ | ||||
| use std::process::Command; | ||||
| use std::collections::HashMap; | ||||
| use crate::virt::buildah::execute_buildah_command; | ||||
| use crate::process::CommandResult; | ||||
|   | ||||
| @@ -1,7 +1,6 @@ | ||||
| mod containers; | ||||
| mod images; | ||||
| mod cmd; | ||||
| mod example; | ||||
|  | ||||
| use std::fmt; | ||||
| use std::error::Error; | ||||
| @@ -44,5 +43,4 @@ impl Error for BuildahError { | ||||
| } | ||||
| pub use containers::*; | ||||
| pub use images::*; | ||||
| pub use cmd::*; | ||||
| pub use example::*; | ||||
| pub use cmd::*; | ||||
		Reference in New Issue
	
	Block a user