bashrs 6.66.0

Rust-to-Shell transpiler for deterministic bootstrap scripts
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
use crate::cli::args::{LintProfileArg, ReportFormat};
use crate::models::{Error, Result};
use std::fs;
use std::path::Path;
use tracing::info;

// ---------------------------------------------------------------------------
// dockerfile_profile_command
// ---------------------------------------------------------------------------

#[allow(clippy::too_many_arguments)]
pub(crate) fn dockerfile_profile_command(
    input: &Path,
    build: bool,
    layers: bool,
    startup: bool,
    memory: bool,
    cpu: bool,
    _workload: Option<&Path>,
    _duration: &str,
    profile: Option<LintProfileArg>,
    simulate_limits: bool,
    full: bool,
    format: ReportFormat,
) -> Result<()> {
    use crate::linter::docker_profiler::{estimate_size, is_docker_available, PlatformProfile};

    info!("Profiling {} for runtime performance", input.display());

    if !is_docker_available() {
        println!("\u{26a0}\u{fe0f}  Docker daemon not available");
        println!("Runtime profiling requires Docker. Falling back to static analysis.\n");
    }

    let source = fs::read_to_string(input).map_err(Error::Io)?;

    let platform = match profile {
        Some(LintProfileArg::Coursera) => PlatformProfile::Coursera,
        _ => PlatformProfile::Standard,
    };

    let estimate = estimate_size(&source);

    match format {
        ReportFormat::Human => {
            docker_profile_human(
                input,
                &estimate,
                platform,
                ProfileSections {
                    build,
                    layers,
                    startup,
                    memory,
                    cpu,
                    simulate_limits,
                    full,
                },
            );
        }
        ReportFormat::Json => docker_profile_json(input, &estimate, platform),
        ReportFormat::Markdown => docker_profile_markdown(input, &estimate),
    }

    Ok(())
}

struct ProfileSections {
    build: bool,
    layers: bool,
    startup: bool,
    memory: bool,
    cpu: bool,
    simulate_limits: bool,
    full: bool,
}

fn docker_profile_human(
    _input: &Path,
    estimate: &crate::linter::docker_profiler::SizeEstimate,
    platform: crate::linter::docker_profiler::PlatformProfile,
    sections: ProfileSections,
) {
    let ProfileSections {
        build,
        layers,
        startup,
        memory,
        cpu,
        simulate_limits,
        full,
    } = sections;
    use crate::linter::docker_profiler::{format_size_estimate, PlatformProfile};

    println!("Docker Image Profile");
    println!("====================\n");

    if build || full {
        docker_profile_build_section(estimate, layers);
    }

    println!("{}", format_size_estimate(estimate, layers));

    if startup || full {
        println!("Startup Analysis:");
        println!("  Requires Docker daemon for actual measurement");
        if platform == PlatformProfile::Coursera {
            println!("  Coursera limit: 60 seconds");
            println!("  Recommendation: Target <30s startup time");
        }
        println!();
    }

    if memory || full {
        println!("Memory Analysis:");
        println!("  Requires Docker daemon for actual measurement");
        if platform == PlatformProfile::Coursera {
            println!("  Coursera limit: 4GB");
        }
        println!();
    }

    if cpu || full {
        println!("CPU Analysis:");
        println!("  Requires Docker daemon for actual measurement");
        if platform == PlatformProfile::Coursera {
            println!("  Coursera limit: 2 CPUs");
        }
        println!();
    }

    if platform == PlatformProfile::Coursera {
        docker_profile_coursera_validation(estimate, &platform, simulate_limits);
    }
}

fn docker_profile_build_section(
    estimate: &crate::linter::docker_profiler::SizeEstimate,
    layers: bool,
) {
    println!("Build Analysis:");
    println!("  Layers: {}", estimate.layer_estimates.len());
    println!(
        "  Estimated build time: {} (based on layer complexity)",
        estimate_build_time(estimate)
    );

    if layers {
        println!("\n  Layer Details:");
        for layer in &estimate.layer_estimates {
            let cached = if layer.cached { " (cached)" } else { "" };
            println!(
                "    [{}] {}{} - line {}",
                layer.layer_num, layer.instruction, cached, layer.line
            );
            if let Some(ref notes) = layer.notes {
                println!("        {}", notes);
            }
        }
    }
    println!();
}

