sal_virt/buildah/
content.rs1use crate::buildah::{execute_buildah_command, BuildahError};
2use sal_process::CommandResult;
3use std::fs::File;
4use std::io::{Read, Write};
5use tempfile::NamedTempFile;
6
7pub struct ContentOperations;
9
10impl ContentOperations {
11 pub fn write_content(
23 container_id: &str,
24 content: &str,
25 dest_path: &str,
26 ) -> Result<CommandResult, BuildahError> {
27 let mut temp_file = NamedTempFile::new()
29 .map_err(|e| BuildahError::Other(format!("Failed to create temporary file: {}", e)))?;
30
31 temp_file.write_all(content.as_bytes()).map_err(|e| {
33 BuildahError::Other(format!("Failed to write to temporary file: {}", e))
34 })?;
35
36 temp_file
38 .flush()
39 .map_err(|e| BuildahError::Other(format!("Failed to flush temporary file: {}", e)))?;
40
41 let temp_path = temp_file.path().to_string_lossy().to_string();
43 execute_buildah_command(&["add", container_id, &temp_path, dest_path])
45 }
46
47 pub fn read_content(container_id: &str, source_path: &str) -> Result<String, BuildahError> {
58 let temp_file = NamedTempFile::new()
60 .map_err(|e| BuildahError::Other(format!("Failed to create temporary file: {}", e)))?;
61
62 let temp_path = temp_file.path().to_string_lossy().to_string();
63
64 let mount_result = execute_buildah_command(&["mount", container_id])?;
67 let mount_point = mount_result.stdout.trim();
68
69 let full_source_path = format!("{}{}", mount_point, source_path);
71
72 execute_buildah_command(&["copy", container_id, &full_source_path, &temp_path])?;
74
75 execute_buildah_command(&["umount", container_id])?;
77
78 let mut file = File::open(temp_file.path())
80 .map_err(|e| BuildahError::Other(format!("Failed to open temporary file: {}", e)))?;
81
82 let mut content = String::new();
83 file.read_to_string(&mut content).map_err(|e| {
84 BuildahError::Other(format!("Failed to read from temporary file: {}", e))
85 })?;
86
87 Ok(content)
88 }
89}