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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
//! HL1xxx - Chart Structure Rules
//!
//! Rules for validating Helm chart structure, Chart.yaml, and file organization.

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

/// Get all HL1xxx rules.
pub fn rules() -> Vec<Box<dyn Rule>> {
    vec![
        Box::new(HL1001),
        Box::new(HL1002),
        Box::new(HL1003),
        Box::new(HL1004),
        Box::new(HL1005),
        Box::new(HL1006),
        Box::new(HL1007),
        Box::new(HL1008),
        Box::new(HL1009),
        Box::new(HL1010),
        Box::new(HL1011),
        Box::new(HL1012),
        Box::new(HL1013),
        Box::new(HL1014),
        Box::new(HL1015),
        Box::new(HL1016),
        Box::new(HL1017),
    ]
}

/// HL1001: Missing Chart.yaml
pub struct HL1001;

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

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

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

    fn description(&self) -> &'static str {
        "Chart.yaml is required for all Helm charts"
    }

    fn check(&self, ctx: &LintContext) -> Vec<CheckFailure> {
        if ctx.chart_metadata.is_none() && !ctx.has_file("Chart.yaml") {
            vec![CheckFailure::new(
                "HL1001",
                Severity::Error,
                "Missing Chart.yaml file",
                "Chart.yaml",
                1,
                RuleCategory::Structure,
            )]
        } else {
            vec![]
        }
    }
}

/// HL1002: Invalid apiVersion
pub struct HL1002;

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

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

    fn name(&self) -> &'static str {
        "invalid-api-version"
    }

    fn description(&self) -> &'static str {
        "Chart apiVersion must be v1 or v2"
    }

    fn check(&self, ctx: &LintContext) -> Vec<CheckFailure> {
        if let Some(chart) = ctx.chart_metadata
            && !chart.has_valid_api_version()
        {
            let version = match &chart.api_version {
                ApiVersion::Unknown(v) => v.clone(),
                _ => "unknown".to_string(),
            };
            return vec![CheckFailure::new(
                "HL1002",
                Severity::Error,
                format!("Invalid apiVersion '{}'. Must be v1 or v2", version),
                "Chart.yaml",
                1,
                RuleCategory::Structure,
            )];
        }
        vec![]
    }
}

/// HL1003: Missing required field 'name'
pub struct HL1003;

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

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

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

    fn description(&self) -> &'static str {
        "Chart.yaml must have a 'name' field"
    }

    fn check(&self, ctx: &LintContext) -> Vec<CheckFailure> {
        if let Some(chart) = ctx.chart_metadata
            && chart.name.is_empty()
        {
            return vec![CheckFailure::new(
                "HL1003",
                Severity::Error,
                "Missing required field 'name' in Chart.yaml",
                "Chart.yaml",
                1,
                RuleCategory::Structure,
            )];
        }
        vec![]
    }
}

/// HL1004: Missing required field 'version'
pub struct HL1004;

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

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

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

    fn description(&self) -> &'static str {
        "Chart.yaml must have a 'version' field"
    }

    fn check(&self, ctx: &LintContext) -> Vec<CheckFailure> {
        if let Some(chart) = ctx.chart_metadata
            && chart.version.is_empty()
        {
            return vec![CheckFailure::new(
                "HL1004",
                Severity::Error,
                "Missing required field 'version' in Chart.yaml",
                "Chart.yaml",
                1,
                RuleCategory::Structure,
            )];
        }
        vec![]
    }
}

/// HL1005: Version not valid SemVer
pub struct HL1005;

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

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

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

    fn description(&self) -> &'static str {
        "Chart version should be valid SemVer"
    }

    fn check(&self, ctx: &LintContext) -> Vec<CheckFailure> {
        if let Some(chart) = ctx.chart_metadata
            && !chart.version.is_empty()
            && !is_valid_semver(&chart.version)
        {
            return vec![CheckFailure::new(
                "HL1005",
                Severity::Warning,
                format!(
                    "Version '{}' is not valid SemVer (expected X.Y.Z format)",
                    chart.version
                ),
                "Chart.yaml",
                1,
                RuleCategory::Structure,
            )];
        }
        vec![]
    }
}

/// HL1006: Missing description
pub struct HL1006;

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

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

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

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

    fn check(&self, ctx: &LintContext) -> Vec<CheckFailure> {
        if let Some(chart) = ctx.chart_metadata
            && (chart.description.is_none()
                || chart
                    .description
                    .as_ref()
                    .map(|d| d.is_empty())
                    .unwrap_or(true))
        {
            return vec![CheckFailure::new(
                "HL1006",
                Severity::Info,
                "Chart.yaml is missing a description",
                "Chart.yaml",
                1,
                RuleCategory::Structure,
            )];
        }
        vec![]
    }
}

