syncable-cli 0.37.1

A Rust-based CLI that analyzes code repositories and generates Infrastructure as Code configurations
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
//! Rule system framework for hadolint-rs.
//!
//! Provides the infrastructure for defining and running Dockerfile linting rules.
//! The design matches hadolint's fold-based architecture:
//!
//! - `simple_rule` - Stateless rules that check each instruction independently
//! - `custom_rule` - Stateful rules that accumulate state across instructions
//! - `very_custom_rule` - Rules with custom finalization logic
//! - `onbuild` - Wrapper to also check ONBUILD-wrapped instructions

use crate::analyzer::hadolint::parser::instruction::Instruction;
use crate::analyzer::hadolint::shell::ParsedShell;
use crate::analyzer::hadolint::types::{CheckFailure, RuleCode, Severity};

pub mod dl1001;
pub mod dl3000;
pub mod dl3001;
pub mod dl3002;
pub mod dl3003;
pub mod dl3004;
pub mod dl3005;
pub mod dl3006;
pub mod dl3007;
pub mod dl3008;
pub mod dl3009;
pub mod dl3010;
pub mod dl3011;
pub mod dl3012;
pub mod dl3013;
pub mod dl3014;
pub mod dl3015;
pub mod dl3016;
pub mod dl3017;
pub mod dl3018;
pub mod dl3019;
pub mod dl3020;
pub mod dl3021;
pub mod dl3022;
pub mod dl3023;
pub mod dl3024;
pub mod dl3025;
pub mod dl3026;
pub mod dl3027;
pub mod dl3028;
pub mod dl3029;
pub mod dl3030;
pub mod dl3031;
pub mod dl3032;
pub mod dl3033;
pub mod dl3034;
pub mod dl3035;
pub mod dl3036;
pub mod dl3037;
pub mod dl3038;
pub mod dl3039;
pub mod dl3040;
pub mod dl3041;
pub mod dl3042;
pub mod dl3043;
pub mod dl3044;
pub mod dl3045;
pub mod dl3046;
pub mod dl3047;
pub mod dl3048;
pub mod dl3049;
pub mod dl3050;
pub mod dl3051;
pub mod dl3052;
pub mod dl3053;
pub mod dl3054;
pub mod dl3055;
pub mod dl3056;
pub mod dl3057;
pub mod dl3058;
pub mod dl3059;
pub mod dl3060;
pub mod dl3061;
pub mod dl3062;
pub mod dl4000;
pub mod dl4001;
pub mod dl4003;
pub mod dl4004;
pub mod dl4005;
pub mod dl4006;

/// A rule that can check Dockerfile instructions.
pub trait Rule: Send + Sync {
    /// Check an instruction and potentially add failures to the state.
    fn check(
        &self,
        state: &mut RuleState,
        line: u32,
        instruction: &Instruction,
        shell: Option<&ParsedShell>,
    );

    /// Finalize the rule and return any additional failures.
    /// Called after all instructions have been processed.
    fn finalize(&self, state: RuleState) -> Vec<CheckFailure> {
        state.failures
    }

    /// Get the rule code.
    fn code(&self) -> &RuleCode;

    /// Get the default severity.
    fn severity(&self) -> Severity;

    /// Get the rule message.
    fn message(&self) -> &str;
}

/// State for rule execution.
#[derive(Debug, Clone, Default)]
pub struct RuleState {
    /// Accumulated failures.
    pub failures: Vec<CheckFailure>,
    /// Custom state data (serialized).
    pub data: RuleData,
}

impl RuleState {
    /// Create a new empty state.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a failure.
    pub fn add_failure(
        &mut self,
        code: impl Into<RuleCode>,
        severity: Severity,
        message: impl Into<String>,
        line: u32,
    ) {
        self.failures
            .push(CheckFailure::new(code, severity, message, line));
    }
}

