From 4727f283e6fc73f4a3e2498dc4c89f6e7d17df93 Mon Sep 17 00:00:00 2001 From: Joxit Date: Tue, 31 Jan 2017 21:36:45 +0100 Subject: [ExecContainer] Add env option for exec command and update example --- examples/containerexec.rs | 14 +++++++++----- src/builder.rs | 9 +++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/examples/containerexec.rs b/examples/containerexec.rs index 25475da..fe542f2 100644 --- a/examples/containerexec.rs +++ b/examples/containerexec.rs @@ -5,12 +5,16 @@ use std::env; fn main() { let docker = Docker::new(); - let options = ExecContainerOptions::builder().cmd(vec!["ls"]).build(); + let options = ExecContainerOptions::builder() + .cmd(vec!["ls"]) + .env(vec!["VAR=value"]) + .build(); if let Some(id) = env::args().nth(1) { - let container = docker.containers() + match docker.containers() .get(&id) - .exec(&options) - .unwrap(); - println!("{:?}", container); + .exec(&options) { + Ok(res) => println!("Success: {:?}", res), + Err(err) => println!("An error occured: {:?}", err), + } } } diff --git a/src/builder.rs b/src/builder.rs index 4b80e57..c13dca6 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -458,6 +458,7 @@ impl ExecContainerOptionsBuilder { ExecContainerOptionsBuilder { params: HashMap::new() } } + /// Command to run, as an array of strings pub fn cmd(&mut self, cmds: Vec<&str>) -> &mut ExecContainerOptionsBuilder { for cmd in cmds { self.params.entry("Cmd").or_insert(Vec::new()).push(cmd.to_owned()); @@ -465,6 +466,14 @@ impl ExecContainerOptionsBuilder { self } + /// A list of environment variables in the form "VAR=value" + pub fn env(&mut self, envs: Vec<&str>) -> &mut ExecContainerOptionsBuilder { + for env in envs { + self.params.entry("Env").or_insert(Vec::new()).push(env.to_owned()); + } + self + } + pub fn build(&self) -> ExecContainerOptions { ExecContainerOptions { params: self.params.clone() } } -- cgit v1.2.3