check-config 0.9.12

Check configuration files.
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
use std::{collections::HashMap, env, str::FromStr};

use base::CheckConstructor;
use url::Url;

use crate::uri::{ReadPath, ReadablePath};

use self::base::{CheckDefinitionError, Checker};

pub(crate) mod base;
pub(crate) mod file;
pub(crate) mod git;
pub(crate) mod package;
pub(crate) mod test_helpers;
pub(crate) mod utils;

pub(crate) trait RelativeUrl {
    fn short_url_str(&self) -> String;
}

impl RelativeUrl for ReadablePath {
    fn short_url_str(&self) -> String {
        let cwd_url = url::Url::parse(&format!(
            "file://{}",
            env::current_dir()
                .unwrap()
                .into_os_string()
                .into_string()
                .unwrap()
        ))
        .unwrap();
        match cwd_url.make_relative(self.as_ref()) {
            Some(relative_url) => relative_url,
            None => self.as_ref().as_str().to_owned(),
        }
    }
}

#[derive(Debug, Clone)]
pub(crate) struct GenericChecker {
    // path to the file where the checkers are defined
    pub(crate) file_with_checks: ReadablePath,
    // overridden file type
    pub(crate) tags: Vec<String>,
    // check_only
    pub(crate) check_only: bool,
    // variables which are present and can be used for templating
    // this is a owned hashmap to make sure that only variables
    // which are read before the definition of this checker are used
    pub(crate) variables: HashMap<String, String>,
}

impl GenericChecker {
    fn file_with_checks(&self) -> &ReadablePath {
        &self.file_with_checks
    }
}

fn read_tags_from_table(
    check_table: &toml_edit::Table,
) -> Result<Vec<String>, CheckDefinitionError> {
    let mut tags = Vec::new();
    match check_table.get("tags") {
        None => Ok(tags),
        Some(item) => {
            if !item.is_array() {
                Err(CheckDefinitionError::InvalidDefinition(
                    "`tags` is not an array".into(),
                ))
            } else {
                for i in item.as_array().unwrap() {
                    if let Some(value) = i.as_str() {
                        tags.push(value.into());
                    } else {
                        return Err(CheckDefinitionError::InvalidDefinition(
                            "`tags` contains a value which is not a string".to_string(),
                        ));
                    };
                }

                Ok(tags)
            }
        }
    }
}

fn get_option_boolean_from_check_table(
    check_table: &toml_edit::Table,
    key: &str,
) -> Result<Option<bool>, CheckDefinitionError> {
    match check_table.get(key) {
        None => Ok(None),
        Some(value) => match value.as_bool() {
            Some(value) => Ok(Some(value)),
            None => Err(CheckDefinitionError::InvalidDefinition(format!(
                "{key} is not a boolean",
            ))),
        },
    }
}

