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
//! HL3xxx - Template Syntax Rules
//!
//! Rules for validating Go template syntax in Helm templates.

use crate::analyzer::helmlint::rules::{LintContext, Rule};
use crate::analyzer::helmlint::types::{CheckFailure, RuleCategory, Severity};

/// Get all HL3xxx rules.
pub fn rules() -> Vec<Box<dyn Rule>> {
    vec![
        Box::new(HL3001),
        Box::new(HL3002),
        Box::new(HL3004),
        Box::new(HL3005),
        Box::new(HL3006),
        Box::new(HL3007),
        Box::new(HL3008),
        Box::new(HL3009),
        Box::new(HL3010),
        Box::new(HL3011),
    ]
}

/// HL3001: Unclosed template action
pub struct HL3001;

impl Rule for HL3001 {
    fn code(&self) -> &'static str {
        "HL3001"
    }

    fn severity(&self) -> Severity {
        Severity::Error
    }

    fn name(&self) -> &'static str {
        "unclosed-action"
    }

    fn description(&self) -> &'static str {
        "Template has unclosed action (missing }})"
    }

    fn check(&self, ctx: &LintContext) -> Vec<CheckFailure> {
        let mut failures = Vec::new();

        for template in ctx.templates {
            for error in &template.errors {
                if error.message.contains("Unclosed template action") {
                    failures.push(CheckFailure::new(
                        "HL3001",
                        Severity::Error,
                        "Unclosed template action (missing }})".to_string(),
                        &template.path,
                        error.line,
                        RuleCategory::Template,
                    ));
                }
            }
        }

        failures
    }
}

/// HL3002: Unclosed range/if block
pub struct HL3002;

impl Rule for HL3002 {
    fn code(&self) -> &'static str {
        "HL3002"
    }

    fn severity(&self) -> Severity {
        Severity::Error
    }

    fn name(&self) -> &'static str {
        "unclosed-block"
    }

    fn description(&self) -> &'static str {
        "Template has unclosed control block (if/range/with)"
    }

    fn check(&self, ctx: &LintContext) -> Vec<CheckFailure> {
        let mut failures = Vec::new();

        for template in ctx.templates {
            for (structure, line) in &template.unclosed_blocks {
                failures.push(CheckFailure::new(
                    "HL3002",
                    Severity::Error,
                    format!("Unclosed {:?} block (missing {{{{- end }}}}))", structure),
                    &template.path,
                    *line,
                    RuleCategory::Template,
                ));
            }
        }

        failures
    }
}

/// HL3004: Missing 'end' for control structure
pub struct HL3004;

impl Rule for HL3004 {
    fn code(&self) -> &'static str {
        "HL3004"
    }

    fn severity(&self) -> Severity {
        Severity::Error
    }

    fn name(&self) -> &'static str {
        "missing-end"
    }

    fn description(&self) -> &'static str {
        "Control structure is missing closing 'end'"
    }

    fn check(&self, ctx: &LintContext) -> Vec<CheckFailure> {
        // This is covered by HL3002, but we check for specific error messages
        let mut failures = Vec::new();

        for template in ctx.templates {
            for error in &template.errors {
                if error.message.contains("Unclosed") && error.message.contains("block") {
                    failures.push(CheckFailure::new(
                        "HL3004",
                        Severity::Error,
                        error.message.clone(),
                        &template.path,
                        error.line,
                        RuleCategory::Template,
                    ));
                }
            }
        }

        failures
    }
}

/// HL3005: Using deprecated function
pub struct HL3005;

impl Rule for HL3005 {
    fn code(&self) -> &'static str {
        "HL3005"
    }

    fn severity(&self) -> Severity {
        Severity::Warning
    }

    fn name(&self) -> &'static str {
        "deprecated-function"
    }

    fn description(&self) -> &'static str {
        "Template uses deprecated function"
    }

    fn check(&self, ctx: &LintContext) -> Vec<CheckFailure> {
        let deprecated_functions = [
            ("dateInZone", "Use 'mustDateModify' instead"),
            ("genCA", "Use 'genSelfSignedCert' for better control"),
        ];

        let mut failures = Vec::new();

        for template in ctx.templates {
            for (func, suggestion) in &deprecated_functions {
                if template.calls_function(func) {
                    failures.push(CheckFailure::new(
                        "HL3005",
                        Severity::Warning,
                        format!("Function '{}' is deprecated. {}", func, suggestion),
                        &template.path,
                        1, // Can't determine exact line without deeper analysis
                        RuleCategory::Template,
                    ));
                }
            }
        }

        failures
    }
}

/// HL3006: Potential nil pointer (missing 'default')
pub struct HL3006;

impl Rule for HL3006 {
    fn code(&self) -> &'static str {
        "HL3006"
    }

    fn severity(&self) -> Severity {
        Severity::Warning
    }

    fn name(&self) -> &'static str {
        "potential-nil"
    }

    fn description(&self) -> &'static str {
        "Value access may fail if value is nil. Consider using 'default'"
    }

    fn check(&self, ctx: &LintContext) -> Vec<CheckFailure> {
        // This is a heuristic check - look for deep value access without default
        let failures = Vec::new();

        for template in ctx.templates {
            // Look for deep nested access patterns that might fail
            for var in &template.variables_used {
                if var.starts_with(".Values.") {
                    let parts: Vec<&str> = var.split('.').collect();
                    // Deep nesting (more than 3 levels) without apparent default is risky
                    if parts.len() > 4 && !template.calls_function("default") {
                        // This is a very rough heuristic
                        // A more sophisticated check would track usage context
                    }
                }
            }
        }

        failures
    }
}

/// HL3007: Template file has invalid extension
pub struct HL3007;

