Skip to main content

nanite_docker/instruction/run/
runmount.rs

1// WARNING: This is even worse than mod.rs. Most of the RUN instruction is implemented here
2// If you get easily overwhelmed, please look at mod.rs's `pub use` first just to see how much is here
3// As said in the comment at the top of mod.rs, this isn't a good example of how the system works.
4// ENTER AT YOUR OWN RISK
5
6// If your first reaction is to complain about the repetition, look at the note in instruction/mod.rs. The repetition is intentional.
7
8use alloc::string::String;
9use alloc::vec::Vec;
10use core::fmt::{Display, Formatter};
11
12/// Describes a --mount option for the RUN command
13#[derive(Debug, Clone)]
14pub enum RunMount {
15    Bind(RunMountBind),
16    Cache(RunMountCache),
17    Tmpfs(RunMountTmpfs),
18    Secret(RunMountSecret),
19    Ssh(RunMountSSH),
20}
21
22#[derive(Debug, Clone)]
23pub struct RunMountBind {
24    pub target: String,
25    pub opts: Vec<RunMountBindOpts>,
26}
27impl Display for RunMountBind {
28    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
29        write!(f, "--mount=type=bind")?;
30        write!(f, ",target={}", self.target)?;
31
32        for i in &self.opts {
33            write!(f, ",{i}")?;
34        }
35
36        Ok(())
37    }
38}
39
40#[derive(Debug, Clone)]
41pub enum RunMountBindOpts {
42    From(String),
43    ReadWrite,
44}
45
46impl Display for RunMountBindOpts {
47    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
48        match self {
49            RunMountBindOpts::From(s) => write!(f, "from={}", s),
50            RunMountBindOpts::ReadWrite => write!(f, "readwrite=true"),
51        }
52    }
53}
54
55#[derive(Debug, Clone)]
56pub struct RunMountCache {
57    pub target: String,
58    pub opts: Vec<RunMountCacheOpts>,
59}
60impl Display for RunMountCache {
61    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
62        write!(f, "--mount=type=cache")?;
63        write!(f, ",target={}", self.target)?;
64
65        for i in &self.opts {
66            write!(f, ",{i}")?;
67        }
68
69        Ok(())
70    }
71}
72
73#[derive(Debug, Clone)]
74pub enum RunMountCacheOpts {
75    Id(String),
76    ReadOnly,
77    Sharing(RunSharing),
78    From(String),
79    Source(String),
80    Mode(String), // This is in octal, may be changed later if a better octal representation is found
81    Uid(usize),
82    Gid(usize),
83}
84
85impl Display for RunMountCacheOpts {
86    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
87        match self {
88            Self::Id(i) => write!(f, "id={i}"),
89            Self::ReadOnly => write!(f, "readonly=true"),
90            Self::Sharing(s) => write!(f, "sharing={s}"),
91            Self::From(r) => write!(f, "from={r}"),
92            Self::Source(s) => write!(f, "source={s}"),
93            Self::Mode(m) => write!(f, "mode={m}"),
94            Self::Uid(u) => write!(f, "uid={u}"),
95            Self::Gid(g) => write!(f, "gid={g}"),
96        }
97    }
98}
99
100#[derive(Debug, Clone)]
101pub enum RunSharing {
102    Shared,
103    Private,
104    Locked,
105}
106
107impl Display for RunSharing {
108    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
109        match self {
110            Self::Shared => write!(f, "shared"),
111            Self::Private => write!(f, "private"),
112            Self::Locked => write!(f, "locked"),
113        }
114    }
115}
116
117#[derive(Clone, Debug)]
118pub struct RunMountTmpfs {
119    pub target: String,
120    pub opts: Vec<RunMountTmpfsOpts>, // Ehehe, a vec of only one possible option
121}
122
123impl Display for RunMountTmpfs {
124    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
125        write!(f, "--mount=type=tmpfs")?;
126        write!(f, ",target={}", self.target)?;
127
128        for i in &self.opts {
129            write!(f, ",{i}")?;
130        }
131
132        Ok(())
133    }
134}
135
136// Yes I know this has only one varient, but it means if docker adds more options in the future, we can easily add them here
137#[derive(Clone, Debug)]
138pub enum RunMountTmpfsOpts {
139    Size(String), // I'm not sure what docker uses for size, a new system may be released to better type sizes
140}
141
142impl Display for RunMountTmpfsOpts {
143    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
144        match self {
145            Self::Size(s) => write!(f, "size={s}"),
146        }
147    }
148}
149
150#[derive(Clone, Debug)]
151pub struct RunMountSecret {
152    pub target: Option<String>,
153    pub opts: Vec<RunMountSecretOpts>,
154}
155impl Display for RunMountSecret {
156    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
157        write!(f, "--mount=type=secret")?;
158        if let Some(t) = &self.target {
159            write!(f, ",source={t}")?;
160        }
161
162        for i in &self.opts {
163            write!(f, ",{i}")?;
164        }
165
166        Ok(())
167    }
168}
169
170#[derive(Clone, Debug)]
171pub enum RunMountSecretOpts {
172    Id(String),
173    Env(String),
174    Required,
175    Mode(String), // See the mention of octal in RunMountCacheOpts::Mode
176    Uid(usize),
177    Gid(usize),
178}
179impl Display for RunMountSecretOpts {
180    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
181        match self {
182            Self::Id(i) => write!(f, "id={i}"),
183            Self::Env(e) => write!(f, "env={e}"),
184            Self::Required => write!(f, "required=true"),
185            Self::Mode(m) => write!(f, "mode={m}"),
186            Self::Uid(u) => write!(f, "uid={u}"),
187            Self::Gid(g) => write!(f, "gid={g}"),
188        }
189    }
190}
191
192#[derive(Clone, Debug)]
193pub struct RunMountSSH {
194    pub target: Option<String>,
195    pub opts: Vec<RunMountSSHOpts>,
196}
197impl Display for RunMountSSH {
198    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
199        write!(f, "--mount=type=ssh")?;
200        if let Some(target) = &self.target {
201            write!(f, ",target={}", target)?;
202        }
203
204        for i in &self.opts {
205            write!(f, ",{i}")?;
206        }
207
208        Ok(())
209    }
210}
211
212#[derive(Clone, Debug)]
213pub enum RunMountSSHOpts {
214    Id(String),
215    Required,
216    Mode(String), // See the mention of octal in RunMountCacheOpts::Mode
217    Uid(usize),
218    Gid(usize),
219}
220impl Display for RunMountSSHOpts {
221    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
222        match self {
223            Self::Id(i) => write!(f, "id={i}"),
224            Self::Required => write!(f, "required=true"),
225            Self::Mode(m) => write!(f, "mode={m}"),
226            Self::Uid(u) => write!(f, "uid={u}"),
227            Self::Gid(g) => write!(f, "gid={g}"),
228        }
229    }
230}