/// Custom data storage for stateful rules.
#[derive(Debug, Clone, Default)]
pub struct RuleData {
    /// Integer values.
    pub ints: std::collections::HashMap<&'static str, i64>,
    /// Boolean values.
    pub bools: std::collections::HashMap<&'static str, bool>,
    /// String values.
    pub strings: std::collections::HashMap<&'static str, String>,
    /// String set values.
    pub string_sets: std::collections::HashMap<&'static str, std::collections::HashSet<String>>,
}

impl RuleData {
    pub fn get_int(&self, key: &'static str) -> i64 {
        self.ints.get(key).copied().unwrap_or(0)
    }

    pub fn set_int(&mut self, key: &'static str, value: i64) {
        self.ints.insert(key, value);
    }

    pub fn get_bool(&self, key: &'static str) -> bool {
        self.bools.get(key).copied().unwrap_or(false)
    }

    pub fn set_bool(&mut self, key: &'static str, value: bool) {
        self.bools.insert(key, value);
    }

    pub fn get_string(&self, key: &'static str) -> Option<&str> {
        self.strings.get(key).map(|s| s.as_str())
    }

    pub fn set_string(&mut self, key: &'static str, value: impl Into<String>) {
        self.strings.insert(key, value.into());
    }

    pub fn get_string_set(&self, key: &'static str) -> Option<&std::collections::HashSet<String>> {
        self.string_sets.get(key)
    }

    pub fn insert_to_set(&mut self, key: &'static str, value: impl Into<String>) {
        self.string_sets
            .entry(key)
            .or_default()
            .insert(value.into());
    }

    pub fn set_contains(&self, key: &'static str, value: &str) -> bool {
        self.string_sets
            .get(key)
            .map(|s| s.contains(value))
            .unwrap_or(false)
    }
}

/// A simple stateless rule.
pub struct SimpleRule<F>
where
    F: Fn(&Instruction, Option<&ParsedShell>) -> bool + Send + Sync,
{
    code: RuleCode,
    severity: Severity,
    message: String,
    check_fn: F,
}

impl<F> SimpleRule<F>
where
    F: Fn(&Instruction, Option<&ParsedShell>) -> bool + Send + Sync,
{
    /// Create a new simple rule.
    pub fn new(
        code: impl Into<RuleCode>,
        severity: Severity,
        message: impl Into<String>,
        check_fn: F,
    ) -> Self {
        Self {
            code: code.into(),
            severity,
            message: message.into(),
            check_fn,
        }
    }
}

impl<F> Rule for SimpleRule<F>
where
    F: Fn(&Instruction, Option<&ParsedShell>) -> bool + Send + Sync,
{
    fn check(
        &self,
        state: &mut RuleState,
        line: u32,
        instruction: &Instruction,
        shell: Option<&ParsedShell>,
    ) {
        if !(self.check_fn)(instruction, shell) {
            state.add_failure(self.code.clone(), self.severity, self.message.clone(), line);
        }
    }

    fn code(&self) -> &RuleCode {
        &self.code
    }

    fn severity(&self) -> Severity {
        self.severity
    }

    fn message(&self) -> &str {
        &self.message
    }
}

/// Create a simple stateless rule.
pub fn simple_rule<F>(
    code: impl Into<RuleCode>,
    severity: Severity,
    message: impl Into<String>,
    check_fn: F,
) -> SimpleRule<F>
where
    F: Fn(&Instruction, Option<&ParsedShell>) -> bool + Send + Sync,
{
    SimpleRule::new(code, severity, message, check_fn)
}

/// A stateful rule with custom step function.
pub struct CustomRule<F>
where
    F: Fn(&mut RuleState, u32, &Instruction, Option<&ParsedShell>) + Send + Sync,
{
    code: RuleCode,
    severity: Severity,
    message: String,
    step_fn: F,
}