fn get_check_from_check_table(
    file_with_checks: &ReadablePath,
    check_type: &str,
    check_table: &toml_edit::Table,
    variables: HashMap<String, String>,
) -> Result<Box<dyn Checker>, CheckDefinitionError> {
    let check_table = check_table.clone();

    let tags = read_tags_from_table(&check_table)?;

    let check_only =
        (get_option_boolean_from_check_table(&check_table, "check_only")?).unwrap_or(false);

    let generic_check = GenericChecker {
        file_with_checks: file_with_checks.clone(),
        tags,
        check_only,
        variables,
    };
    match check_type {
        "entry_absent" => Ok(Box::new(file::entry_absent::EntryAbsent::from_check_table(
            generic_check,
            check_table,
        )?)),
        "entry_present" => Ok(Box::new(
            file::entry_present::EntryPresent::from_check_table(generic_check, check_table)?,
        )),
        "file_absent" => Ok(Box::new(file::file_absent::FileAbsent::from_check_table(
            generic_check,
            check_table,
        )?)),
        "file_present" => Ok(Box::new(file::file_present::FilePresent::from_check_table(
            generic_check,
            check_table,
        )?)),
        "file_copied" => Ok(Box::new(file::file_copied::FileCopied::from_check_table(
            generic_check,
            check_table,
        )?)),
        "dir_copied" => Ok(Box::new(file::dir_copied::DirCopied::from_check_table(
            generic_check,
            check_table,
        )?)),
        "dir_present" => Ok(Box::new(file::dir_present::DirPresent::from_check_table(
            generic_check,
            check_table,
        )?)),
        "dir_absent" => Ok(Box::new(file::dir_absent::DirAbsent::from_check_table(
            generic_check,
            check_table,
        )?)),
        "file_unpacked" => Ok(Box::new(
            file::file_unpacked::FileUnpacked::from_check_table(generic_check, check_table)?,
        )),
        "lines_absent" => Ok(Box::new(file::lines_absent::LinesAbsent::from_check_table(
            generic_check,
            check_table,
        )?)),
        "lines_present" => Ok(Box::new(
            file::lines_present::LinesPresent::from_check_table(generic_check, check_table)?,
        )),
        "package_present" => Ok(Box::new(
            package::package_present::PackagePresent::from_check_table(generic_check, check_table)?,
        )),
        "package_absent" => Ok(Box::new(
            package::package_absent::PackageAbsent::from_check_table(generic_check, check_table)?,
        )),
        "key_value_present" => Ok(Box::new(
            file::key_value_present::KeyValuePresent::from_check_table(
                generic_check,
                check_table.clone(),
            )?,
        )),
        "key_absent" => Ok(Box::new(file::key_absent::KeyAbsent::from_check_table(
            generic_check,
            check_table.clone(),
        )?)),
        "key_value_regex_matched" => Ok(Box::new(
            file::key_value_regex_match::EntryRegexMatched::from_check_table(
                generic_check,
                check_table.clone(),
            )?,
        )),
        "git_fetched" => Ok(Box::new(git::GitFetched::from_check_table(
            generic_check,
            check_table.clone(),
        )?)),
        _ => {
            log::error!("unknown check {check_type} {check_table}");
            Err(CheckDefinitionError::UnknownCheckType(
                check_type.to_string(),
            ))
        }
    }
}

pub(crate) fn read_checks_from_path(
    file_with_checks: &ReadablePath,
    variables: &mut HashMap<String, String>,
) -> Vec<Box<dyn Checker>> {
    let mut checks: Vec<Box<dyn Checker>> = vec![];
    let mut file_with_checks = file_with_checks.clone();
    let checks_toml_str = match file_with_checks.read_to_string() {
        Ok(checks_toml) => checks_toml,
        Err(_) => {
            let uri = match Url::parse(
                format!("{}/check-config.toml", file_with_checks.as_ref()).as_str(),
            ) {
                Ok(uri) => uri,
                Err(_) => {
                    log::error!("âš  {file_with_checks} could not be read");
                    return checks;
                }
            };
            file_with_checks = ReadablePath::from_url(uri);
            match file_with_checks.read_to_string() {
                Ok(checks_toml) => checks_toml,
                Err(_) => {
                    log::error!("âš  {file_with_checks} could not be read");
                    return checks;
                }
            }
        }
    };

    let mut checks_toml: toml_edit::Table =
        match toml_edit::DocumentMut::from_str(checks_toml_str.as_str()) {
            Ok(checks_toml) => checks_toml.as_table().to_owned(),
            Err(e) => {
                log::error!("Invalid toml file {file_with_checks} {e}");
                return checks;
            }
        };

    let top_level_keys = if file_with_checks.as_ref().path().ends_with("pyproject.toml") {
        vec!["tool", "check-config"]
    } else if file_with_checks.as_ref().path().ends_with("Cargo.toml") {
        vec!["package", "metadata", "check-config"]
    } else {
        vec![]
    };

    for key in top_level_keys {
        checks_toml = match checks_toml.get(key) {
            Some(toml) => match toml.as_table() {
                Some(toml) => toml.clone(),
                None => {
                    log::error!("Top level key {key} in {file_with_checks} is not a table");
                    return vec![];
                }
            },
            None => {
                log::error!("Top level key {key} is not found in {file_with_checks}");
                return vec![];
            }
        }
    }

    for (key, value) in checks_toml {
        if key == "include" {
            if let toml_edit::Item::Value(toml_edit::Value::Array(include_uris)) = value {
                for include_uri in include_uris {
                    let include_path = match ReadablePath::from_string(
                        include_uri.as_str().expect("uri is a string"),
                        Some(&file_with_checks),
                    ) {
                        Ok(include_path) => include_path,
                        Err(_) => {
                            log::error!("{include_uri} is not a valid uri");
                            std::process::exit(1);
                        }
                    };
                    checks.extend(read_checks_from_path(&include_path, variables));
                }
            }

            continue;
        }
        if key == "variables" {
            if let toml_edit::Item::Table(current_variables) = &value {
                current_variables.iter().for_each(|(k, v)| {
                    let v = v.as_str().expect("value is a string");
                    // TODO: fix error or convert when value is not a string
                    variables.insert(k.to_string(), v.to_string());
                });
            }

            continue;
        }

        let check_type = key;
        let mut checks_to_add = vec![];
        match value {
            toml_edit::Item::Table(config_table) => {
                checks_to_add.push(get_check_from_check_table(
                    &file_with_checks,
                    check_type.as_str(),
                    &config_table,
                    variables.clone(),
                ));
            }
            toml_edit::Item::ArrayOfTables(array) => {
                for config_table in array {
                    checks_to_add.push(get_check_from_check_table(
                        &file_with_checks,
                        check_type.as_str(),
                        &config_table,
                        variables.clone(),
                    ));
                }
            }
            _ => {}
        }

        for check in checks_to_add {
            match check {
                Ok(check) => checks.push(check),
                Err(err) => {
                    log::error!("Checkfile {file_with_checks}:{check_type} has errors: {err}")
                }
            }
        }
    }
    checks
}

