...
This commit is contained in:
@@ -125,4 +125,17 @@ pub fn list(all: bool) -> Result<CommandResult, NerdctlError> {
|
||||
}
|
||||
|
||||
execute_nerdctl_command(&args)
|
||||
}
|
||||
|
||||
/// Get container logs
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `container` - Container name or ID
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// * `Result<CommandResult, NerdctlError>` - Command result or error
|
||||
pub fn logs(container: &str) -> Result<CommandResult, NerdctlError> {
|
||||
execute_nerdctl_command(&["logs", container])
|
||||
}
|
@@ -27,23 +27,37 @@ impl Container {
|
||||
match self.verify_running() {
|
||||
Ok(true) => start_result,
|
||||
Ok(false) => {
|
||||
// Container started but isn't running - try to get more details
|
||||
// Container started but isn't running - get detailed information
|
||||
let mut error_message = format!("Container {} started but is not running.", container_id);
|
||||
|
||||
// Get container status
|
||||
if let Ok(status) = self.status() {
|
||||
Err(NerdctlError::CommandFailed(
|
||||
format!("Container {} started but is not running. Status: {}, State: {}, Health: {}",
|
||||
container_id,
|
||||
status.status,
|
||||
status.state,
|
||||
status.health_status.unwrap_or_else(|| "N/A".to_string())
|
||||
)
|
||||
))
|
||||
} else {
|
||||
Err(NerdctlError::CommandFailed(
|
||||
format!("Container {} started but is not running. Unable to get status details.",
|
||||
container_id
|
||||
)
|
||||
))
|
||||
error_message.push_str(&format!("\nStatus: {}, State: {}, Health: {}",
|
||||
status.status,
|
||||
status.state,
|
||||
status.health_status.unwrap_or_else(|| "N/A".to_string())
|
||||
));
|
||||
}
|
||||
|
||||
// Get container logs
|
||||
if let Ok(logs) = execute_nerdctl_command(&["logs", container_id]) {
|
||||
if !logs.stdout.trim().is_empty() {
|
||||
error_message.push_str(&format!("\nContainer logs (stdout):\n{}", logs.stdout.trim()));
|
||||
}
|
||||
if !logs.stderr.trim().is_empty() {
|
||||
error_message.push_str(&format!("\nContainer logs (stderr):\n{}", logs.stderr.trim()));
|
||||
}
|
||||
}
|
||||
|
||||
// Get container exit code if available
|
||||
if let Ok(inspect_result) = execute_nerdctl_command(&["inspect", "--format", "{{.State.ExitCode}}", container_id]) {
|
||||
let exit_code = inspect_result.stdout.trim();
|
||||
if !exit_code.is_empty() && exit_code != "0" {
|
||||
error_message.push_str(&format!("\nContainer exit code: {}", exit_code));
|
||||
}
|
||||
}
|
||||
|
||||
Err(NerdctlError::CommandFailed(error_message))
|
||||
},
|
||||
Err(err) => {
|
||||
// Failed to verify if container is running
|
||||
@@ -274,6 +288,19 @@ impl Container {
|
||||
} else {
|
||||
Err(NerdctlError::Other("No container ID available".to_string()))
|
||||
}
|
||||
|
||||
/// Get container logs
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// * `Result<CommandResult, NerdctlError>` - Command result or error
|
||||
pub fn logs(&self) -> Result<CommandResult, NerdctlError> {
|
||||
if let Some(container_id) = &self.container_id {
|
||||
execute_nerdctl_command(&["logs", container_id])
|
||||
} else {
|
||||
Err(NerdctlError::Other("No container ID available".to_string()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Get container resource usage
|
||||
|
Reference in New Issue
Block a user