impl<F> CustomRule<F>
where
    F: Fn(&mut RuleState, u32, &Instruction, Option<&ParsedShell>) + Send + Sync,
{
    /// Create a new custom rule.
    pub fn new(
        code: impl Into<RuleCode>,
        severity: Severity,
        message: impl Into<String>,
        step_fn: F,
    ) -> Self {
        Self {
            code: code.into(),
            severity,
            message: message.into(),
            step_fn,
        }
    }
}

impl<F> Rule for CustomRule<F>
where
    F: Fn(&mut RuleState, u32, &Instruction, Option<&ParsedShell>) + Send + Sync,
{
    fn check(
        &self,
        state: &mut RuleState,
        line: u32,
        instruction: &Instruction,
        shell: Option<&ParsedShell>,
    ) {
        (self.step_fn)(state, line, instruction, shell);
    }

    fn code(&self) -> &RuleCode {
        &self.code
    }

    fn severity(&self) -> Severity {
        self.severity
    }

    fn message(&self) -> &str {
        &self.message
    }
}

/// Create a custom stateful rule.
pub fn custom_rule<F>(
    code: impl Into<RuleCode>,
    severity: Severity,
    message: impl Into<String>,
    step_fn: F,
) -> CustomRule<F>
where
    F: Fn(&mut RuleState, u32, &Instruction, Option<&ParsedShell>) + Send + Sync,
{
    CustomRule::new(code, severity, message, step_fn)
}

/// A rule with custom finalization.
pub struct VeryCustomRule<F, D>
where
    F: Fn(&mut RuleState, u32, &Instruction, Option<&ParsedShell>) + Send + Sync,
    D: Fn(RuleState) -> Vec<CheckFailure> + Send + Sync,
{
    code: RuleCode,
    severity: Severity,
    message: String,
    step_fn: F,
    done_fn: D,
}

impl<F, D> VeryCustomRule<F, D>
where
    F: Fn(&mut RuleState, u32, &Instruction, Option<&ParsedShell>) + Send + Sync,
    D: Fn(RuleState) -> Vec<CheckFailure> + Send + Sync,
{
    /// Create a new very custom rule.
    pub fn new(
        code: impl Into<RuleCode>,
        severity: Severity,
        message: impl Into<String>,
        step_fn: F,
        done_fn: D,
    ) -> Self {
        Self {
            code: code.into(),
            severity,
            message: message.into(),
            step_fn,
            done_fn,
        }
    }
}

impl<F, D> Rule for VeryCustomRule<F, D>
where
    F: Fn(&mut RuleState, u32, &Instruction, Option<&ParsedShell>) + Send + Sync,
    D: Fn(RuleState) -> Vec<CheckFailure> + Send + Sync,
{
    fn check(
        &self,
        state: &mut RuleState,
        line: u32,
        instruction: &Instruction,
        shell: Option<&ParsedShell>,
    ) {
        (self.step_fn)(state, line, instruction, shell);
    }

    fn finalize(&self, state: RuleState) -> Vec<CheckFailure> {
        (self.done_fn)(state)
    }

    fn code(&self) -> &RuleCode {
        &self.code
    }

    fn severity(&self) -> Severity {
        self.severity
    }

    fn message(&self) -> &str {
        &self.message
    }
}

/// Create a rule with custom finalization.
pub fn very_custom_rule<F, D>(
    code: impl Into<RuleCode>,
    severity: Severity,
    message: impl Into<String>,
    step_fn: F,
    done_fn: D,
) -> VeryCustomRule<F, D>
where
    F: Fn(&mut RuleState, u32, &Instruction, Option<&ParsedShell>) + Send + Sync,
    D: Fn(RuleState) -> Vec<CheckFailure> + Send + Sync,
{
    VeryCustomRule::new(code, severity, message, step_fn, done_fn)
}

