checklints 0.2.1

Check repositories against toml file(s) of constraints
Documentation
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
use crate::cli::Cli;

use crate::types::RemoteFile;
use crate::THIS_CRATE_NAME;
use anyhow::{bail, Result};
use log::debug;
use serde::{Deserialize, Serialize};
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::{env, fs};

fn default_user_checklists() -> bool {
    true
}

fn default_fail_fast() -> bool {
    false
}

fn default_no_read_cache() -> bool {
    false
}

fn default_no_write_cache() -> bool {
    false
}

fn default_no_cache() -> bool {
    false
}

fn default_clear_cache() -> bool {
    false
}

fn default_external_checklists() -> Vec<RemoteFile> {
    Vec::new()
}

fn default_external_templates() -> Vec<RemoteFile> {
    Vec::new()
}

pub fn write_default_config(path: &Path) -> Result<()> {
    let config = MaybeSettings::default();
    let contents = toml::to_string(&config)?;
    let mut f = File::create(path)?;
    debug!("Writing default config to {}", path.display());
    write!(f, "{contents}")?;
    Ok(())
}

#[derive(Debug)]
pub struct Settings {
    user_checklists: bool,
    fail_fast: bool,
    no_read_cache: bool,
    no_write_cache: bool,
    clear_cache: bool,
    external_checklists: Vec<RemoteFile>,
    external_templates: Vec<RemoteFile>,
}

impl Settings {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn builder() -> SettingsBuilder {
        SettingsBuilder::new()
    }

    pub fn user_checklists(&self) -> bool {
        self.user_checklists
    }

    pub fn fail_fast(&self) -> bool {
        self.fail_fast
    }

    pub fn no_read_cache(&self) -> bool {
        self.no_read_cache
    }

    pub fn no_write_cache(&self) -> bool {
        self.no_write_cache
    }

    pub fn clear_cache(&self) -> bool {
        self.clear_cache
    }

    pub fn external_checklists(&self) -> &[RemoteFile] {
        &self.external_checklists
    }

    pub fn external_templates(&self) -> &[RemoteFile] {
        &self.external_templates
    }
}

impl Default for Settings {
    fn default() -> Self {
        Self {
            user_checklists: default_user_checklists(),
            fail_fast: default_fail_fast(),
            no_read_cache: default_no_read_cache(),
            no_write_cache: default_no_write_cache(),
            clear_cache: default_clear_cache(),
            external_checklists: default_external_checklists(),
            external_templates: default_external_templates(),
        }
    }
}

#[derive(Debug, Deserialize, Serialize)]
pub struct MaybeSettings {
    user_checklists: Option<bool>,
    fail_fast: Option<bool>,
    no_read_cache: Option<bool>,
    no_write_cache: Option<bool>,
    no_cache: Option<bool>,
    clear_cache: Option<bool>,
    #[serde(default)]
    external_checklists: Vec<RemoteFile>,
    #[serde(default)]
    external_templates: Vec<RemoteFile>,
}

impl MaybeSettings {
    fn to_settings(self) -> Result<Settings> {
        let Some(user_checklists) = self.user_checklists else {
            bail!("Settings option 'user_checklists' not set");
        };
        let Some(fail_fast) = self.fail_fast else {
            bail!("Settings option 'fail_fast' not set");
        };

        let (no_read_cache, no_write_cache) = match self.no_cache {
            Some(no_cache) => {
                // No cache implies no_read and no_write
                if no_cache {
                    (true, true)
                } else {
                    let Some(no_read_cache) = self.no_read_cache else {
                        bail!("Settings option 'no_read_cache' not set");
                    };
                    let Some(no_write_cache) = self.no_write_cache else {
                        bail!("Settings option 'no_write_cache' not set");
                    };
                    (no_read_cache, no_write_cache)
                }
            }
            None => {
                let Some(no_read_cache) = self.no_read_cache else {
                    bail!("Settings option 'no_read_cache' not set");
                };
                let Some(no_write_cache) = self.no_write_cache else {
                    bail!("Settings option 'no_write_cache' not set");
                };
                (no_read_cache, no_write_cache)
            }
        };

        let Some(clear_cache) = self.clear_cache else {
            bail!("Settings option 'clear_cache' not set");
        };

        let external_checklists = self.external_checklists;
        let external_templates = self.external_templates;

        Ok(Settings {
            user_checklists,
            fail_fast,
            no_read_cache,
            no_write_cache,
            clear_cache,
            external_checklists,
            external_templates,
        })
    }
}

impl MaybeSettings {
    fn empty() -> Self {
        Self {
            user_checklists: None,
            fail_fast: None,
            no_read_cache: None,
            no_write_cache: None,
            no_cache: None,
            clear_cache: None,
            external_checklists: Vec::new(),
            external_templates: Vec::new(),
        }
    }

