Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds the ability to push images to remote repository #312

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,17 @@ impl Docker {
.await
}

pub(crate) async fn post_with_headers<'a, H>(
&self,
endpoint: &str,
body: Option<(Body, Mime)>,
headers: Option<H>,
) -> Result<String> where H: IntoIterator<Item = (&'static str, String)> + 'a{
self.transport
.request(Method::POST, endpoint, body, headers)
.await
}

pub(crate) async fn put(
&self,
endpoint: &str,
Expand Down
69 changes: 69 additions & 0 deletions src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,20 @@ impl<'docker> Images<'docker> {
Box::pin(self.docker.stream_post_into(path.join("?"), None, headers))
}

pub async fn push(&self, image : &str, push_options : &PushOptions) -> Result<()> {
let mut path = vec![format!("/images/{}/push", image)];
if let Some(query) = push_options.serialize() {
path.push(query)
}

let headers = push_options
.auth_header()
.map(|a| iter::once(("X-Registry-Auth", a)));

let _ = self.docker.post_with_headers(&path.join("?"), None, headers).await?;
Ok(())
}

/// exports a collection of named images,
/// either by name, name:tag, or image id, into a tarball
///
Expand Down Expand Up @@ -768,6 +782,61 @@ impl ImageListOptionsBuilder {
}
}

#[derive(Default, Debug)]
pub struct PushOptions {
auth: Option<RegistryAuth>,
params: HashMap<&'static str, String>,
}

impl PushOptions {

pub fn builder() -> PushOptionsBuilder {
PushOptionsBuilder::default()
}

fn serialize(&self) -> Option<String> {
if self.params.is_empty() {
None
} else {
Some(
form_urlencoded::Serializer::new(String::new())
.extend_pairs(&self.params)
.finish(),
)
}
}

fn auth_header(&self) -> Option<String> {
self.auth.clone().map(|a| a.serialize())
}
}

#[derive(Default)]
pub struct PushOptionsBuilder {
auth: Option<RegistryAuth>,
params: HashMap<&'static str, String>,
}

impl PushOptionsBuilder {

pub fn tag(&mut self, t: String) -> &mut Self {
self.params.insert("tag", t);
self
}

pub fn auth(&mut self, auth: RegistryAuth) -> &mut Self {
self.auth = Some(auth);
self
}

pub fn build(&mut self) -> PushOptions {
PushOptions {
auth: self.auth.take(),
params: self.params.clone(),
}
}
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SearchResult {
pub description: String,
Expand Down