/// Get all enabled rules.
pub fn all_rules() -> Vec<Box<dyn Rule>> {
    vec![
        // DL1xxx rules (deprecation warnings)
        Box::new(dl1001::rule()),
        // Simple DL3xxx rules
        Box::new(dl3000::rule()),
        Box::new(dl3001::rule()),
        Box::new(dl3003::rule()),
        Box::new(dl3004::rule()),
        Box::new(dl3005::rule()),
        Box::new(dl3007::rule()),
        Box::new(dl3010::rule()),
        Box::new(dl3011::rule()),
        Box::new(dl3017::rule()),
        Box::new(dl3020::rule()),
        Box::new(dl3021::rule()),
        Box::new(dl3025::rule()),
        Box::new(dl3026::rule()),
        Box::new(dl3027::rule()),
        Box::new(dl3029::rule()),
        Box::new(dl3031::rule()),
        Box::new(dl3035::rule()),
        Box::new(dl3039::rule()),
        Box::new(dl3043::rule()),
        Box::new(dl3044::rule()),
        Box::new(dl3046::rule()),
        Box::new(dl3048::rule()),
        Box::new(dl3049::rule()),
        Box::new(dl3050::rule()),
        Box::new(dl3051::rule()),
        Box::new(dl3052::rule()),
        Box::new(dl3053::rule()),
        Box::new(dl3054::rule()),
        Box::new(dl3055::rule()),
        Box::new(dl3056::rule()),
        Box::new(dl3058::rule()),
        Box::new(dl3061::rule()),
        // DL4xxx simple rules
        Box::new(dl4000::rule()),
        Box::new(dl4005::rule()),
        Box::new(dl4006::rule()),
        // Stateful rules
        Box::new(dl3002::rule()),
        Box::new(dl3006::rule()),
        Box::new(dl3012::rule()),
        Box::new(dl3022::rule()),
        Box::new(dl3023::rule()),
        Box::new(dl3024::rule()),
        Box::new(dl3045::rule()),
        Box::new(dl3047::rule()),
        Box::new(dl3057::rule()),
        Box::new(dl3059::rule()),
        Box::new(dl3062::rule()),
        Box::new(dl4001::rule()),
        Box::new(dl4003::rule()),
        Box::new(dl4004::rule()),
        // Shell-dependent rules
        Box::new(dl3008::rule()),
        Box::new(dl3009::rule()),
        Box::new(dl3013::rule()),
        Box::new(dl3014::rule()),
        Box::new(dl3015::rule()),
        Box::new(dl3016::rule()),
        Box::new(dl3018::rule()),
        Box::new(dl3019::rule()),
        Box::new(dl3028::rule()),
        Box::new(dl3030::rule()),
        Box::new(dl3032::rule()),
        Box::new(dl3033::rule()),
        Box::new(dl3034::rule()),
        Box::new(dl3036::rule()),
        Box::new(dl3037::rule()),
        Box::new(dl3038::rule()),
        Box::new(dl3040::rule()),
        Box::new(dl3041::rule()),
        Box::new(dl3042::rule()),
        Box::new(dl3060::rule()),
    ]
}

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

    #[test]
    fn test_simple_rule() {
        let rule = simple_rule("TEST001", Severity::Warning, "Test message", |instr, _| {
            !matches!(instr, Instruction::Maintainer(_))
        });

        let mut state = RuleState::new();
        let instr = Instruction::Maintainer("test".to_string());
        rule.check(&mut state, 1, &instr, None);

        assert_eq!(state.failures.len(), 1);
        assert_eq!(state.failures[0].code.as_str(), "TEST001");
    }

    #[test]
    fn test_rule_data() {
        let mut data = RuleData::default();

        data.set_int("count", 5);
        assert_eq!(data.get_int("count"), 5);

        data.set_bool("seen", true);
        assert!(data.get_bool("seen"));

        data.set_string("name", "test");
        assert_eq!(data.get_string("name"), Some("test"));

        data.insert_to_set("aliases", "builder");
        assert!(data.set_contains("aliases", "builder"));
        assert!(!data.set_contains("aliases", "runner"));
    }
}