rash_core 2.17.8

Declarative shell scripting using Rust native bindings
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
/// ANCHOR: module
/// # setup
///
/// Load variables from .env, YAML, and JSON files.
///
/// Environment variables from .env files are loaded into the `env` namespace, while
/// YAML and JSON variables are loaded as top-level context variables.
///
/// ## Attributes
///
/// ```yaml
/// check_mode:
///   support: always
/// ```
/// ANCHOR_END: module
/// ANCHOR: examples
/// ## Examples
///
/// ```yaml
/// - name: Load configuration from multiple sources
///   setup:
///     from:
///       - .env
///       - config.yaml
///       - settings.json
///
/// - name: Use loaded variables
///   debug:
///     msg: "Database URL: {{ env.DATABASE_URL }}"
///
/// - name: Load from single file
///   setup:
///     from: vars/production.yml
/// ```
/// ANCHOR_END: examples
use crate::context::GlobalParams;
use crate::error::{Error, ErrorKind, Result};
use crate::modules::{Module, ModuleResult, parse_params};

#[cfg(feature = "docs")]
use rash_derive::DocJsonSchema;

use std::collections::HashMap;
use std::fs::read_to_string;
use std::path::Path;

use minijinja::Value;
#[cfg(feature = "docs")]
use schemars::{JsonSchema, Schema};
use serde::Deserialize;
use serde_norway::Value as YamlValue;

#[derive(Debug, PartialEq, Deserialize, Clone)]
#[cfg_attr(feature = "docs", derive(JsonSchema, DocJsonSchema))]
#[serde(deny_unknown_fields)]
pub struct Params {
    /// List of file paths to load variables from.
    /// Supports .env, .yaml/.yml, and .json files. `.env` files are loaded into the `env`
    /// namespace, while YAML and JSON files are loaded as top-level context variables.
    /// If a file has no extension, its format is auto-detected based on its content.
    #[serde(default)]
    from: Vec<String>,
}

fn load_file_vars_with_type(file_path: &str) -> Result<(serde_json::Value, bool)> {
    let path = Path::new(file_path);

    let content = read_to_string(path).map_err(|e| {
        Error::new(
            ErrorKind::InvalidData,
            format!("Failed to read file '{file_path}': {e}"),
        )
    })?;

    detect_and_load_file_format(&content, path)
}

fn detect_and_load_file_format(content: &str, path: &Path) -> Result<(serde_json::Value, bool)> {
    match path.extension().and_then(|s| s.to_str()) {
        Some("env") => {
            let vars = load_env_vars(content)?;
            Ok((vars, true))
        }
        Some("yaml") | Some("yml") => {
            let vars = load_yaml_vars(content)?;
            Ok((vars, false))
        }
        Some("json") => {
            let vars = load_json_vars(content)?;
            Ok((vars, false))
        }
        _ => {
            // Auto-detect format by content or filename
            if path.file_name().and_then(|s| s.to_str()) == Some(".env") {
                let vars = load_env_vars(content)?;
                Ok((vars, true))
            } else if content.trim_start().starts_with('{') {
                let vars = load_json_vars(content)?;
                Ok((vars, false))
            } else if content.contains('=') && !content.trim_start().starts_with('-') {
                let vars = load_env_vars(content)?;
                Ok((vars, true))
            } else {
                let vars = load_yaml_vars(content)?;
                Ok((vars, false))
            }
        }
    }
}

fn load_env_vars(content: &str) -> Result<serde_json::Value> {
    let mut vars = HashMap::new();

    for (line_num, line) in content.lines().enumerate() {
        let line = line.trim();
        if line.is_empty() || line.starts_with('#') {
            continue;
        }

        if let Some(pos) = line.find('=') {
            let key = line[..pos].trim();
            let value = line[pos + 1..].trim();

            // Validate environment variable name (POSIX: [a-zA-Z_][a-zA-Z0-9_]*)
            let mut chars = key.chars();
            let valid = match chars.next() {
                Some(c) if c.is_ascii_alphabetic() || c == '_' => {
                    chars.all(|c| c.is_ascii_alphanumeric() || c == '_')
                }
                _ => false,
            };
            if !valid {
                return Err(Error::new(
                    ErrorKind::InvalidData,
                    format!(
                        "Invalid environment variable name '{}' at line {}",
                        key,
                        line_num + 1
                    ),
                ));
            }

            // Remove quotes if present
            let cleaned_value = if (value.starts_with('"') && value.ends_with('"'))
                || (value.starts_with('\'') && value.ends_with('\''))
            {
                &value[1..value.len() - 1]
            } else {
                value
            };

            vars.insert(key.to_string(), cleaned_value.to_string());
        } else {
            return Err(Error::new(
                ErrorKind::InvalidData,
                format!("Invalid .env format at line {}: missing '='", line_num + 1),
            ));
        }
    }

    serde_json::to_value(vars)
        .map_err(|e| Error::new(ErrorKind::InvalidData, format!("serde_json error: {e}")))
}