fn docker_profile_coursera_validation(
    estimate: &crate::linter::docker_profiler::SizeEstimate,
    platform: &crate::linter::docker_profiler::PlatformProfile,
    simulate_limits: bool,
) {
    println!("Coursera Platform Validation:");
    let max_size_gb = platform.max_size_bytes() as f64 / 1_000_000_000.0;
    let estimated_gb = estimate.total_estimated as f64 / 1_000_000_000.0;
    let size_ok = estimate.total_estimated < platform.max_size_bytes();
    let size_icon = if size_ok { "\u{2713}" } else { "\u{2717}" };

    println!(
        "  {} Image size: {:.2}GB (limit: {:.0}GB)",
        size_icon, estimated_gb, max_size_gb
    );

    if simulate_limits {
        println!("\n  Simulation flags for docker run:");
        println!("    --memory=4g --cpus=2");
    }
    println!();
}

fn docker_profile_json(
    input: &Path,
    estimate: &crate::linter::docker_profiler::SizeEstimate,
    platform: crate::linter::docker_profiler::PlatformProfile,
) {
    use crate::linter::docker_profiler::is_docker_available;

    let json = serde_json::json!({
        "file": input.display().to_string(),
        "profile": format!("{:?}", platform),
        "build": {
            "layers": estimate.layer_estimates.len(),
            "estimated_build_time": estimate_build_time(estimate),
        },
        "size": {
            "base_image": estimate.base_image,
            "base_image_bytes": estimate.base_image_size,
            "total_estimated_bytes": estimate.total_estimated,
            "bloat_patterns": estimate.bloat_patterns.len(),
        },
        "docker_available": is_docker_available(),
        "platform_limits": {
            "max_size_bytes": platform.max_size_bytes(),
            "max_memory_bytes": platform.max_memory_bytes(),
            "max_startup_ms": platform.max_startup_ms(),
        }
    });
    println!(
        "{}",
        serde_json::to_string_pretty(&json).unwrap_or_default()
    );
}

fn docker_profile_markdown(input: &Path, estimate: &crate::linter::docker_profiler::SizeEstimate) {
    println!("# Docker Image Profile\n");
    println!("**File**: {}\n", input.display());
    println!("## Build Analysis\n");
    println!("- **Layers**: {}", estimate.layer_estimates.len());
    println!(
        "- **Estimated build time**: {}\n",
        estimate_build_time(estimate)
    );
    println!("## Size Analysis\n");
    println!("- **Base image**: {}", estimate.base_image);
    println!(
        "- **Estimated total**: {:.2}GB\n",
        estimate.total_estimated as f64 / 1_000_000_000.0
    );
}

// ---------------------------------------------------------------------------
// estimate_build_time
// ---------------------------------------------------------------------------

/// Estimate build time based on layer complexity
pub(crate) fn estimate_build_time(
    estimate: &crate::linter::docker_profiler::SizeEstimate,
) -> String {
    // Rough heuristic: 1 second per 100MB + base times
    let mut total_seconds = 0u64;

    for layer in &estimate.layer_estimates {
        // Base time for each layer
        total_seconds += 1;

        // Add time based on size
        total_seconds += layer.estimated_size / 100_000_000;

        // Add extra time for known slow operations
        let content_lower = layer.content.to_lowercase();
        if content_lower.contains("apt-get install") {
            total_seconds += 10;
        }
        if content_lower.contains("pip install") {
            total_seconds += 5;
        }
        if content_lower.contains("npm install") {
            total_seconds += 5;
        }
    }

    if total_seconds < 60 {
        format!("~{}s", total_seconds)
    } else {
        format!("~{}m {}s", total_seconds / 60, total_seconds % 60)
    }
}

// ---------------------------------------------------------------------------
// dockerfile_size_check_command
// ---------------------------------------------------------------------------

#[allow(clippy::too_many_arguments)]
pub(crate) fn dockerfile_size_check_command(
    input: &Path,
    verbose: bool,
    layers: bool,
    detect_bloat: bool,
    verify: bool,
    docker_verify: bool,
    profile: Option<LintProfileArg>,
    strict: bool,
    max_size: Option<&str>,
    compression_analysis: bool,
    format: ReportFormat,
) -> Result<()> {
    use crate::linter::docker_profiler::{
        estimate_size, format_size_estimate_json, PlatformProfile,
    };

    info!("Checking size of {}", input.display());

    let source = fs::read_to_string(input).map_err(Error::Io)?;
    let estimate = estimate_size(&source);

    let platform = match profile {
        Some(LintProfileArg::Coursera) => PlatformProfile::Coursera,
        _ => PlatformProfile::Standard,
    };

    let custom_limit = parse_size_limit(max_size);

    match format {
        ReportFormat::Human => size_check_human_output(
            &estimate,
            &platform,
            SizeCheckOptions {
                custom_limit,
                verbose,
                layers,
                detect_bloat,
                verify,
                docker_verify,
                compression_analysis,
                strict,
            },
        ),
        ReportFormat::Json => {
            println!("{}", format_size_estimate_json(&estimate));
            Ok(())
        }
        ReportFormat::Markdown => {
            size_check_markdown_output(input, &estimate);
            Ok(())
        }
    }
}

