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
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
use crate::{api::Filter, util::url::encoded_pairs};

use std::{
    collections::HashMap,
    path::{Path, PathBuf},
};

use serde::Serialize;

#[derive(Clone, Serialize, Debug)]
#[serde(untagged)]
pub enum RegistryAuth {
    Password {
        username: String,
        password: String,

        #[serde(skip_serializing_if = "Option::is_none")]
        email: Option<String>,

        #[serde(rename = "serveraddress")]
        #[serde(skip_serializing_if = "Option::is_none")]
        server_address: Option<String>,
    },
    Token {
        #[serde(rename = "identitytoken")]
        identity_token: String,
    },
}

impl RegistryAuth {
    /// return a new instance with token authentication
    pub fn token<S>(token: S) -> RegistryAuth
    where
        S: Into<String>,
    {
        RegistryAuth::Token {
            identity_token: token.into(),
        }
    }

    /// return a new instance of a builder for authentication
    pub fn builder() -> RegistryAuthBuilder {
        RegistryAuthBuilder::default()
    }

    /// serialize authentication as JSON in base64
    pub fn serialize(&self) -> String {
        serde_json::to_string(self)
            .map(|c| base64::encode_config(&c, base64::URL_SAFE))
            .unwrap_or_default()
    }
}

#[derive(Default)]
pub struct RegistryAuthBuilder {
    username: Option<String>,
    password: Option<String>,
    email: Option<String>,
    server_address: Option<String>,
}

impl RegistryAuthBuilder {
    pub fn username<I>(&mut self, username: I) -> &mut Self
    where
        I: Into<String>,
    {
        self.username = Some(username.into());
        self
    }

    pub fn password<I>(&mut self, password: I) -> &mut Self
    where
        I: Into<String>,
    {
        self.password = Some(password.into());
        self
    }

    pub fn email<I>(&mut self, email: I) -> &mut Self
    where
        I: Into<String>,
    {
        self.email = Some(email.into());
        self
    }

    pub fn server_address<I>(&mut self, server_address: I) -> &mut Self
    where
        I: Into<String>,
    {
        self.server_address = Some(server_address.into());
        self
    }

    pub fn build(&self) -> RegistryAuth {
        RegistryAuth::Password {
            username: self.username.clone().unwrap_or_default(),
            password: self.password.clone().unwrap_or_default(),
            email: self.email.clone(),
            server_address: self.server_address.clone(),
        }
    }
}

impl_url_opts_builder!(Tag);

impl TagOptsBuilder {
    impl_url_str_field!(repo: R => "repo");

    impl_url_str_field!(tag: T => "tag");
}

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

impl PullOpts {
    /// return a new instance of a builder for Opts
    pub fn builder() -> PullOptsBuilder {
        PullOptsBuilder::default()
    }

    /// serialize Opts as a string. returns None if no Opts are defined
    pub fn serialize(&self) -> Option<String> {
        if self.params.is_empty() {
            None
        } else {
            Some(encoded_pairs(
                self.params
                    .iter()
                    .map(|(k, v)| (k, v.as_str().unwrap_or_default())),
            ))
        }
    }

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

pub struct PullOptsBuilder {
    auth: Option<RegistryAuth>,
    params: HashMap<&'static str, serde_json::Value>,
}

impl Default for PullOptsBuilder {
    fn default() -> Self {
        let mut params = HashMap::new();
        params.insert("tag", serde_json::Value::String("latest".into()));

        PullOptsBuilder { auth: None, params }
    }
}

impl PullOptsBuilder {
    impl_str_field!(
    " Name of the image to pull. The name may include a tag or digest."
    "This parameter may only be used when pulling an image."
    "If an untagged value is provided and no `tag` is provided, _all_"
    "tags will be pulled"
    "The pull is cancelled if the HTTP connection is closed."
    image: I => "fromImage");

    impl_str_field!(src: S => "fromSrc");

    impl_str_field!(
    "Repository name given to an image when it is imported. The repo may include a tag."
    "This parameter may only be used when importing an image."
    ""
    "By default a `latest` tag is added when calling"
    "[PullOptsBuilder::default](PullOptsBuilder::default]."
    repo: S => "repo");

    impl_str_field!(
    "Tag or digest. If empty when pulling an image,"
    "this causes all tags for the given image to be pulled."
    tag: T => "tag");

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

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

#[derive(Default, Debug)]
pub struct BuildOpts {
    pub path: PathBuf,
    params: HashMap<&'static str, String>,
}

impl BuildOpts {
    /// return a new instance of a builder for Opts
    /// path is expected to be a file path to a directory containing a Dockerfile
    /// describing how to build a Docker image
    pub fn builder<P>(path: P) -> BuildOptsBuilder
    where
        P: AsRef<Path>,
    {
        BuildOptsBuilder::new(path)
    }

    /// serialize Opts as a string. returns None if no Opts are defined
    pub fn serialize(&self) -> Option<String> {
        if self.params.is_empty() {
            None
        } else {
            Some(encoded_pairs(&self.params))
        }
    }
}

#[derive(Default)]
pub struct BuildOptsBuilder {
    path: PathBuf,
    params: HashMap<&'static str, String>,
}

impl BuildOptsBuilder {
    /// path is expected to be a file path to a directory containing a Dockerfile
    /// describing how to build a Docker image
    pub(crate) fn new<P>(path: P) -> Self
    where
        P: AsRef<Path>,
    {
        BuildOptsBuilder {
            path: path.as_ref().to_path_buf(),
            ..Default::default()
        }
    }