fn load_yaml_vars(content: &str) -> Result<serde_json::Value> {
    let yaml_value: YamlValue = serde_norway::from_str(content)
        .map_err(|e| Error::new(ErrorKind::InvalidData, format!("Invalid YAML: {e}")))?;

    serde_json::to_value(yaml_value).map_err(|e| {
        Error::new(
            ErrorKind::InvalidData,
            format!("YAML conversion error: {e}"),
        )
    })
}

fn load_json_vars(content: &str) -> Result<serde_json::Value> {
    serde_json::from_str(content)
        .map_err(|e| Error::new(ErrorKind::InvalidData, format!("Invalid JSON: {e}")))
}

fn merge_context_with_env_vars(
    context_json: &mut serde_json::Map<String, serde_json::Value>,
    env_vars: serde_json::Value,
) {
    if let serde_json::Value::Object(env_map) = env_vars {
        // Get or create env object
        let env_obj = context_json
            .entry("env".to_string())
            .or_insert_with(|| serde_json::Value::Object(serde_json::Map::new()));

        if let serde_json::Value::Object(env_existing) = env_obj {
            // Merge .env variables into env object
            for (k, v) in env_map {
                env_existing.insert(k, v);
            }
        }
    }
}

fn merge_context_with_regular_vars(
    context_json: &mut serde_json::Map<String, serde_json::Value>,
    file_vars: serde_json::Value,
) {
    if let serde_json::Value::Object(new_map) = file_vars {
        // Merge all regular variables as top-level keys
        for (k, v) in new_map {
            context_json.insert(k, v);
        }
    }
}

fn load_and_merge_files(file_paths: &[String]) -> Result<(Vec<String>, Value)> {
    let mut loaded_files = Vec::with_capacity(file_paths.len());

    // Convert context to JSON for easier manipulation
    let mut context_json: serde_json::Map<String, serde_json::Value> = serde_json::Map::new();
    for file_path in file_paths {
        match load_file_vars_with_type(file_path) {
            Ok((file_vars, is_env_file)) => {
                if is_env_file {
                    merge_context_with_env_vars(&mut context_json, file_vars);
                } else {
                    merge_context_with_regular_vars(&mut context_json, file_vars);
                }

                loaded_files.push(file_path.clone());
            }
            Err(e) => {
                return Err(Error::new(
                    ErrorKind::InvalidData,
                    format!("Failed to load '{file_path}': {e}"),
                ));
            }
        }
    }

    let final_context = Value::from_serialize(context_json);
    Ok((loaded_files, final_context))
}

fn setup_context(params: Params) -> Result<(ModuleResult, Option<Value>)> {
    if params.from.is_empty() {
        return Ok((
            ModuleResult::new(false, None, Some("No files specified to load".to_string())),
            None,
        ));
    }

    let (loaded_files, new_vars) = load_and_merge_files(&params.from)?;

    Ok((
        ModuleResult::new(
            !loaded_files.is_empty(),
            None,
            Some(format!(
                "Loaded variables from: {}",
                loaded_files.join(", ")
            )),
        ),
        Some(new_vars),
    ))
}

#[derive(Debug)]
pub struct Setup;

impl Module for Setup {
    fn get_name(&self) -> &str {
        "setup"
    }

    fn exec(
        &self,
        _: &GlobalParams,
        optional_params: YamlValue,
        _vars: &Value,
        _check_mode: bool,
    ) -> Result<(ModuleResult, Option<Value>)> {
        setup_context(parse_params(optional_params)?)
    }

