Skip to main content

dotenv_space/formats/
shell.rs

1// ============================================================================
2// formats/shell.rs
3// ============================================================================
4
5use crate::core::converter::{ConvertOptions, Converter};
6use anyhow::Result;
7use std::collections::HashMap;
8
9pub struct ShellExportConverter;
10
11impl Converter for ShellExportConverter {
12    fn convert(&self, vars: &HashMap<String, String>, options: &ConvertOptions) -> Result<String> {
13        let filtered = options.filter_vars(vars);
14
15        let mut output = String::new();
16        output.push_str("#!/bin/bash\n");
17        output.push_str("# Generated by evnx\n\n");
18
19        for (k, v) in filtered.iter() {
20            let key = options.transform_key(k);
21            let value = options.transform_value(v);
22            // Escape quotes in values
23            let escaped_value = value.replace('"', "\\\"");
24            output.push_str(&format!("export {}=\"{}\"\n", key, escaped_value));
25        }
26
27        Ok(output)
28    }
29
30    fn name(&self) -> &str {
31        "shell"
32    }
33
34    fn description(&self) -> &str {
35        "Shell export script"
36    }
37}