fn parse_size_limit(max_size: Option<&str>) -> Option<u64> {
    max_size.and_then(|s| {
        let s = s.to_uppercase();
        if s.ends_with("GB") {
            s[..s.len() - 2]
                .trim()
                .parse::<f64>()
                .ok()
                .map(|n| (n * 1_000_000_000.0) as u64)
        } else if s.ends_with("MB") {
            s[..s.len() - 2]
                .trim()
                .parse::<f64>()
                .ok()
                .map(|n| (n * 1_000_000.0) as u64)
        } else {
            None
        }
    })
}

struct SizeCheckOptions {
    custom_limit: Option<u64>,
    verbose: bool,
    layers: bool,
    detect_bloat: bool,
    verify: bool,
    docker_verify: bool,
    compression_analysis: bool,
    strict: bool,
}

fn size_check_human_output(
    estimate: &crate::linter::docker_profiler::SizeEstimate,
    platform: &crate::linter::docker_profiler::PlatformProfile,
    opts: SizeCheckOptions,
) -> Result<()> {
    let SizeCheckOptions {
        custom_limit,
        verbose,
        layers,
        detect_bloat,
        verify,
        docker_verify,
        compression_analysis,
        strict,
    } = opts;
    use crate::linter::docker_profiler::{format_size_estimate, is_docker_available};

    println!("{}", format_size_estimate(estimate, verbose || layers));

    if detect_bloat && !estimate.bloat_patterns.is_empty() {
        println!("Bloat Detection Results:");
        for pattern in &estimate.bloat_patterns {
            println!(
                "  {} [line {}]: {}",
                pattern.code, pattern.line, pattern.description
            );
            println!("    Wasted: ~{}MB", pattern.wasted_bytes / 1_000_000);
            println!("    Fix: {}", pattern.remediation);
            println!();
        }
    }

    if (verify || docker_verify) && is_docker_available() {
        println!("Docker Verification:");
        println!("  Requires docker build to verify actual size\n");
    }

    if compression_analysis {
        println!("Compression Opportunities:");
        println!("  - Use multi-stage builds to reduce final image size");
        println!("  - Compress large data files with gzip (~70% reduction)");
        println!("  - Use .dockerignore to exclude unnecessary files\n");
    }

    size_check_limit_check(estimate, platform, custom_limit, strict)
}

fn size_check_limit_check(
    estimate: &crate::linter::docker_profiler::SizeEstimate,
    platform: &crate::linter::docker_profiler::PlatformProfile,
    custom_limit: Option<u64>,
    strict: bool,
) -> Result<()> {
    let effective_limit = custom_limit.unwrap_or(platform.max_size_bytes());
    if effective_limit == u64::MAX {
        return Ok(());
    }

    let limit_gb = effective_limit as f64 / 1_000_000_000.0;
    let estimated_gb = estimate.total_estimated as f64 / 1_000_000_000.0;

    println!("Size Limit Check:");
    if estimate.total_estimated > effective_limit {
        println!(
            "  \u{2717} EXCEEDS LIMIT: {:.2}GB > {:.0}GB",
            estimated_gb, limit_gb
        );
        if strict {
            return Err(Error::Validation(format!(
                "Image size ({:.2}GB) exceeds limit ({:.0}GB)",
                estimated_gb, limit_gb
            )));
        }
    } else {
        let percentage = (estimate.total_estimated as f64 / effective_limit as f64) * 100.0;
        println!(
            "  \u{2713} Within limit: {:.2}GB / {:.0}GB ({:.0}%)",
            estimated_gb, limit_gb, percentage
        );
    }
    println!();
    Ok(())
}

fn size_check_markdown_output(
    input: &Path,
    estimate: &crate::linter::docker_profiler::SizeEstimate,
) {
    println!("# Image Size Analysis\n");
    println!("**File**: {}\n", input.display());
    println!("## Summary\n");
    println!("- **Base image**: {}", estimate.base_image);
    println!(
        "- **Estimated total**: {:.2}GB\n",
        estimate.total_estimated as f64 / 1_000_000_000.0
    );

    if !estimate.bloat_patterns.is_empty() {
        println!("## Bloat Patterns\n");
        for pattern in &estimate.bloat_patterns {
            println!(
                "- **{}** (line {}): {}",
                pattern.code, pattern.line, pattern.description
            );
        }
        println!();
    }
}