    #[cfg(feature = "docs")]
    fn get_json_schema(&self) -> Option<Schema> {
        Some(Params::get_json_schema())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::NamedTempFile;

    #[test]
    fn test_parse_params() {
        let yaml: YamlValue = serde_norway::from_str(
            r#"
            from:
              - .env
              - config.yaml
            "#,
        )
        .unwrap();
        let params: Params = parse_params(yaml).unwrap();
        assert_eq!(
            params,
            Params {
                from: vec![".env".to_owned(), "config.yaml".to_owned()],
            }
        );
    }

    #[test]
    fn test_parse_params_single_file() {
        let yaml: YamlValue = serde_norway::from_str(
            r#"
            from:
              - config.json
            "#,
        )
        .unwrap();
        let params: Params = parse_params(yaml).unwrap();
        assert_eq!(
            params,
            Params {
                from: vec!["config.json".to_owned()],
            }
        );
    }

    #[test]
    fn test_parse_params_empty() {
        let yaml: YamlValue = serde_norway::from_str("{}").unwrap();
        let params: Params = parse_params(yaml).unwrap();
        assert_eq!(params, Params { from: vec![] });
    }

    #[test]
    fn test_load_env_vars() {
        let content = r#"
# This is a comment
DATABASE_URL=postgres://localhost/mydb
API_KEY="secret-key"
DEBUG=true
PORT=3000
EMPTY_VAR=
        "#;

        let result = load_env_vars(content).unwrap();
        let expected = serde_json::json!({
            "DATABASE_URL": "postgres://localhost/mydb",
            "API_KEY": "secret-key",
            "DEBUG": "true",
            "PORT": "3000",
            "EMPTY_VAR": ""
        });

        assert_eq!(result, expected);
    }

    #[test]
    fn test_load_yaml_vars() {
        let content = r#"
database:
  host: localhost
  port: 5432
  name: mydb
api:
  key: secret
  timeout: 30
        "#;

        let result = load_yaml_vars(content).unwrap();
        let expected = serde_json::json!({
            "database": {
                "host": "localhost",
                "port": 5432,
                "name": "mydb"
            },
            "api": {
                "key": "secret",
                "timeout": 30
            }
        });

        assert_eq!(result, expected);
    }

    #[test]
    fn test_load_json_vars() {
        let content = r#"
{
    "app": {
        "name": "myapp",
        "version": "1.0.0"
    },
    "features": ["auth", "api"]
}
        "#;

        let result = load_json_vars(content).unwrap();
        let expected = serde_json::json!({
            "app": {
                "name": "myapp",
                "version": "1.0.0"
            },
            "features": ["auth", "api"]
        });

        assert_eq!(result, expected);
    }

    #[test]
    fn test_setup_context_no_files() {
        let params = Params { from: vec![] };

        let (result, new_vars) = setup_context(params).unwrap();

        assert!(!result.get_changed());
        assert!(result.get_output().unwrap().contains("No files specified"));
        assert_eq!(new_vars, None);
    }

    #[test]
    fn test_setup_context_with_files() {
        // Create temporary files
        let mut env_file = NamedTempFile::new().unwrap();
        writeln!(env_file, "TEST_VAR=hello").unwrap();
        writeln!(env_file, "PORT=8080").unwrap();
        env_file.flush().unwrap(); // Ensure data is written to disk

        let mut yaml_file = NamedTempFile::new().unwrap();
        writeln!(yaml_file, "config:").unwrap();
        writeln!(yaml_file, "  debug: true").unwrap();
        yaml_file.flush().unwrap(); // Ensure data is written to disk

        let params = Params {
            from: vec![
                env_file.path().to_str().unwrap().to_string(),
                yaml_file.path().to_str().unwrap().to_string(),
            ],
        };

        let (result, optional_new_vars) = setup_context(params).unwrap();
        let new_vars = optional_new_vars.unwrap();

        assert!(result.get_changed());
        assert!(
            result
                .get_output()
                .unwrap()
                .contains("Loaded variables from")
        );

        assert_eq!(
            new_vars
                .get_attr("env")
                .unwrap()
                .get_attr("TEST_VAR")
                .unwrap()
                .to_string(),
            "hello"
        );
        assert_eq!(
            new_vars
                .get_attr("env")
                .unwrap()
                .get_attr("PORT")
                .unwrap()
                .to_string(),
            "8080"
        );
    }

    #[test]
    fn test_load_file_security_validation() {
        // Test non-existent file
        let result = load_file_vars_with_type("/non/existent/file.env");
        assert!(result.is_err());
    }

    #[test]
    fn test_load_env_vars_validation() {
        // Test invalid variable name
        let content = "123INVALID=value\nVALID_VAR=test";
        let result = load_env_vars(content);
        assert!(result.is_err());

        // Test missing equals sign
        let content = "INVALID_LINE_WITHOUT_EQUALS";
        let result = load_env_vars(content);
        assert!(result.is_err());

        // Test empty key
        let content = "=value";
        let result = load_env_vars(content);
        assert!(result.is_err());
    }
}