diff options
author | softprops <d.tangren@gmail.com> | 2016-01-18 14:08:27 -0500 |
---|---|---|
committer | softprops <d.tangren@gmail.com> | 2016-01-18 14:08:27 -0500 |
commit | 6c474a754f6b6339a10a7dff4dc68096630e3166 (patch) | |
tree | 345623b161c421e4bd3a4e62db0ac5fa9285acf9 /src | |
parent | bf41af93452643651f02d1bb57aef7a5d3b29e83 (diff) |
rust fmt
Diffstat (limited to 'src')
-rw-r--r-- | src/builder.rs | 64 | ||||
-rw-r--r-- | src/lib.rs | 114 | ||||
-rw-r--r-- | src/rep.rs | 30 | ||||
-rw-r--r-- | src/tarball.rs | 8 | ||||
-rw-r--r-- | src/transport.rs | 26 |
5 files changed, 151 insertions, 91 deletions
diff --git a/src/builder.rs b/src/builder.rs index 1d74e26..3031540 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -12,7 +12,7 @@ pub struct PullOptions { impl PullOptions { /// return a new instance of a builder for options - pub fn builder() -> PullOptionsBuilder { + pub fn builder() -> PullOptionsBuilder { PullOptionsBuilder::new() } @@ -33,27 +33,33 @@ pub struct PullOptionsBuilder { impl PullOptionsBuilder { pub fn new() -> PullOptionsBuilder { - PullOptionsBuilder { - ..Default::default() - } + PullOptionsBuilder { ..Default::default() } } - pub fn image<I>(&mut self, img: I) -> &mut PullOptionsBuilder where I: Into<String> { + 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> { + 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> { + 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>{ + pub fn tag<T>(&mut self, t: T) -> &mut PullOptionsBuilder + where T: Into<String> + { self.params.insert("tag", t.into()); self } @@ -73,7 +79,9 @@ impl BuildOptions { /// return a new instance of a builder for options /// path is expected to be a file path to a directory containing a Dockerfile /// describing how to build a Docker image - pub fn builder<S>(path: S) -> BuildOptionsBuilder where S: Into<String> { + pub fn builder<S>(path: S) -> BuildOptionsBuilder + where S: Into<String> + { BuildOptionsBuilder::new(path) } @@ -96,26 +104,31 @@ pub struct BuildOptionsBuilder { impl BuildOptionsBuilder { /// path is expected to be a file path to a directory containing a Dockerfile /// describing how to build a Docker image - pub fn new<S>(path: S) -> BuildOptionsBuilder where S: Into<String>{ - BuildOptionsBuilder { - path: path.into(), - ..Default::default() - } + pub fn new<S>(path: S) -> BuildOptionsBuilder + where S: Into<String> + { + BuildOptionsBuilder { path: path.into(), ..Default::default() } } /// set the name of the docker file. defaults to "DockerFile" - pub fn dockerfile<P>(&mut self, path: P) -> &mut BuildOptionsBuilder where P: Into<String> { + pub fn dockerfile<P>(&mut self, path: P) -> &mut BuildOptionsBuilder + where P: Into<String> + { self.params.insert("dockerfile", path.into()); self } /// tag this image with a name after building it - pub fn tag<T>(&mut self, t: T) -> &mut BuildOptionsBuilder where T: Into<String> { + pub fn tag<T>(&mut self, t: T) -> &mut BuildOptionsBuilder + where T: Into<String> + { self.params.insert("t", t.into()); self } - pub fn remote<R>(&mut self, r: R) -> &mut BuildOptionsBuilder where R: Into<String> { + pub fn remote<R>(&mut self, r: R) -> &mut BuildOptionsBuilder + where R: Into<String> + { self.params.insert("remote", r.into()); self } @@ -145,7 +158,10 @@ impl BuildOptionsBuilder { // todo: buildargs pub fn build(&self) -> BuildOptions { - BuildOptions { path: self.path.clone(), params: self.params.clone() } + BuildOptions { + path: self.path.clone(), + params: self.params.clone(), + } } } @@ -234,7 +250,7 @@ impl ContainerListOptionsBuilder { /// Interface for building a new docker container from an existing image pub struct ContainerOptions { - params: HashMap<&'static str, String> + params: HashMap<&'static str, String>, } impl ContainerOptions { @@ -256,22 +272,18 @@ impl ContainerOptions { #[derive(Default)] pub struct ContainerOptionsBuilder { - params: HashMap<&'static str, String> + params: HashMap<&'static str, String>, } impl ContainerOptionsBuilder { pub fn new(name: &str) -> ContainerOptionsBuilder { let mut params = HashMap::new(); params.insert("Image", name.to_owned()); - ContainerOptionsBuilder { - params: params - } + ContainerOptionsBuilder { params: params } } pub fn build(&self) -> ContainerOptions { - ContainerOptions { - params: self.params.clone() - } + ContainerOptions { params: self.params.clone() } } } @@ -35,8 +35,8 @@ pub mod errors; mod tarball; pub use errors::Error; -pub use builder::{BuildOptions, ContainerOptions, ContainerListOptions, ContainerFilter, EventsOptions, ImageFilter, - ImageListOptions, LogsOptions, PullOptions}; +pub use builder::{BuildOptions, ContainerOptions, ContainerListOptions, ContainerFilter, + EventsOptions, ImageFilter, ImageListOptions, LogsOptions, PullOptions}; use hyper::{Client, Url}; use hyper::header::ContentType; use hyper::net::{HttpsConnector, Openssl}; @@ -45,8 +45,9 @@ use hyperlocal::UnixSocketConnector; use openssl::x509::X509FileType; use openssl::ssl::{SslContext, SslMethod}; use rep::Image as ImageRep; -use rep::{PullOutput, PullInfo, BuildOutput, Change, ContainerCreateInfo, ContainerDetails, Container as ContainerRep, Event, Exit, History, ImageDetails, - Info, SearchResult, Stats, Status, Top, Version}; +use rep::{PullOutput, PullInfo, BuildOutput, Change, ContainerCreateInfo, ContainerDetails, + Container as ContainerRep, Event, Exit, History, ImageDetails, Info, SearchResult, + Stats, Status, Top, Version}; use rustc_serialize::json::{self, Json}; use std::env::{self, VarError}; use std::io::Read; @@ -147,26 +148,26 @@ impl<'a> Images<'a> { try!(tarball::dir(&mut bytes, &opts.path[..])); - let raw = try!(self.docker.stream_post(&path.join("?"), Some((Body::BufBody(&bytes[..], bytes.len()), tar())))); + let raw = try!(self.docker.stream_post(&path.join("?"), + Some((Body::BufBody(&bytes[..], bytes.len()), + tar())))); let it = jed::Iter::new(raw).into_iter().map(|j| { // fixme: better error handling debug!("{:?}", j); let obj = j.as_object().expect("expected json object"); obj.get("stream") - .map(|stream| BuildOutput::Stream( - stream.as_string() - .expect("expected stream to be a string") - .to_owned() - ) - ) - .or(obj.get("error") - .map(|err| BuildOutput::Err( - err.as_string() - .expect("expected error to be a string") - .to_owned() - ) - ) - ).expect("expected build output stream or error") + .map(|stream| { + BuildOutput::Stream(stream.as_string() + .expect("expected stream to be a string") + .to_owned()) + }) + .or(obj.get("error") + .map(|err| { + BuildOutput::Err(err.as_string() + .expect("expected error to be a string") + .to_owned()) + })) + .expect("expected build output stream or error") }); Ok(Box::new(it)) } @@ -199,23 +200,31 @@ impl<'a> Images<'a> { 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 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); + debug!("{:?}", j); let s = json::encode(&j).unwrap(); json::decode::<PullInfo>(&s) - .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() - .expect("expected error to be a string") - .to_owned() - ) - ) - ).expect("expected pull status or error") + .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() + .expect("expected error to be a string") + .to_owned()) + })) + .expect("expected pull status or error") }); Ok(Box::new(it)) } @@ -302,7 +311,10 @@ impl<'a, 'b> Container<'a, 'b> { /// Start the container instance pub fn start(&'a self) -> Result<()> { - self.docker.post(&format!("/containers/{}/start", self.id)[..], None as Option<(&'a str, ContentType)>).map(|_| ()) + self.docker + .post(&format!("/containers/{}/start", self.id)[..], + None as Option<(&'a str, ContentType)>) + .map(|_| ()) } /// Stop the container instance @@ -346,17 +358,24 @@ impl<'a, 'b> Container<'a, 'b> { /// Pause the container instance pub fn pause(&self) -> Result<()> { - self.docker.post(&format!("/containers/{}/pause", self.id)[..], None as Option<(&'a str, ContentType)>).map(|_| ()) + self.docker + .post(&format!("/containers/{}/pause", self.id)[..], + None as Option<(&'a str, ContentType)>) + .map(|_| ()) } /// Unpause the container instance pub fn unpause(&self) -> Result<()> { - self.docker.post(&format!("/containers/{}/unpause", self.id)[..], None as Option<(&'a str, ContentType)>).map(|_| ()) + self.docker + .post(&format!("/containers/{}/unpause", self.id)[..], + None as Option<(&'a str, ContentType)>) + .map(|_| ()) } /// Wait until the container stops pub fn wait(&self) -> Result<Exit> { - let raw = try!(self.docker.post(&format!("/containers/{}/wait", self.id)[..], None as Option<(&'a str, ContentType)>)); + let raw = try!(self.docker.post(&format!("/containers/{}/wait", self.id)[..], + None as Option<(&'a str, ContentType)>)); Ok(try!(json::decode::<Exit>(&raw))) } @@ -518,22 +537,35 @@ impl Docker { } fn get<'a>(&self, endpoint: &str) -> Result<String> { - self.transport.request(Method::Get, endpoint, None as Option<(&'a str, ContentType)>) + self.transport.request(Method::Get, + endpoint, + None as Option<(&'a str, ContentType)>) } - fn post<'a, B>(&'a self, endpoint: &str, body: Option<(B, ContentType)>) -> Result<String> where B: Into<Body<'a>> { + fn post<'a, B>(&'a self, endpoint: &str, body: Option<(B, ContentType)>) -> Result<String> + where B: Into<Body<'a>> + { self.transport.request(Method::Post, endpoint, body) } fn delete<'a>(&self, endpoint: &str) -> Result<String> { - self.transport.request(Method::Delete, endpoint, None as Option<(&'a str, ContentType)>) + self.transport.request(Method::Delete, + endpoint, + None as Option<(&'a str, ContentType)>) } - fn stream_post<'a, B>(&'a self, endpoint: &str, body:Option<(B, ContentType)>) -> Result<Box<Read>> where B: Into<Body<'a>> { + fn stream_post<'a, B>(&'a self, + endpoint: &str, + body: Option<(B, ContentType)>) + -> Result<Box<Read>> + where B: Into<Body<'a>> + { self.transport.stream(Method::Post, endpoint, body) } fn stream_get<'a>(&self, endpoint: &str) -> Result<Box<Read>> { - self.transport.stream(Method::Get, endpoint, None as Option<(&'a str, ContentType)>) + self.transport.stream(Method::Get, + endpoint, + None as Option<(&'a str, ContentType)>) } } @@ -78,7 +78,7 @@ pub struct ContainerDetails { pub ResolvConfPath: String, pub RestartCount: u64, pub State: State, - pub Mounts: Vec<Mount> + pub Mounts: Vec<Mount>, } #[derive(Debug, RustcEncodable, RustcDecodable)] @@ -87,7 +87,7 @@ pub struct Mount { pub Source: String, pub Destination: String, pub Mode: String, - pub RW: bool + pub RW: bool, } #[derive(Debug, RustcEncodable, RustcDecodable)] @@ -101,7 +101,7 @@ pub struct State { pub Pid: u64, pub Restarting: bool, pub Running: bool, - pub StartedAt: String + pub StartedAt: String, } #[derive(Debug, RustcEncodable, RustcDecodable)] @@ -111,9 +111,8 @@ pub struct NetworkSettings { pub Gateway: String, pub IPAddress: String, pub IPPrefixLen: u64, - pub MacAddress: String, -// pub PortMapping: Option<???>, - // pub Ports: Option<???> + pub MacAddress: String, /* pub PortMapping: Option<???>, + * pub Ports: Option<???> */ } #[derive(Debug, RustcEncodable, RustcDecodable)] @@ -150,9 +149,9 @@ pub struct Config { pub Hostname: String, pub Image: String, pub Labels: HashMap<String, String>, -// pub MacAddress: String, + // pub MacAddress: String, pub OnBuild: Option<String>, -// pub NetworkDisabled: bool, + // pub NetworkDisabled: bool, pub OpenStdin: bool, pub StdinOnce: bool, pub Tty: bool, @@ -375,7 +374,7 @@ pub struct Event { #[derive(Debug)] pub enum BuildOutput { Stream(String), - Err(String) + Err(String), } // fixme: all fields are options because PullInfo.progressDefault is sometimes an empty object instead of a null/absent value @@ -383,7 +382,7 @@ pub enum BuildOutput { pub struct ProgressDetail { current: Option<u64>, total: Option<u64>, - status: Option<String> // fixme: it looks like this field isn't deserializing properly + status: Option<String>, // fixme: it looks like this field isn't deserializing properly } #[derive(Clone, Debug, RustcDecodable)] @@ -392,13 +391,18 @@ pub struct PullInfo { pub id: Option<String>, pub status: String, pub progress: Option<String>, - pub progressDetail: Option<ProgressDetail> + pub progressDetail: Option<ProgressDetail>, } #[derive(Debug)] pub enum PullOutput { - Status { id: Option<String>, status: String, progress: Option<String>, progress_detail: Option<ProgressDetail> }, - Err(String) + Status { + id: Option<String>, + status: String, + progress: Option<String>, + progress_detail: Option<ProgressDetail>, + }, + Err(String), } #[derive(Debug)] diff --git a/src/tarball.rs b/src/tarball.rs index e88a434..a631c03 100644 --- a/src/tarball.rs +++ b/src/tarball.rs @@ -7,9 +7,13 @@ use std::io::{self, Write, Read}; use tar::Archive; // todo: this is pretty involved. (re)factor this into its own crate -pub fn dir<W>(buf: W, path: &str) -> io::Result<()> where W: Write { +pub fn dir<W>(buf: W, path: &str) -> io::Result<()> + where W: Write +{ let archive = Archive::new(GzEncoder::new(buf, Compression::Best)); - fn bundle<F>(dir: &Path, f: &F, bundle_dir: bool) -> io::Result<()> where F: Fn(&Path) -> io::Result<()> { + fn bundle<F>(dir: &Path, f: &F, bundle_dir: bool) -> io::Result<()> + where F: Fn(&Path) -> io::Result<()> + { if try!(fs::metadata(dir)).is_dir() { if bundle_dir { try!(f(&dir)); diff --git a/src/transport.rs b/src/transport.rs index 6795f85..1435bc0 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -15,11 +15,9 @@ use std::io::{Read, Write}; use hyperlocal::DomainUrl; pub fn tar() -> ContentType { - ContentType( - mime::Mime( - mime::TopLevel::Application, - mime::SubLevel::Ext(String::from("tar")), - vec![])) + ContentType(mime::Mime(mime::TopLevel::Application, + mime::SubLevel::Ext(String::from("tar")), + vec![])) } /// Transports are types which define the means of communication @@ -47,7 +45,13 @@ impl fmt::Debug for Transport { } impl Transport { - pub fn request<'a, B>(&'a self, method: Method, endpoint: &str, body: Option<(B, ContentType)>) -> Result<String> where B: Into<Body<'a>> { + pub fn request<'a, B>(&'a self, + method: Method, + endpoint: &str, + body: Option<(B, ContentType)>) + -> Result<String> + where B: Into<Body<'a>> + { let mut res = match self.stream(method, endpoint, body) { Ok(r) => r, Err(e) => panic!("failed request {:?}", e), @@ -58,9 +62,13 @@ impl Transport { Ok(body) } - pub fn stream<'c, B>( - &'c self, method: Method, endpoint: &str, body: Option<(B, ContentType)> - ) -> Result<Box<Read>> where B: Into<Body<'c>> { + pub fn stream<'c, B>(&'c self, + method: Method, + endpoint: &str, + body: Option<(B, ContentType)>) + -> Result<Box<Read>> + where B: Into<Body<'c>> + { let req = match *self { Transport::Tcp { ref client, ref host } => { client.request(method, &format!("{}{}", host, endpoint)[..]) |