impl Rule for HL3007 {
    fn code(&self) -> &'static str {
        "HL3007"
    }

    fn severity(&self) -> Severity {
        Severity::Warning
    }

    fn name(&self) -> &'static str {
        "invalid-template-extension"
    }

    fn description(&self) -> &'static str {
        "Template file should have .yaml, .yml, or .tpl extension"
    }

    fn check(&self, ctx: &LintContext) -> Vec<CheckFailure> {
        let valid_extensions = [".yaml", ".yml", ".tpl", ".txt"];
        let mut failures = Vec::new();

        for file in ctx.files {
            if file.contains("templates/") && !file.contains("templates/tests/") {
                let has_valid_ext = valid_extensions.iter().any(|ext| file.ends_with(ext));
                let is_helper = file.contains("_helpers");
                let is_notes = file.contains("NOTES.txt");

                if !has_valid_ext && !is_helper && !is_notes && !file.ends_with('/') {
                    failures.push(CheckFailure::new(
                        "HL3007",
                        Severity::Warning,
                        format!("Template file '{}' has unexpected extension", file),
                        file,
                        1,
                        RuleCategory::Template,
                    ));
                }
            }
        }

        failures
    }
}

/// HL3008: NOTES.txt missing
pub struct HL3008;

impl Rule for HL3008 {
    fn code(&self) -> &'static str {
        "HL3008"
    }

    fn severity(&self) -> Severity {
        Severity::Info
    }

    fn name(&self) -> &'static str {
        "missing-notes"
    }

    fn description(&self) -> &'static str {
        "Chart should have a NOTES.txt for post-install instructions"
    }

    fn check(&self, ctx: &LintContext) -> Vec<CheckFailure> {
        // Skip for library charts
        if let Some(chart) = ctx.chart_metadata
            && chart.is_library()
        {
            return vec![];
        }

        let has_notes = ctx.files.iter().any(|f| f.ends_with("NOTES.txt"));
        if !has_notes {
            return vec![CheckFailure::new(
                "HL3008",
                Severity::Info,
                "Chart is missing templates/NOTES.txt for post-install instructions",
                "templates/NOTES.txt",
                1,
                RuleCategory::Template,
            )];
        }

        vec![]
    }
}

/// HL3009: Helper without description comment
pub struct HL3009;

impl Rule for HL3009 {
    fn code(&self) -> &'static str {
        "HL3009"
    }

    fn severity(&self) -> Severity {
        Severity::Info
    }

    fn name(&self) -> &'static str {
        "helper-missing-comment"
    }

    fn description(&self) -> &'static str {
        "Helper template should have a description comment"
    }

    fn check(&self, ctx: &LintContext) -> Vec<CheckFailure> {
        let mut failures = Vec::new();

        if let Some(helpers) = ctx.helpers {
            for helper in &helpers.helpers {
                if helper.doc_comment.is_none() {
                    failures.push(CheckFailure::new(
                        "HL3009",
                        Severity::Info,
                        format!("Helper '{}' is missing a description comment", helper.name),
                        &helpers.path,
                        helper.line,
                        RuleCategory::Template,
                    ));
                }
            }
        }

        failures
    }
}

/// HL3010: Unused helper defined
pub struct HL3010;

impl Rule for HL3010 {
    fn code(&self) -> &'static str {
        "HL3010"
    }

    fn severity(&self) -> Severity {
        Severity::Info
    }

    fn name(&self) -> &'static str {
        "unused-helper"
    }

    fn description(&self) -> &'static str {
        "Helper template is defined but never used"
    }

    fn check(&self, ctx: &LintContext) -> Vec<CheckFailure> {
        let mut failures = Vec::new();

        let helpers = match ctx.helpers {
            Some(h) => h,
            None => return failures,
        };

        let referenced = ctx.template_references();

        for helper in &helpers.helpers {
            if !referenced.contains(helper.name.as_str()) {
                // Check if it's used via include in other helpers
                let used_in_helpers = helpers
                    .helpers
                    .iter()
                    .any(|h| h.name != helper.name && h.content.contains(&helper.name));

                if !used_in_helpers {
                    failures.push(CheckFailure::new(
                        "HL3010",
                        Severity::Info,
                        format!("Helper '{}' is defined but never used", helper.name),
                        &helpers.path,
                        helper.line,
                        RuleCategory::Template,
                    ));
                }
            }
        }

        failures
    }
}

/// HL3011: Include of non-existent template
pub struct HL3011;

impl Rule for HL3011 {
    fn code(&self) -> &'static str {
        "HL3011"
    }

    fn severity(&self) -> Severity {
        Severity::Error
    }

    fn name(&self) -> &'static str {
        "include-not-found"
    }

    fn description(&self) -> &'static str {
        "Template includes a helper that is not defined"
    }

    fn check(&self, ctx: &LintContext) -> Vec<CheckFailure> {
        let mut failures = Vec::new();

        let defined_helpers: std::collections::HashSet<&str> =
            ctx.helper_names().into_iter().collect();
        let referenced = ctx.template_references();

        for ref_name in referenced {
            if !defined_helpers.contains(ref_name) {
                // Find which template references this
                for template in ctx.templates {
                    if template.referenced_templates.contains(ref_name) {
                        failures.push(CheckFailure::new(
                            "HL3011",
                            Severity::Error,
                            format!("Template includes '{}' which is not defined", ref_name),
                            &template.path,
                            1,
                            RuleCategory::Template,
                        ));
                        break;
                    }
                }
            }
        }

        failures
    }
}

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

    // Tests would require setting up LintContext which needs parsed templates
    // For now, we just verify the rules compile and have correct metadata

    #[test]
    fn test_rules_exist() {
        let all_rules = rules();
        assert!(!all_rules.is_empty());
    }
}