diff options
author | softprops <d.tangren@gmail.com> | 2016-01-18 13:19:09 -0500 |
---|---|---|
committer | softprops <d.tangren@gmail.com> | 2016-01-18 13:19:09 -0500 |
commit | f681bbe8883556ed3d7ff041d9230295c747cb7b (patch) | |
tree | 1c97d094a736019faba46cffb42edae7fbc3e441 | |
parent | 2139459d574a2704cb0756679c6e41b5232bd247 (diff) |
cleanup
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | examples/imagedelete.rs | 17 | ||||
-rw-r--r-- | examples/imagepull.rs (renamed from examples/imagecreate.rs) | 4 | ||||
-rw-r--r-- | src/builder.rs | 58 | ||||
-rw-r--r-- | src/lib.rs | 18 | ||||
-rw-r--r-- | src/rep.rs | 14 |
6 files changed, 97 insertions, 16 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 182b65f..755c8bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # 0.2.1 (unreleased) * removed `Body` type with a preference for `Into<hyper::client::Body>` +* implemented `Image.build` +* renamed `Image.create` to `Image.pull` to avoid confusion with `Image.build` and added `PullOptions` argument and return type of iterable `PullOutput` # 0.2.0 diff --git a/examples/imagedelete.rs b/examples/imagedelete.rs new file mode 100644 index 0000000..56c35e4 --- /dev/null +++ b/examples/imagedelete.rs @@ -0,0 +1,17 @@ +extern crate shiplift; + +use shiplift::Docker; +use std::env; + +fn main() { + let docker = Docker::new(); + if let Some(img) = env::args().nth(1) { + let image = docker.images() + .get(&img[..]) + .delete() + .unwrap(); + for status in image { + println!("{:?}", status); + } + } +} diff --git a/examples/imagecreate.rs b/examples/imagepull.rs index 2b1b895..2097f82 100644 --- a/examples/imagecreate.rs +++ b/examples/imagepull.rs @@ -1,13 +1,13 @@ extern crate shiplift; -use shiplift::Docker; +use shiplift::{Docker, PullOptions}; use std::env; fn main() { let docker = Docker::new(); if let Some(img) = env::args().nth(1) { let image = docker.images() - .create(&img[..]) + .pull(&PullOptions::builder().image(img).build()) .unwrap(); for output in image { println!("{:?}", output); diff --git a/src/builder.rs b/src/builder.rs index 0bc0fdf..a8406d2 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -6,6 +6,64 @@ use rustc_serialize::json::{self, Json, ToJson}; use url::form_urlencoded; #[derive(Default)] +pub struct PullOptions { + params: HashMap<&'static str, String>, +} + +impl PullOptions { + /// return a new instance of a builder for options + pub fn builder() -> PullOptionsBuilder { + PullOptionsBuilder::new() + } + + /// serialize options as a string. returns None if no options are defined + pub fn serialize(&self) -> Option<String> { + if self.params.is_empty() { + None + } else { + Some(form_urlencoded::serialize(&self.params)) + } + } +} + +#[derive(Default)] +pub struct PullOptionsBuilder { + params: HashMap<&'static str, String>, +} + +impl PullOptionsBuilder { + pub fn new() -> PullOptionsBuilder { + PullOptionsBuilder { + ..Default::default() + } + } + + pub fn image<I>(&mut self, img: I) -> &mut PullOptionsBuilder where I: Into<String> { + self.params.insert("fromImage", img.into()); + self + } + + pub fn src<S>(&mut self, s: S) -> &mut PullOptionsBuilder where S: Into<String> { + self.params.insert("fromSrc", s.into()); + self + } + + pub fn repo<R>(&mut self, r: R) -> &mut PullOptionsBuilder where R: Into<String> { + self.params.insert("repo", r.into()); + self + } + + pub fn tag<T>(&mut self, t: T) -> &mut PullOptionsBuilder where T: Into<String>{ + self.params.insert("tag", t.into()); + self + } + + pub fn build(&self) -> PullOptions { + PullOptions { params: self.params.clone() } + } +} + +#[derive(Default)] pub struct BuildOptions { pub path: String, params: HashMap<&'static str, String>, @@ -36,7 +36,7 @@ mod tarball; pub use errors::Error; pub use builder::{BuildOptions, ContainerOptions, ContainerListOptions, ContainerFilter, EventsOptions, ImageFilter, - ImageListOptions, LogsOptions}; + ImageListOptions, LogsOptions, PullOptions}; use hyper::{Client, Url}; use hyper::header::ContentType; use hyper::net::{HttpsConnector, Openssl}; @@ -193,17 +193,21 @@ impl<'a> Images<'a> { Ok(try!(json::decode::<Vec<SearchResult>>(&raw))) } - // todo: should this be named `pull` to avoid confusion with build? - /// Create a new docker images from an existing image - pub fn create(&self, from: &str) -> Result<Box<Iterator<Item = PullOutput>>> { - let query = form_urlencoded::serialize(vec![("fromImage", from)]); - let raw = try!(self.docker.stream_post(&format!("/images/create?{}", query)[..], None as Option<(&'a str, ContentType)>)); + /// Pull and create a new docker images from an existing image + pub fn pull(&self, opts: &PullOptions) -> Result<Box<Iterator<Item = PullOutput>>> { + let mut path = vec!["/images/create".to_owned()]; + if let Some(query) = opts.serialize() { + path.push(query); + } + let raw = try!(self.docker.stream_post(&path.join("?"), None as Option<(&'a str, ContentType)>)); let it = jed::Iter::new(raw).into_iter().map(|j| { // fixme: better error handling debug!("{:?}",j); let s = json::encode(&j).unwrap(); json::decode::<PullInfo>(&s) - .map(|info| PullOutput::Status(info)).ok() + .map(|info| PullOutput::Status { + id: info.id, status: info.status, progress: info.progress, progress_detail: info.progressDetail + }).ok() .or(j.as_object().expect("expected json object").get("error") .map(|err| PullOutput::Err( err.as_string() @@ -379,25 +379,25 @@ pub enum BuildOutput { } // fixme: all fields are options because PullInfo.progressDefault is sometimes an empty object instead of a null/absent value -#[derive(Debug, RustcDecodable)] +#[derive(Clone, Debug, RustcDecodable)] pub struct ProgressDetail { current: Option<u64>, total: Option<u64>, status: Option<String> // fixme: it looks like this field isn't deserializing properly } -#[derive(Debug, RustcDecodable)] +#[derive(Clone, Debug, RustcDecodable)] #[allow(non_snake_case)] pub struct PullInfo { - id: Option<String>, - status: String, - progress: Option<String>, - progressDetail: Option<ProgressDetail> + pub id: Option<String>, + pub status: String, + pub progress: Option<String>, + pub progressDetail: Option<ProgressDetail> } #[derive(Debug)] pub enum PullOutput { - Status(PullInfo), + Status { id: Option<String>, status: String, progress: Option<String>, progress_detail: Option<ProgressDetail> }, Err(String) } |