summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorWojciech Kępka <46892771+wojciechkepka@users.noreply.github.com>2021-02-06 07:57:49 +0100
committerGitHub <noreply@github.com>2021-02-06 01:57:49 -0500
commitef3dfad8f691f64e41fb1d369399471cde6ad8c0 (patch)
treee8b5bba9d148b3522171eaf72b33624334774416 /examples
parent9b85dc8a9d370139e8eb3cafadf5a0f8f6dc6597 (diff)
Add Exec struct for easier manipulation of exec instances (#251)
* Add ExecDetails and ProcessConfig * Add exec_with_id, exec_inspect * Add example how to inspect an exec instance * Make clippy happy * exit_code is an Option on ExecDetails * Add Exec struct * Update example * Fix typo * Add Exec::resize and ExecResizeOptions * Add resize example
Diffstat (limited to 'examples')
-rw-r--r--examples/execinspect.rs32
-rw-r--r--examples/execresize.rs30
2 files changed, 62 insertions, 0 deletions
diff --git a/examples/execinspect.rs b/examples/execinspect.rs
new file mode 100644
index 0000000..de92ec7
--- /dev/null
+++ b/examples/execinspect.rs
@@ -0,0 +1,32 @@
+use futures::StreamExt;
+use shiplift::{Docker, Exec, ExecContainerOptions};
+use std::env;
+
+#[tokio::main]
+async fn main() {
+ let docker = Docker::new();
+ let mut args = env::args().skip(1);
+
+ // First argument is container id
+ let id = args.next().expect("You need to specify a container id");
+ // Rest is command to run in the container
+ let cmd = args.collect::<Vec<String>>();
+ println!("{} {:?}", id, cmd);
+
+ // Create options with specified command
+ let opts = ExecContainerOptions::builder()
+ .cmd(cmd.iter().map(String::as_str).collect())
+ .attach_stdout(true)
+ .attach_stderr(true)
+ .build();
+
+ let exec = Exec::create(&docker, &id, &opts).await.unwrap();
+
+ println!("{:#?}", exec.inspect().await.unwrap());
+
+ let mut stream = exec.start();
+
+ stream.next().await;
+
+ println!("{:#?}", exec.inspect().await.unwrap());
+}
diff --git a/examples/execresize.rs b/examples/execresize.rs
new file mode 100644
index 0000000..7c9cf11
--- /dev/null
+++ b/examples/execresize.rs
@@ -0,0 +1,30 @@
+use shiplift::{Docker, Exec, ExecContainerOptions, ExecResizeOptions};
+use std::env;
+
+#[tokio::main]
+async fn main() {
+ let docker = Docker::new();
+ let mut args = env::args().skip(1);
+
+ // First argument is container id
+ let id = args.next().expect("You need to specify a container id");
+ // Second is width
+ let width: u64 = args.next().map_or(Ok(0), |s| s.parse::<u64>()).unwrap();
+ // Third is height
+ let height: u64 = args.next().map_or(Ok(0), |s| s.parse::<u64>()).unwrap();
+
+ // Create an exec instance
+ let exec_opts = ExecContainerOptions::builder()
+ .cmd(vec!["echo", "123"])
+ .attach_stdout(true)
+ .attach_stderr(true)
+ .build();
+ let exec = Exec::create(&docker, &id, &exec_opts).await.unwrap();
+
+ // Resize its window with given parameters
+ let resize_opts = ExecResizeOptions::builder()
+ .width(width)
+ .height(height)
+ .build();
+ exec.resize(&resize_opts).await.unwrap();
+}