#[cfg(test)]
mod test {
    use super::*;
    use std::fs::File;
    use std::io::Write;
    use tempfile::tempdir;

    #[test]
    fn test_read_checks_from_path() {
        let dir = tempdir().unwrap();
        let path_with_checkers = dir.path().join("check-config.toml");
        let mut file_with_checkers = File::create(&path_with_checkers).unwrap();

        writeln!(
            file_with_checkers,
            r#"
include = []  # optional list of toml files with additional checks

[[file_absent]]
file = "test/absent_file"

[[file_present]]
file = "test/present_file"

[[key_absent]]
file = "test/present.toml"
key.key = "key"

[[key_value_present]]
file = "test/present.toml"
key.key1 = 1

[key_value_regex_matched]
file = "test/present.toml"
key.key = 'v.*'

[[lines_absent]]
file = "test/present.txt"
lines = """\
multi
line"""

[[lines_present]]
file = "test/present.txt"
lines = """\
multi
line"""

[[entry_present]]
file = "test/present.toml"
entry.key = [1,2,3]

[[entry_absent]]
file = "test/present.toml"
entry.key = [1,2,3]
        "#
        )
        .expect("file is created");

        let mut variables = HashMap::new();
        let path_with_checkers = ReadablePath::from_string(
            &format!("file://{}", path_with_checkers.to_str().unwrap()),
            None,
        )
        .unwrap();
        let checks = read_checks_from_path(&path_with_checkers, &mut variables);

        assert_eq!(checks.len(), 9);
    }

    #[test]
    fn test_read_invalid_checks_from_path() {
        let dir = tempdir().unwrap();
        let path_with_checkers = dir.path().join("check-config.toml");
        let mut file_with_checkers = File::create(&path_with_checkers).unwrap();

        writeln!(
            file_with_checkers,
            r#"
["test/absent_file".fileXabsent]

        "#
        )
        .expect("write is succsful");

        let mut variables = HashMap::new();

        let path_with_checkers = ReadablePath::from_string(
            &format!("file://{}", path_with_checkers.to_str().unwrap()),
            None,
        )
        .unwrap();
        let checks = read_checks_from_path(&path_with_checkers, &mut variables);

        assert_eq!(checks.len(), 0);
    }
}