/// HL1007: Missing maintainers
pub struct HL1007;

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

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

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

    fn description(&self) -> &'static str {
        "Chart should have maintainers listed"
    }

    fn check(&self, ctx: &LintContext) -> Vec<CheckFailure> {
        if let Some(chart) = ctx.chart_metadata
            && chart.maintainers.is_empty()
        {
            return vec![CheckFailure::new(
                "HL1007",
                Severity::Info,
                "Chart.yaml has no maintainers listed",
                "Chart.yaml",
                1,
                RuleCategory::Structure,
            )];
        }
        vec![]
    }
}

/// HL1008: Chart is deprecated
pub struct HL1008;

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

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

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

    fn description(&self) -> &'static str {
        "Chart is marked as deprecated"
    }

    fn check(&self, ctx: &LintContext) -> Vec<CheckFailure> {
        if let Some(chart) = ctx.chart_metadata
            && chart.is_deprecated()
        {
            return vec![CheckFailure::new(
                "HL1008",
                Severity::Warning,
                "Chart is marked as deprecated",
                "Chart.yaml",
                1,
                RuleCategory::Structure,
            )];
        }
        vec![]
    }
}

/// HL1009: Missing templates directory
pub struct HL1009;

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

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

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

    fn description(&self) -> &'static str {
        "Chart should have a templates directory"
    }

    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_templates = ctx
            .files
            .iter()
            .any(|f| f.starts_with("templates/") || f.contains("/templates/"));
        if !has_templates && ctx.templates.is_empty() {
            return vec![CheckFailure::new(
                "HL1009",
                Severity::Warning,
                "Chart has no templates directory",
                ".",
                1,
                RuleCategory::Structure,
            )];
        }
        vec![]
    }
}

/// HL1010: Invalid chart type
pub struct HL1010;

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

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

    fn name(&self) -> &'static str {
        "invalid-chart-type"
    }

    fn description(&self) -> &'static str {
        "Chart type must be 'application' or 'library'"
    }

    fn check(&self, _ctx: &LintContext) -> Vec<CheckFailure> {
        // This is handled during parsing - if type is invalid, serde will fail
        // or produce Unknown variant which we handle elsewhere
        vec![]
    }
}

/// HL1011: Missing values.yaml
pub struct HL1011;

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

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

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

    fn description(&self) -> &'static str {
        "Chart should have a values.yaml file"
    }

    fn check(&self, ctx: &LintContext) -> Vec<CheckFailure> {
        if ctx.values.is_none() && !ctx.has_file("values.yaml") {
            return vec![CheckFailure::new(
                "HL1011",
                Severity::Warning,
                "Missing values.yaml file",
                "values.yaml",
                1,
                RuleCategory::Structure,
            )];
        }
        vec![]
    }
}

/// HL1012: Chart name contains invalid characters
pub struct HL1012;

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

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

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

    fn description(&self) -> &'static str {
        "Chart name must contain only lowercase alphanumeric characters and hyphens"
    }

    fn check(&self, ctx: &LintContext) -> Vec<CheckFailure> {
        if let Some(chart) = ctx.chart_metadata
            && !is_valid_chart_name(&chart.name)
        {
            return vec![CheckFailure::new(
                "HL1012",
                Severity::Error,
                format!(
                    "Chart name '{}' contains invalid characters. Use only lowercase letters, numbers, and hyphens",
                    chart.name
                ),
                "Chart.yaml",
                1,
                RuleCategory::Structure,
            )];
        }
        vec![]
    }
}

/// HL1013: Icon URL not HTTPS
pub struct HL1013;

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

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

    fn name(&self) -> &'static str {
        "icon-not-https"
    }

    fn description(&self) -> &'static str {
        "Icon URL should use HTTPS"
    }

    fn check(&self, ctx: &LintContext) -> Vec<CheckFailure> {
        if let Some(chart) = ctx.chart_metadata
            && let Some(icon) = &chart.icon
            && icon.starts_with("http://")
        {
            return vec![CheckFailure::new(
                "HL1013",
                Severity::Warning,
                "Icon URL should use HTTPS instead of HTTP",
                "Chart.yaml",
                1,
                RuleCategory::Structure,
            )];
        }
        vec![]
    }
}

/// HL1014: Home URL not HTTPS
pub struct HL1014;

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

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

    fn name(&self) -> &'static str {
        "home-not-https"
    }

    fn description(&self) -> &'static str {
        "Home URL should use HTTPS"
    }

    fn check(&self, ctx: &LintContext) -> Vec<CheckFailure> {
        if let Some(chart) = ctx.chart_metadata
            && let Some(home) = &chart.home
            && home.starts_with("http://")
        {
            return vec![CheckFailure::new(
                "HL1014",
                Severity::Warning,
                "Home URL should use HTTPS instead of HTTP",
                "Chart.yaml",
                1,
                RuleCategory::Structure,
            )];
        }
        vec![]
    }
}

