diff options
author | softprops <d.tangren@gmail.com> | 2016-01-03 19:20:32 -0500 |
---|---|---|
committer | softprops <d.tangren@gmail.com> | 2016-01-03 19:20:32 -0500 |
commit | 72816d70eb847cbed4570f248547a438fe95038e (patch) | |
tree | fc5f3890cb3cc705b02cc197bca7f18529000ba5 | |
parent | 97689b1449f4c67069d52d0f4ffac8228371d098 (diff) |
implement image list options
-rw-r--r-- | examples/images.rs | 15 | ||||
-rw-r--r-- | src/builder.rs | 75 | ||||
-rw-r--r-- | src/lib.rs | 16 |
3 files changed, 100 insertions, 6 deletions
diff --git a/examples/images.rs b/examples/images.rs new file mode 100644 index 0000000..d284a37 --- /dev/null +++ b/examples/images.rs @@ -0,0 +1,15 @@ +extern crate shiplift; + +use shiplift::{Docker, ImageListOptions, ImageFilter}; + +fn main() { + let docker = Docker::new(); + for i in docker.images(). + list( + &ImageListOptions::builder().filter( + vec![ImageFilter::Dangling] + ).build() + ).unwrap() { + println!("image -> {:?}", i) + } +} diff --git a/src/builder.rs b/src/builder.rs index a4d643e..3313000 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -254,3 +254,78 @@ impl LogsOptionsBuilder { } } } + + +/// Filter options for image listings +pub enum ImageFilter { + Dangling, + LabelName(String), + Label(String, String) +} + +#[derive(Default)] +pub struct ImageListOptions { + params: HashMap<&'static str, String> +} + +impl ImageListOptions { + pub fn builder() -> ImageListOptionsBuilder { + ImageListOptionsBuilder::new() + } + pub fn serialize(&self) -> Option<String> { + if self.params.is_empty() { None } + else { + Some(form_urlencoded::serialize(&self.params)) + } + } +} + +#[derive(Default)] +pub struct ImageListOptionsBuilder { + params: HashMap<&'static str, String> +} + +impl ImageListOptionsBuilder { + pub fn new() -> ImageListOptionsBuilder { + ImageListOptionsBuilder { + ..Default::default() + } + } + + pub fn digests(&mut self, d: bool) -> &mut ImageListOptionsBuilder { + self.params.insert("digests", d.to_string()); + self + } + + pub fn all(&mut self, a: bool) -> &mut ImageListOptionsBuilder { + self.params.insert("all", a.to_string()); + self + } + + pub fn filter_name(&mut self, name: &str) -> &mut ImageListOptionsBuilder { + self.params.insert("filter", name.to_owned()); + self + } + + pub fn filter(&mut self, filters: Vec<ImageFilter>) -> &mut ImageListOptionsBuilder { + let mut param = HashMap::new(); + for f in filters { + match f { + ImageFilter::Dangling => param.insert("dangling", vec![true.to_string()]), + ImageFilter::LabelName(n) => param.insert("label", vec![n]), + ImageFilter::Label(n,v) => param.insert("label", vec![format!("{}={}", n, v)]) + }; + + } + // structure is a a json encoded object mapping string keys to a list + // of string values + self.params.insert("filters", json::encode(¶m).unwrap()); + self + } + + pub fn build(&self) -> ImageListOptions { + ImageListOptions { + params: self.params.clone() + } + } +} @@ -26,7 +26,7 @@ pub mod transport; pub mod errors; pub use errors::Error; -pub use builder::{ContainerListOptions, ContainerFilter, EventsOptions, LogsOptions}; +pub use builder::{ContainerListOptions, ContainerFilter, EventsOptions, ImageFilter, ImageListOptions, LogsOptions}; use builder::ContainerBuilder; use hyper::{Client, Url}; @@ -119,8 +119,12 @@ impl<'a> Images<'a> { } /// Lists the docker images on the current docker host - pub fn list(self) -> Result<Vec<ImageRep>> { - let raw = try!(self.docker.get("/images/json")); + pub fn list(&self, opts: &ImageListOptions) -> Result<Vec<ImageRep>> { + let mut path = vec!["/images/json".to_owned()]; + if let Some(query) = opts.serialize() { + path.push(query); + } + let raw = try!(self.docker.get(&path.join("?"))); Ok(try!(json::decode::<Vec<ImageRep>>(&raw))) } @@ -130,21 +134,21 @@ impl<'a> Images<'a> { } /// Search for docker images by term - pub fn search(self, term: &str) -> Result<Vec<SearchResult>> { + pub fn search(&self, term: &str) -> Result<Vec<SearchResult>> { let query = form_urlencoded::serialize(vec![("term", term)]); let raw = try!(self.docker.get(&format!("/images/search?{}", query)[..])); Ok(try!(json::decode::<Vec<SearchResult>>(&raw))) } /// Create a new docker images from an existing image - pub fn create(self, from: &str) -> Result<Box<Read>> { + pub fn create(&self, from: &str) -> Result<Box<Read>> { let query = form_urlencoded::serialize(vec![("fromImage", from)]); self.docker.stream_post(&format!("/images/create?{}", query)[..]) } /// exports a collection of named images, /// either by name, name:tag, or image id, into a tarball - pub fn export(self, names: Vec<&str>) -> Result<Box<Read>> { + pub fn export(&self, names: Vec<&str>) -> Result<Box<Read>> { let params = names.iter() .map(|n| ("names", *n)) .collect::<Vec<(&str, &str)>>(); |