1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use std::path::PathBuf;

use oci_distribution::secrets::RegistryAuth;

#[derive(Default)]
#[must_use]
pub struct OciOptions {
  pub(crate) allow_latest: bool,
  pub(crate) allow_insecure: Vec<String>,
  pub(crate) username: Option<String>,
  pub(crate) password: Option<String>,
  pub(crate) base_dir: Option<PathBuf>,
  pub(crate) cache_dir: Option<PathBuf>,
  pub(crate) overwrite: bool,
}

impl OciOptions {
  pub fn allow_latest(mut self, allow_latest: bool) -> Self {
    self.allow_latest = allow_latest;
    self
  }

  pub fn allow_insecure(mut self, allow_insecure: Vec<String>) -> Self {
    self.allow_insecure = allow_insecure;
    self
  }

  pub fn cache_dir(mut self, cache_dir: Option<PathBuf>) -> Self {
    self.cache_dir = cache_dir;
    self
  }

  pub fn base_dir(mut self, base_dir: Option<PathBuf>) -> Self {
    self.base_dir = base_dir;
    self
  }

  pub fn username(mut self, username: Option<String>) -> Self {
    self.username = username;
    self
  }

  pub fn password(mut self, password: Option<String>) -> Self {
    self.password = password;
    self
  }

  pub fn overwrite(mut self, overwrite: bool) -> Self {
    self.overwrite = overwrite;
    self
  }

  #[must_use]
  pub fn get_cache_dir(&self) -> Option<&PathBuf> {
    self.cache_dir.as_ref()
  }

  #[must_use]
  pub fn get_base_dir(&self) -> Option<&PathBuf> {
    self.base_dir.as_ref()
  }

  #[must_use]
  pub fn get_auth(&self) -> RegistryAuth {
    match (self.username.as_ref(), self.password.as_ref()) {
      (Some(username), Some(password)) => RegistryAuth::Basic(username.clone(), password.clone()),
      _ => RegistryAuth::Anonymous,
    }
  }
}

impl std::fmt::Debug for OciOptions {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.debug_struct("OciOptions")
      .field("allow_latest", &self.allow_latest)
      .field("allow_insecure", &self.allow_insecure)
      .field("username", &self.username)
      .field("password", &self.password.as_ref().map(|_| "********"))
      .finish()
  }
}