/// HL1015: Duplicate dependency names
pub struct HL1015;

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

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

    fn name(&self) -> &'static str {
        "duplicate-dependencies"
    }

    fn description(&self) -> &'static str {
        "Chart has duplicate dependency names"
    }

    fn check(&self, ctx: &LintContext) -> Vec<CheckFailure> {
        if let Some(chart) = ctx.chart_metadata {
            let duplicates = chart.has_duplicate_dependencies();
            if !duplicates.is_empty() {
                return vec![CheckFailure::new(
                    "HL1015",
                    Severity::Error,
                    format!("Duplicate dependency names: {}", duplicates.join(", ")),
                    "Chart.yaml",
                    1,
                    RuleCategory::Structure,
                )];
            }
        }
        vec![]
    }
}

/// HL1016: Dependency missing version
pub struct HL1016;

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

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

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

    fn description(&self) -> &'static str {
        "Chart dependency is missing a version"
    }

    fn check(&self, ctx: &LintContext) -> Vec<CheckFailure> {
        let mut failures = Vec::new();
        if let Some(chart) = ctx.chart_metadata {
            for dep in &chart.dependencies {
                if dep.version.is_none()
                    || dep.version.as_ref().map(|v| v.is_empty()).unwrap_or(true)
                {
                    failures.push(CheckFailure::new(
                        "HL1016",
                        Severity::Warning,
                        format!("Dependency '{}' is missing a version", dep.name),
                        "Chart.yaml",
                        1,
                        RuleCategory::Structure,
                    ));
                }
            }
        }
        failures
    }
}

/// HL1017: Dependency missing repository
pub struct HL1017;

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

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

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

    fn description(&self) -> &'static str {
        "Chart dependency is missing a repository"
    }

    fn check(&self, ctx: &LintContext) -> Vec<CheckFailure> {
        let mut failures = Vec::new();
        if let Some(chart) = ctx.chart_metadata {
            for dep in &chart.dependencies {
                if dep.repository.is_none()
                    || dep
                        .repository
                        .as_ref()
                        .map(|r| r.is_empty())
                        .unwrap_or(true)
                {
                    // Skip if it's a file:// reference (local dependency)
                    failures.push(CheckFailure::new(
                        "HL1017",
                        Severity::Error,
                        format!("Dependency '{}' is missing a repository", dep.name),
                        "Chart.yaml",
                        1,
                        RuleCategory::Structure,
                    ));
                }
            }
        }
        failures
    }
}

/// Check if a version string is valid SemVer.
fn is_valid_semver(version: &str) -> bool {
    let parts: Vec<&str> = version.split('.').collect();
    if parts.len() < 2 || parts.len() > 3 {
        return false;
    }

    // Check major and minor are numeric
    for (i, part) in parts.iter().enumerate() {
        // Allow pre-release and build metadata on the last part
        let numeric_part = if i == parts.len() - 1 {
            part.split(['-', '+']).next().unwrap_or(part)
        } else {
            part
        };

        if numeric_part.parse::<u64>().is_err() {
            return false;
        }
    }

    true
}

/// Check if a chart name is valid.
fn is_valid_chart_name(name: &str) -> bool {
    if name.is_empty() {
        return false;
    }

    // Must start with a letter
    if !name
        .chars()
        .next()
        .map(|c| c.is_ascii_lowercase())
        .unwrap_or(false)
    {
        return false;
    }

    // Must contain only lowercase letters, numbers, and hyphens
    name.chars()
        .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-')
}

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

    #[test]
    fn test_valid_semver() {
        assert!(is_valid_semver("1.0.0"));
        assert!(is_valid_semver("0.1.0"));
        assert!(is_valid_semver("10.20.30"));
        assert!(is_valid_semver("1.0.0-alpha"));
        assert!(is_valid_semver("1.0.0+build"));
        assert!(is_valid_semver("1.0"));
        assert!(!is_valid_semver("1"));
        assert!(!is_valid_semver("v1.0.0"));
        assert!(!is_valid_semver("1.0.0.0"));
        assert!(!is_valid_semver(""));
    }

    #[test]
    fn test_valid_chart_name() {
        assert!(is_valid_chart_name("my-chart"));
        assert!(is_valid_chart_name("mychart"));
        assert!(is_valid_chart_name("my-chart-123"));
        assert!(!is_valid_chart_name("My-Chart"));
        assert!(!is_valid_chart_name("my_chart"));
        assert!(!is_valid_chart_name("123-chart"));
        assert!(!is_valid_chart_name(""));
    }
}