use crate::errors::Result;
use crate::Client;
#[derive(Debug)]
pub struct Config {
index: String,
insecure_registry: bool,
user_agent: Option<String>,
username: Option<String>,
password: Option<String>,
accept_invalid_certs: bool,
}
impl Config {
pub fn default() -> Self {
Self {
index: "registry-1.docker.io".into(),
insecure_registry: false,
accept_invalid_certs: false,
user_agent: Some(crate::USER_AGENT.to_owned()),
username: None,
password: None,
}
}
pub fn registry(mut self, reg: &str) -> Self {
self.index = reg.to_owned();
self
}
pub fn insecure_registry(mut self, insecure: bool) -> Self {
self.insecure_registry = insecure;
self
}
pub fn accept_invalid_certs(mut self, accept_invalid_certs: bool) -> Self {
self.accept_invalid_certs = accept_invalid_certs;
self
}
pub fn user_agent(mut self, user_agent: Option<String>) -> Self {
self.user_agent = user_agent;
self
}
pub fn username(mut self, user: Option<String>) -> Self {
self.username = user;
self
}
pub fn password(mut self, password: Option<String>) -> Self {
self.password = password;
self
}
pub fn read_credentials<T: ::std::io::Read>(mut self, reader: T) -> Self {
if let Ok(creds) = crate::get_credentials(reader, &self.index) {
self.username = creds.0;
self.password = creds.1;
};
self
}
pub fn build(self) -> Result<Client> {
let base = if self.insecure_registry {
"http://".to_string() + &self.index
} else {
"https://".to_string() + &self.index
};
trace!(
"Built client for {:?}: endpoint {:?} - user {:?}",
self.index,
base,
self.username
);
let creds = match (self.username, self.password) {
(None, None) => None,
(u, p) => Some((
u.unwrap_or_else(|| "".into()),
p.unwrap_or_else(|| "".into()),
)),
};
let client = reqwest::blocking::ClientBuilder::new()
.danger_accept_invalid_certs(self.accept_invalid_certs)
.build()?;
let c = Client {
base_url: base,
credentials: creds,
index: self.index,
user_agent: self.user_agent,
auth: None,
client,
};
Ok(c)
}
}