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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//! Docker inspect command implementation.
//!
//! This module provides the `docker inspect` command for getting detailed information
//! about Docker objects (containers, images, volumes, networks, etc.).
use super::{CommandExecutor, CommandOutput, DockerCommand};
use crate::error::Result;
use async_trait::async_trait;
use serde_json::Value;
/// Docker inspect command builder
///
/// # Example
///
/// ```no_run
/// use docker_wrapper::InspectCommand;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// // Inspect a container
/// let info = InspectCommand::new("my-container")
/// .run()
/// .await?;
///
/// // Parse as JSON
/// let json = info.json()?;
/// println!("Container state: {}", json[0]["State"]["Status"]);
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct InspectCommand {
/// Objects to inspect (container/image/volume/network IDs or names)
objects: Vec<String>,
/// Output format
format: Option<String>,
/// Return size information
size: bool,
/// Type of object to inspect
object_type: Option<String>,
/// Command executor
pub executor: CommandExecutor,
}
impl InspectCommand {
/// Create a new inspect command for a single object
///
/// # Example
///
/// ```
/// use docker_wrapper::InspectCommand;
///
/// let cmd = InspectCommand::new("my-container");
/// ```
#[must_use]
pub fn new(object: impl Into<String>) -> Self {
Self {
objects: vec![object.into()],
format: None,
size: false,
object_type: None,
executor: CommandExecutor::new(),
}
}
/// Create a new inspect command for multiple objects
///
/// # Example
///
/// ```
/// use docker_wrapper::InspectCommand;
///
/// let cmd = InspectCommand::new_multiple(vec!["container1", "container2"]);
/// ```
#[must_use]
pub fn new_multiple(objects: Vec<impl Into<String>>) -> Self {
Self {
objects: objects.into_iter().map(Into::into).collect(),
format: None,
size: false,
object_type: None,
executor: CommandExecutor::new(),
}
}
/// Add another object to inspect
#[must_use]
pub fn object(mut self, object: impl Into<String>) -> Self {
self.objects.push(object.into());
self
}
/// Set custom format string (Go template)
///
/// # Example
///
/// ```
/// use docker_wrapper::InspectCommand;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let cmd = InspectCommand::new("my-container")
/// .format("{{.State.Status}}");
/// # Ok(())
/// # }
/// ```
#[must_use]
pub fn format(mut self, format: impl Into<String>) -> Self {
self.format = Some(format.into());
self
}
/// Display total file sizes
#[must_use]
pub fn size(mut self) -> Self {
self.size = true;
self
}
/// Specify the type of object to inspect
///
/// Valid types: container, image, volume, network, plugin, node, service, etc.
#[must_use]
pub fn object_type(mut self, typ: impl Into<String>) -> Self {
self.object_type = Some(typ.into());
self
}
/// Execute the inspect command
///
/// # Errors
/// Returns an error if:
/// - The Docker daemon is not running
/// - The specified object doesn't exist
/// - The object type is invalid
pub async fn run(&self) -> Result<InspectOutput> {
let output = self.execute().await?;
Ok(InspectOutput { output })
}
/// Gets the command executor
#[must_use]
pub fn get_executor(&self) -> &CommandExecutor {
&self.executor
}
/// Gets the command executor mutably
pub fn get_executor_mut(&mut self) -> &mut CommandExecutor {
&mut self.executor
}
/// Builds the command arguments for Docker inspect
#[must_use]
pub fn build_command_args(&self) -> Vec<String> {
let mut args = vec!["inspect".to_string()];
if let Some(ref format) = self.format {
args.push("--format".to_string());
args.push(format.clone());
}
if self.size {
args.push("--size".to_string());
}
if let Some(ref typ) = self.object_type {
args.push("--type".to_string());
args.push(typ.clone());
}
// Add object names/IDs
args.extend(self.objects.clone());
// Add any additional raw arguments
args.extend(self.executor.raw_args.clone());
args
}
}
#[async_trait]
impl DockerCommand for InspectCommand {
type Output = CommandOutput;
fn get_executor(&self) -> &CommandExecutor {
&self.executor
}
fn get_executor_mut(&mut self) -> &mut CommandExecutor {
&mut self.executor
}
fn build_command_args(&self) -> Vec<String> {
self.build_command_args()
}
async fn execute(&self) -> Result<Self::Output> {
let args = self.build_command_args();
self.execute_command(args).await
}
}
/// Result from the inspect command
#[derive(Debug, Clone)]
pub struct InspectOutput {
/// Raw command output
pub output: CommandOutput,
}
impl InspectOutput {
/// Parse the output as JSON
///
/// # Errors
/// Returns an error if the output is not valid JSON
///
/// # Example
///
/// ```no_run
/// # use docker_wrapper::InspectCommand;
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let info = InspectCommand::new("my-container").run().await?;
/// let json = info.json()?;
/// println!("Container ID: {}", json[0]["Id"]);
/// # Ok(())
/// # }
/// ```
pub fn json(&self) -> Result<Value> {
serde_json::from_str(&self.output.stdout)
.map_err(|e| crate::error::Error::parse_error(format!("Failed to parse JSON: {e}")))
}
/// Get raw stdout
#[must_use]
pub fn stdout(&self) -> &str {
&self.output.stdout
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_inspect_single_object() {
let cmd = InspectCommand::new("test-container");
let args = cmd.build_command_args();
assert_eq!(args, vec!["inspect", "test-container"]);
}
#[test]
fn test_inspect_multiple_objects() {
let cmd = InspectCommand::new_multiple(vec!["container1", "image1", "volume1"]);
let args = cmd.build_command_args();
assert_eq!(args, vec!["inspect", "container1", "image1", "volume1"]);
}
#[test]
fn test_inspect_with_format() {
let cmd = InspectCommand::new("test-container").format("{{.State.Status}}");
let args = cmd.build_command_args();
assert_eq!(
args,
vec!["inspect", "--format", "{{.State.Status}}", "test-container"]
);
}
#[test]
fn test_inspect_with_size() {
let cmd = InspectCommand::new("test-image").size();
let args = cmd.build_command_args();
assert_eq!(args, vec!["inspect", "--size", "test-image"]);
}
#[test]
fn test_inspect_with_type() {
let cmd = InspectCommand::new("my-network").object_type("network");
let args = cmd.build_command_args();
assert_eq!(args, vec!["inspect", "--type", "network", "my-network"]);
}
#[test]
fn test_inspect_all_options() {
let cmd = InspectCommand::new("test-container")
.format("{{json .}}")
.size()
.object_type("container");
let args = cmd.build_command_args();
assert_eq!(
args,
vec![
"inspect",
"--format",
"{{json .}}",
"--size",
"--type",
"container",
"test-container"
]
);
}
}