-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbasic_usage.rs
More file actions
124 lines (112 loc) · 4.42 KB
/
basic_usage.rs
File metadata and controls
124 lines (112 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//! Basic usage example for docker-wrapper prerequisites checking.
//!
//! This example demonstrates how to check Docker prerequisites
//! and handle various scenarios.
use docker_wrapper::{ensure_docker, DockerPrerequisites, Error};
#[tokio::main]
async fn main() {
// Initialize logging
tracing_subscriber::fmt::init();
println!("Docker Wrapper - Prerequisites Example");
println!("=====================================\n");
// Example 1: Simple prerequisites check
println!("1. Simple Docker check:");
match ensure_docker().await {
Ok(info) => {
println!("✅ Docker is available!");
println!(" Binary: {}", info.binary_path);
println!(" Version: {}", info.version.version);
println!(" OS: {}", info.os);
println!(" Architecture: {}", info.architecture);
if info.daemon_running {
println!(" Daemon: Running");
if let Some(server_version) = &info.server_version {
println!(" Server Version: {}", server_version.version);
}
} else {
println!(" Daemon: Not running");
}
}
Err(e) => {
println!("❌ Docker check failed: {e}");
handle_prerequisites_error(&e);
}
}
println!();
// Example 2: Custom minimum version check
println!("2. Custom minimum version check (25.0.0):");
let custom_version = match docker_wrapper::prerequisites::DockerVersion::parse("25.0.0") {
Ok(version) => version,
Err(e) => {
println!("❌ Failed to parse version: {e}");
return;
}
};
let checker = DockerPrerequisites::new(custom_version);
match checker.check().await {
Ok(info) => {
println!("✅ Docker meets high version requirement!");
println!(" Version: {}", info.version.version);
}
Err(e) => {
println!("❌ Docker version check failed: {e}");
match &e {
Error::UnsupportedVersion { found, minimum } => {
println!(" Found: {found}, Required: {minimum}");
}
_ => handle_prerequisites_error(&e),
}
}
}
println!();
// Example 3: Detailed system information
println!("3. Detailed system information:");
match ensure_docker().await {
Ok(info) => {
println!("System Information:");
println!("├── Operating System: {}", info.os);
println!("├── Architecture: {}", info.architecture);
println!("├── Docker Binary: {}", info.binary_path);
println!("├── Client Version: {}", info.version.version);
println!("│ ├── Major: {}", info.version.major);
println!("│ ├── Minor: {}", info.version.minor);
println!("│ └── Patch: {}", info.version.patch);
if info.daemon_running {
println!("└── Docker Daemon: Running");
if let Some(server_version) = &info.server_version {
println!(" └── Server Version: {}", server_version.version);
}
} else {
println!("└── Docker Daemon: Not Running");
}
}
Err(e) => {
println!("❌ Failed to get system information: {e}");
}
}
}
/// Handle different types of prerequisites errors with helpful messages
fn handle_prerequisites_error(error: &Error) {
match error {
Error::DockerNotFound => {
println!(" 💡 Install Docker from: https://docs.docker.com/get-docker/");
}
Error::DaemonNotRunning => {
println!(" 💡 Start Docker daemon with: sudo systemctl start docker");
println!(" 💡 Or start Docker Desktop application");
}
Error::UnsupportedVersion { found, minimum } => {
println!(" 💡 Update Docker to version {minimum} or higher");
println!(" 💡 Current version: {found}");
}
Error::CommandFailed { command, .. } => {
println!(" 💡 Command execution failed: {command}");
}
Error::ParseError { message } => {
println!(" 💡 Parse error: {message}");
}
_ => {
println!(" 💡 Unexpected error: {error}");
}
}
}