    pub fn layer(&mut self, mut layer: Self) {
        if let Some(enable) = layer.user_checklists {
            self.user_checklists = Some(enable);
        }

        if let Some(enable) = layer.fail_fast {
            self.fail_fast = Some(enable);
        }

        if let Some(enable) = layer.no_read_cache {
            self.no_read_cache = Some(enable);
        }

        if let Some(enable) = layer.no_read_cache {
            self.no_read_cache = Some(enable);
        }

        if let Some(enable) = layer.no_write_cache {
            self.no_write_cache = Some(enable);
        }

        if let Some(enable) = layer.no_cache {
            self.no_cache = Some(enable);
        }

        if let Some(enable) = layer.clear_cache {
            self.clear_cache = Some(enable);
        }

        self.external_checklists
            .append(&mut layer.external_checklists);

        self.external_templates
            .append(&mut layer.external_templates);
    }

    pub fn from_args(args: Cli) -> Self {
        let mut layer = MaybeSettings::empty();

        if args.no_user_checklists {
            layer.user_checklists = Some(false);
        }

        if args.fail_fast {
            layer.fail_fast = Some(true);
        }

        if args.no_write_cache {
            layer.no_write_cache = Some(true);
        }

        if args.no_read_cache {
            layer.no_read_cache = Some(true);
        }

        if args.no_cache {
            layer.no_cache = Some(true);
        }

        if args.clear_cache {
            layer.clear_cache = Some(true);
        }

        layer.external_checklists = args.external_checklist;
        layer.external_templates = args.external_template;

        layer
    }

    pub fn from_env() -> Result<Self> {
        let mut layer = Self::empty();

        let key = "USER_CHECKLISTS";
        if let Ok(user_checklists) = env::var(prefix_key(key)) {
            layer.user_checklists = Some(true);
        }

        let key = "FAIL_FAST";
        if let Ok(fail_fast) = env::var(prefix_key(key)) {
            layer.fail_fast = Some(true);
        }

        let key = "NO_CACHE";
        if let Ok(no_cache) = env::var(prefix_key(key)) {
            layer.no_cache = Some(true);
        }

        let key = "NO_READ_CACHE";
        if let Ok(no_read_cache) = env::var(prefix_key(key)) {
            layer.no_read_cache = Some(true);
        }

        let key = "NO_WRITE_CACHE";
        if let Ok(no_write_cache) = env::var(prefix_key(key)) {
            layer.no_write_cache = Some(true);
        }

        let key = "CLEAR_CACHE";
        if let Ok(clear_cache) = env::var(prefix_key(key)) {
            layer.clear_cache = Some(true);
        }

        Ok(layer)
    }
}

impl Default for MaybeSettings {
    fn default() -> Self {
        Self {
            user_checklists: Some(default_user_checklists()),
            fail_fast: Some(default_fail_fast()),
            no_read_cache: Some(default_no_read_cache()),
            no_write_cache: Some(default_no_write_cache()),
            no_cache: Some(default_no_cache()),
            clear_cache: Some(default_clear_cache()),
            external_checklists: default_external_checklists(),
            external_templates: default_external_templates(),
        }
    }
}

fn prefix_key(key: &str) -> String {
    let prefix = THIS_CRATE_NAME.to_uppercase();
    format!("{prefix}_{key}")
}

#[derive(Default)]
pub struct SettingsBuilder {
    settings: MaybeSettings,
}

impl SettingsBuilder {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn build(self) -> Result<Settings> {
        self.settings.to_settings()
    }

    pub fn env_layer(mut self) -> Result<Self> {
        let layer = MaybeSettings::from_env()?;
        self.settings.layer(layer);
        Ok(self)
    }

    pub fn config_layer(mut self, config_file: &Path) -> Result<Self> {
        let contents = fs::read_to_string(config_file)?;
        let layer: MaybeSettings = toml::from_str(&contents)?;
        self.settings.layer(layer);
        Ok(self)
    }

    pub fn arg_layer(mut self, args: Cli) -> Self {
        let layer = MaybeSettings::from_args(args);
        self.settings.layer(layer);
        self
    }

    pub fn user_checklists(mut self, enable: bool) -> Self {
        self.settings.user_checklists = Some(enable);
        self
    }

    pub fn fail_fast(mut self, enable: bool) -> Self {
        self.settings.fail_fast = Some(enable);
        self
    }

    pub fn no_read_cache(mut self, enable: bool) -> Self {
        self.settings.no_read_cache = Some(enable);
        self
    }

    pub fn no_write_cache(mut self, enable: bool) -> Self {
        self.settings.no_write_cache = Some(enable);
        self
    }

    pub fn no_cache(mut self, enable: bool) -> Self {
        self.settings.no_cache = Some(enable);
        self
    }

    pub fn clear_cache(mut self, enable: bool) -> Self {
        self.settings.clear_cache = Some(enable);
        self
    }

    pub fn add_external_checklist(mut self, checklist: RemoteFile) -> Self {
        self.settings.external_checklists.push(checklist);
        self
    }

    pub fn add_external_template(mut self, template: RemoteFile) -> Self {
        self.settings.external_templates.push(template);
        self
    }

    pub fn set_external_checklists(mut self, checklists: Vec<RemoteFile>) -> Self {
        self.settings.external_checklists = checklists;
        self
    }

    pub fn set_external_templates(mut self, templates: Vec<RemoteFile>) -> Self {
        self.settings.external_templates = templates;
        self
    }
}