    impl_url_str_field!("set the name of the docker file. defaults to `DockerFile`" dockerfile: P => "dockerfile");

    impl_url_str_field!("tag this image with a name after building it" tag: T => "t");

    impl_url_str_field!("Extra hosts to add to /etc/hosts." extra_hosts: H => "extrahosts");

    impl_url_str_field!(remote: R => "remote");

    impl_url_bool_field!("Suppress verbose build output." quiet => "q");

    impl_url_bool_field!("Don't use the image cache when building image." nocahe => "nocache");

    impl_url_str_field!("Attempt to pull the image even if an older image exists locally."
                        pull: I => "pull");

    impl_url_bool_field!(rm => "rm");

    impl_url_bool_field!(forcerm => "forcerm");

    impl_url_field!("Set memory limit for build." memory: usize => "memory");

    impl_url_field!("Total memory (memory + swap). Set as -1 to disable swap." memswap: usize => "memswap");

    impl_url_field!("CPU shares (relative weight)." cpu_shares: usize => "cpushares");

    impl_url_str_field!("CPUs in which to allow execution (eg. `0-3`, `0,1`)" cpu_set_cpus: C => "cpusetcpus");

    impl_url_field!("The length of a CPU period in microseconds." cpu_period: usize => "cpuperiod");

    impl_url_field!(
        "Microseconds of CPU time that the container can get in a CPU period."
        cpu_quota: usize => "cpuquota"
    );

    // TODO: buildargs

    impl_url_field!(
        "Size of /dev/shm in bytes. The size must be greater than 0. If omitted the system uses 64MB."
        shm_size: usize => "shmsize"
    );

    impl_url_bool_field!(
        "Squash the resulting images layers into a single layer. (Experimental release only.)"
        squash => "squash"
    );

    impl_url_str_field!(
        "`bridge`, `host`, `none`, `container:<name|id>`, or a custom network name."
        network_mode: T => "networkmode"
    );

    impl_url_str_field!("Platform in the format os[/arch[/variant]]." platform: P => "platform");

    impl_url_str_field!("Target build stage." target: T => "target");

    impl_url_str_field!("BuildKit output configuration." outputs: C => "outputs");

    // TODO: labels

    pub fn build(&self) -> BuildOpts {
        BuildOpts {
            path: self.path.clone(),
            params: self.params.clone(),
        }
    }
}

/// Filter Opts for image listings
pub enum ImageFilter {
    Dangling,
    LabelName(String),
    Label(String, String),
}

impl Filter for ImageFilter {
    fn query_key_val(&self) -> (&'static str, String) {
        match &self {
            ImageFilter::Dangling => ("dangling", true.to_string()),
            ImageFilter::LabelName(n) => ("label", n.to_owned()),
            ImageFilter::Label(n, v) => ("label", format!("{}={}", n, v)),
        }
    }
}

impl_url_opts_builder!(derives = Default | ImageList);

impl ImageListOptsBuilder {
    impl_url_bool_field!(
        "Show all images. Only images from a final layer (no children) are shown by default."
        all => "all"
    );
    impl_url_bool_field!(
        "Show digest information as a RepoDigests field on each image."
        digests => "digests"
    );
    impl_filter_func!(ImageFilter);
}

impl_url_opts_builder!(derives = Default | RmImage);

impl RmImageOptsBuilder {
    impl_url_bool_field!(
        "Remove the image even if it is being used by stopped containers or has other tags."
        force => "force"
    );
    impl_url_bool_field!("Do not delete untagged parent images." noprune => "noprune");
}

impl_url_opts_builder!(derives = Default | ImagePrune);

pub enum ImagesPruneFilter {
    Dangling(bool),
    Until(String),
    LabelKey(String),
    LabelKeyVal(String, String),
}

impl Filter for ImagesPruneFilter {
    fn query_key_val(&self) -> (&'static str, String) {
        use ImagesPruneFilter::*;
        match &self {
            Dangling(dangling) => ("dangling", dangling.to_string()),
            Until(until) => ("until", until.to_owned()),
            LabelKey(label) => ("label", label.to_owned()),
            LabelKeyVal(key, val) => ("label", format!("{}={}", key, val)),
        }
    }
}

impl ImagePruneOptsBuilder {
    impl_filter_func!(ImagesPruneFilter);
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Test registry auth with token
    #[test]
    fn registry_auth_token() {
        let opts = RegistryAuth::token("abc");
        assert_eq!(
            base64::encode(r#"{"identitytoken":"abc"}"#),
            opts.serialize()
        );
    }

    /// Test registry auth with username and password
    #[test]
    fn registry_auth_password_simple() {
        let opts = RegistryAuth::builder()
            .username("user_abc")
            .password("password_abc")
            .build();
        assert_eq!(
            base64::encode(r#"{"username":"user_abc","password":"password_abc"}"#),
            opts.serialize()
        );
    }

    /// Test registry auth with all fields
    #[test]
    fn registry_auth_password_all() {
        let opts = RegistryAuth::builder()
            .username("user_abc")
            .password("password_abc")
            .email("email_abc")
            .server_address("https://example.org")
            .build();
        assert_eq!(
            base64::encode(
                r#"{"username":"user_abc","password":"password_abc","email":"email_abc","serveraddress":"https://example.org"}"#
            ),
            opts.serialize()
        );
    }
}