diff options
-rw-r--r-- | src/builder.rs | 45 | ||||
-rw-r--r-- | src/lib.rs | 27 |
2 files changed, 71 insertions, 1 deletions
diff --git a/src/builder.rs b/src/builder.rs index 7eb8eef..4b80e57 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -425,6 +425,51 @@ impl ContainerOptionsBuilder { } } +pub struct ExecContainerOptions { + params: HashMap<&'static str, Vec<String>>, +} + +impl ExecContainerOptions { + /// return a new instance of a builder for options + pub fn builder() -> ExecContainerOptionsBuilder { + ExecContainerOptionsBuilder::new() + } + + /// serialize options as a string. returns None if no options are defined + pub fn serialize(&self) -> Result<String> { + let mut body = BTreeMap::new(); + + for (k, v) in &self.params { + body.insert(k.to_string(), v.to_json()); + } + + let json_obj: Json = body.to_json(); + Ok(try!(json::encode(&json_obj))) + } +} + +#[derive(Default)] +pub struct ExecContainerOptionsBuilder { + params: HashMap<&'static str, Vec<String>>, +} + +impl ExecContainerOptionsBuilder { + pub fn new() -> ExecContainerOptionsBuilder { + ExecContainerOptionsBuilder { params: HashMap::new() } + } + + 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()); + } + self + } + + pub fn build(&self) -> ExecContainerOptions { + ExecContainerOptions { params: self.params.clone() } + } +} + /// Options for filtering streams of Docker events #[derive(Default)] pub struct EventsOptions { @@ -37,7 +37,7 @@ mod tarball; pub use errors::Error; pub use builder::{BuildOptions, ContainerOptions, ContainerListOptions, ContainerFilter, EventsOptions, ImageFilter, ImageListOptions, LogsOptions, - PullOptions, RmContainerOptions + PullOptions, RmContainerOptions, ExecContainerOptions }; use hyper::{Client, Url}; use hyper::header::ContentType; @@ -418,6 +418,31 @@ impl<'a, 'b> Container<'a, 'b> { Ok(()) } + // Exec the specified command in the container + pub fn exec(&self, opts: &ExecContainerOptions) -> Result<()> { + let data = try!(opts.serialize()); + let mut bytes = data.as_bytes(); + match self.docker + .post(&format!("/containers/{}/exec", self.id)[..], + Some((&mut bytes, ContentType::json()))) { + Err(e) => Err(e), + Ok(res) => { + let data = "{}"; + let mut bytes = data.as_bytes(); + self.docker + .post(&format!("/exec/{}/start", + Json::from_str(res.as_str()) + .unwrap() + .search("Id") + .unwrap() + .as_string() + .unwrap())[..], + Some((&mut bytes, ContentType::json()))) + .map(|_| ()) + } + } + } + // todo attach, attach/ws, copy, archive } |