makiatto-cli 0.6.1

CLI tool for managing Makiatto CDN deployments
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
use std::{
    collections::{HashMap, HashSet},
    path::PathBuf,
    sync::Arc,
    time::SystemTime,
};

use argh::FromArgs;
use miette::{Result, miette};
use serde_json::json;
use uuid::Uuid;

use crate::{
    config::{Config, DnsRecord, Domain, Machine, Profile},
    machine::corrosion,
    machine::corrosion::Statement,
    ssh::SshSession,
    ui,
};

/// sync the project to the cdn
#[derive(FromArgs)]
#[argh(subcommand, name = "sync")]
pub struct SyncCommand {
    /// path to SSH private key (optional)
    #[argh(option, long = "ssh-priv-key")]
    pub key_path: Option<PathBuf>,
}

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct DnsRecordKey {
    pub name: String,
    pub record_type: String,
}

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct DnsRecordData {
    pub value: String,
    pub ttl: u32,
    pub priority: i32,
    pub geo_enabled: bool,
}

/// Sync project files and DNS configuration to the CDN
///
/// # Errors
/// Returns an error if sync operations fail
pub fn sync_project(command: &SyncCommand, profile: &Profile, config: &Config) -> Result<()> {
    if profile.machines.is_empty() {
        return Err(miette!(
            "No machines configured. Use `machine init` to add one first"
        ));
    }

    if config.domains.is_empty() {
        return Err(miette!("No domains configured in makiatto.toml"));
    }

    // validate all wasm file paths
    for domain in config.domains.iter() {
        for function in domain.functions.iter() {
            let path_str = function.path.display().to_string();
            if !std::path::Path::new(&path_str)
                .extension()
                .is_some_and(|ext| ext.eq_ignore_ascii_case("wasm"))
            {
                return Err(miette::miette!(
                    "Domain '{}': Function path '{}' must end with .wasm extension",
                    domain.name,
                    path_str
                ));
            }
        }

        for transform in domain.transforms.iter() {
            let path_str = transform.path.display().to_string();
            if !std::path::Path::new(&path_str)
                .extension()
                .is_some_and(|ext| ext.eq_ignore_ascii_case("wasm"))
            {
                return Err(miette::miette!(
                    "Domain '{}': Transform path '{}' must end with .wasm extension",
                    domain.name,
                    path_str
                ));
            }
        }
    }

    let sync_machine = profile
        .machines
        .iter()
        .find(|m| m.is_nameserver && m.sync_target)
        .unwrap_or(&profile.machines[0]);

    ui::header(&format!("Syncing to machine '{}'", sync_machine.name));
    ui::status(&format!("Connecting to {}", sync_machine.ssh_target));

    let ssh = SshSession::new(
        &sync_machine.ssh_target,
        sync_machine.port,
        command.key_path.as_ref(),
    )?;

    check_remote_version(&ssh);

    for domain in config.domains.iter() {
        ui::header(&format!("Processing domain: {}", domain.name));
        sync_domain_files(&ssh, domain, command.key_path.as_ref(), sync_machine)?;
        sync_domain_records(&ssh, domain, &profile.machines)?;
        sync_domain_functions(&ssh, domain)?;
        sync_domain_transforms(&ssh, domain)?;
    }

    ui::status("Sync completed successfully");
    Ok(())
}

fn check_remote_version(ssh: &SshSession) {
    let cli_version = env!("CARGO_PKG_VERSION");

    match ssh.exec("makiatto --version 2>/dev/null") {
        Ok(output) => {
            let remote_version = output.trim();
            if !remote_version.is_empty() && remote_version != cli_version {
                ui::warn(&format!(
                    "maki is v{cli_version} but makiatto daemon is v{remote_version}. Consider running 'maki machine upgrade' to update the daemon binary",
                ));
            }
        }
        Err(_) => {
            ui::warn("Could not determine remote makiatto version");
        }
    }
}

fn sync_domain_files(
    ssh: &SshSession,
    domain: &Domain,
    key_path: Option<&PathBuf>,
    machine: &Machine,
) -> Result<()> {
    ui::status("Syncing files...");

    let target_dir = format!("/var/makiatto/sites/{}", domain.name);
    ssh.exec(&format!("sudo mkdir -p {target_dir}"))?;

    if let Err(e) = ssh.exec(&format!(
        "curl -s -X POST http://{}:8282/watcher/pause",
        machine.wg_address
    )) {
        return Err(miette!(format!("Failed to pause file watcher: {e}")));
    }

    // ensure the watcher is resumed no matter how we leave this function — an
    // early error during rsync/chown/scan must not leave it paused forever
    let _resume_guard = WatcherResumeGuard {
        ssh,
        wg_address: &machine.wg_address,
    };

    let spinner = ui::spinner("Running rsync...");

    let source = domain.path.to_string_lossy();
    let target = format!("{}@{}:{}/", ssh.user, ssh.host, target_dir);

    let ssh_args = if let Some(key_path) = key_path {
        format!(
            "ssh -i {} -p {} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null",
            key_path.display(),
            ssh.port
        )
    } else {
        format!(
            "ssh -p {} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null",
            ssh.port
        )
    };

    // single-quote-escape the password so a quote in it can't break out of the
    // remote `echo '...'`. The password is still handed to the remote sudo via
    // rsync's --rsync-path, which is inherent to running rsync under sudo.
    let rsync_path = if let Some(password) = &ssh.password {
        let escaped = password.replace('\'', r"'\''");
        format!("echo '{escaped}' | sudo -S rsync")
    } else {
        "sudo rsync".to_string()
    };

    let mut rsync_cmd = std::process::Command::new("rsync");
    rsync_cmd
        .arg("-avz")
        .arg("--delete-after")
        .arg("--progress")
        .arg("-e")
        .arg(&ssh_args)
        .arg("--rsync-path")
        .arg(&rsync_path)
        .arg(format!("{}/", source.trim_end_matches('/')))
        .arg(&target);

    let output = rsync_cmd
        .output()
        .map_err(|e| miette!("Failed to execute rsync: {e}"))?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        return Err(miette!("rsync failed: {stderr}"));
    }

    spinner.finish_with_message("✓ rsync completed");

    // fix ownership (`rsync --chown` not available in older rsync on macOS)
    ssh.exec(&format!("sudo chown -R makiatto:makiatto {target_dir}"))?;

    // trigger a manual directory scan to catch any files missed by the watcher
    ui::status("Scanning synced files...");
    ssh.exec(&format!(
        "curl -s -X POST http://{}:8282/scan/{}",
        machine.wg_address, domain.name
    ))?;

    // the watcher is resumed by `_resume_guard` when this function returns
    Ok(())
}

/// Resumes the remote file watcher on drop, so a paused watcher is never left
/// behind if a sync step fails partway through.
struct WatcherResumeGuard<'a> {
    ssh: &'a SshSession,
    wg_address: &'a str,
}

impl Drop for WatcherResumeGuard<'_> {
    fn drop(&mut self) {
        if let Err(e) = self.ssh.exec(&format!(
            "curl -s -X POST http://{}:8282/watcher/resume",
            self.wg_address
        )) {
            ui::warn(&format!("Failed to resume file watcher: {e}"));
        }
    }
}

fn sync_domain_records(ssh: &SshSession, domain: &Domain, machines: &[Machine]) -> Result<()> {
    ui::status("Updating DNS records...");

    let domain_sql = Statement::with_params(
        "INSERT OR IGNORE INTO domains (name) VALUES (?)",
        vec![json!(domain.name.as_ref())],
    );
    corrosion::execute_transactions(ssh, &[domain_sql])?;

    for alias in domain.aliases.iter() {
        let alias_sql = Statement::with_params(
            "INSERT OR REPLACE INTO domain_aliases (alias, target) VALUES (?, ?)",
            vec![json!(alias.as_ref()), json!(domain.name.as_ref())],
        );
        corrosion::execute_transactions(ssh, &[alias_sql])?;
    }

    let existing_records = get_existing_dns_records(ssh, &domain.name)?;

    let mut desired_records = Vec::new();
    generate_dns_records(
        &domain.name,
        machines,
        &domain.records,
        &mut desired_records,
    )?;

    apply_dns_diff(ssh, &domain.name, &existing_records, &desired_records)?;

    Ok(())
}

fn get_existing_dns_records(
    ssh: &SshSession,
    domain: &str,
) -> Result<Vec<(DnsRecordKey, DnsRecordData)>> {
    let sql = format!(
        "SELECT name, record_type, value, ttl, priority, geo_enabled FROM dns_records WHERE domain = '{domain}'"
    );
    let cmd = format!("sudo sqlite3 /var/makiatto/cluster.db -separator '|' \"{sql}\"");

    let output = ssh.exec(&cmd)?;
    let mut records = Vec::new();

    for line in output.lines() {
        if line.trim().is_empty() {
            continue;
        }

        let parts: Vec<&str> = line.split('|').collect();
        if parts.len() != 6 {
            continue;
        }

        let key = DnsRecordKey {
            name: parts[0].to_string(),
            record_type: parts[1].to_string(),
        };

        let data = DnsRecordData {
            value: parts[2].to_string(),
            ttl: parts[3].parse().unwrap_or(300),
            priority: parts[4].parse().unwrap_or(0),
            geo_enabled: parts[5] == "1",
        };

        records.push((key, data));
    }

    Ok(records)
}

/// Generate all DNS records for a domain including infrastructure and custom records
///
/// # Errors
/// Returns an error if no nameservers are configured
///
/// # Panics
/// May panic if a machine has `ipv6` set to `Some` but the value is accessed incorrectly
pub fn generate_dns_records(
    domain: &str,
    machines: &[Machine],
    custom_records: &[DnsRecord],
    records: &mut Vec<(DnsRecordKey, DnsRecordData)>,
) -> Result<()> {
    let mut nameservers: Vec<_> = machines.iter().filter(|m| m.is_nameserver).collect();
    nameservers.sort_by(|a, b| a.name.cmp(&b.name));

    if nameservers.is_empty() {
        return Err(miette!(
            "No nameservers configured. At least one machine must be a nameserver"
        ));
    }

    if let Some(machine) = machines.iter().find(|m| !m.ipv4.is_empty()) {
        records.push((
            DnsRecordKey {
                name: "@".to_string(),
                record_type: "A".to_string(),
            },
            DnsRecordData {
                value: machine.ipv4.to_string(),
                ttl: 300,
                priority: 0,
                geo_enabled: true,
            },
        ));
    }

    if let Some(machine) = machines
        .iter()
        .find(|m| m.ipv6.is_some() && !m.ipv6.as_ref().unwrap().is_empty())
    {
        records.push((
            DnsRecordKey {
                name: "@".to_string(),
                record_type: "AAAA".to_string(),
            },
            DnsRecordData {
                value: machine.ipv6.as_ref().unwrap().to_string(),
                ttl: 300,
                priority: 0,
                geo_enabled: true,
            },
        ));
    }

    // generate NS records for each nameserver
    for nameserver in &nameservers {
        let ns_name = format!("{}.ns", nameserver.name);

        records.push((
            DnsRecordKey {
                name: "@".to_string(),
                record_type: "NS".to_string(),
            },
            DnsRecordData {
                value: format!("{ns_name}.{domain}"),
                ttl: 300,
                priority: 0,
                geo_enabled: false,
            },
        ));

        records.push((
            DnsRecordKey {
                name: ns_name.clone(),
                record_type: "A".to_string(),
            },
            DnsRecordData {
                value: nameserver.ipv4.to_string(),
                ttl: 300,
                priority: 0,
                geo_enabled: false,
            },
        ));

        if let Some(ipv6) = &nameserver.ipv6
            && !ipv6.is_empty()
        {
            records.push((
                DnsRecordKey {
                    name: ns_name,
                    record_type: "AAAA".to_string(),
                },
                DnsRecordData {
                    value: ipv6.to_string(),
                    ttl: 300,
                    priority: 0,
                    geo_enabled: false,
                },
            ));
        }
    }

    if let Some(primary_ns) = nameservers.first() {
        let serial = {
            let now = SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_secs();
            let date = now / 86400; // Days since epoch
            format!("{date}00")
        };

        let soa_value = format!(
            "{}.ns.{} admin.{} {} 86400 10800 3600000 3600",
            primary_ns.name, domain, domain, serial
        );

        records.push((
            DnsRecordKey {
                name: "@".to_string(),
                record_type: "SOA".to_string(),
            },
            DnsRecordData {
                value: soa_value,
                ttl: 300,
                priority: 0,
                geo_enabled: false,
            },
        ));
    }

    records.push((
        DnsRecordKey {
            name: "@".to_string(),
            record_type: "CAA".to_string(),
        },
        DnsRecordData {
            value: "0 issue letsencrypt.org".to_string(),
            ttl: 300,
            priority: 0,
            geo_enabled: false,
        },
    ));

    // Add custom records from config
    for record in custom_records {
        records.push((
            DnsRecordKey {
                name: record.name.to_string(),
                record_type: record.record_type.to_string(),
            },
            DnsRecordData {
                value: record.value.to_string(),
                ttl: record.ttl,
                priority: record.priority.unwrap_or(0),
                geo_enabled: false,
            },
        ));
    }

    Ok(())
}

fn apply_dns_diff(
    ssh: &SshSession,
    domain: &str,
    existing: &[(DnsRecordKey, DnsRecordData)],
    desired: &[(DnsRecordKey, DnsRecordData)],
) -> Result<()> {
    let existing_set: HashSet<_> = existing.iter().collect();
    let desired_set: HashSet<_> = desired.iter().collect();

    let to_delete: Vec<_> = existing
        .iter()
        .filter(|r| !desired_set.contains(r))
        .collect();

    let to_add: Vec<_> = desired
        .iter()
        .filter(|r| !existing_set.contains(r))
        .collect();

    if to_delete.is_empty() && to_add.is_empty() {
        return Ok(());
    }

    let mut sqls = Vec::new();

    for (key, data) in &to_delete {
        ui::action(&format!(
            "Removing DNS record: {} {} -> {}",
            key.name, key.record_type, data.value
        ));
        let sql = Statement::with_params(
            "DELETE FROM dns_records WHERE domain = ? AND name = ? AND record_type = ?",
            vec![json!(domain), json!(key.name), json!(key.record_type)],
        );
        sqls.push(sql);
    }

    for (key, data) in &to_add {
        ui::action(&format!(
            "Adding DNS record: {} {} -> {}",
            key.name, key.record_type, data.value
        ));
        let id = Uuid::now_v7().to_string();
        let sql = Statement::with_params(
            "INSERT INTO dns_records (id, domain, name, record_type, value, ttl, priority, geo_enabled) \
             VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
            vec![
                json!(id),
                json!(domain),
                json!(key.name),
                json!(key.record_type),
                json!(data.value),
                json!(data.ttl),
                json!(data.priority),
                json!(i32::from(data.geo_enabled)),
            ],
        );
        sqls.push(sql);
    }

    corrosion::execute_transactions(ssh, &sqls)?;

    let added_ns_records: Vec<_> = to_add
        .iter()
        .filter(|(key, _)| key.record_type == "NS")
        .collect();

    if !added_ns_records.is_empty() {
        ui::warn("⚠️ New nameserver records detected");
        ui::info(
            "Consider running `maki dns nameserver-setup` to get the complete nameserver configuration guide.",
        );
    }

    Ok(())
}

/// Merge an optional `env_file` (KEY=VALUE lines) with the inline `env` map and
/// serialise the result to a JSON object string. Inline `env` entries take
/// precedence over file entries.
fn resolve_env(env_file: Option<&PathBuf>, env: &HashMap<Arc<str>, Arc<str>>) -> Result<String> {
    let mut merged: HashMap<String, String> = HashMap::new();

    if let Some(path) = env_file {
        let content = std::fs::read_to_string(path)
            .map_err(|e| miette!("Failed to read env_file '{}': {e}", path.display()))?;

        for line in content.lines() {
            let line = line.trim();
            if line.is_empty() || line.starts_with('#') {
                continue;
            }
            if let Some((key, value)) = line.split_once('=') {
                let value = value.trim().trim_matches(['"', '\'']);
                merged.insert(key.trim().to_string(), value.to_string());
            }
        }
    }

    for (key, value) in env {
        merged.insert(key.to_string(), value.to_string());
    }

    serde_json::to_string(&merged).map_err(|e| miette!("Failed to serialise env: {e}"))
}

fn sync_domain_functions(ssh: &SshSession, domain: &Domain) -> Result<()> {
    if domain.functions.is_empty() {
        return Ok(());
    }

    ui::status("Syncing WASM functions...");

    let delete_sql = Statement::with_params(
        "DELETE FROM domain_functions WHERE domain = ?",
        vec![json!(domain.name.as_ref())],
    );
    corrosion::execute_transactions(ssh, &[delete_sql])?;

    let mut sqls = Vec::new();
    for function in domain.functions.iter() {
        let path_str = function.path.display().to_string();

        let route = path_str.strip_suffix(".wasm").unwrap();
        let route = if route.starts_with('/') {
            route.to_string()
        } else {
            format!("/{route}")
        };

        let id = format!("{}:{}", domain.name, route);

        let methods_json = if let Some(ref methods) = function.methods {
            serde_json::to_string(methods).unwrap_or_else(|_| "null".to_string())
        } else {
            "null".to_string()
        };

        let env_json = resolve_env(function.env_file.as_ref(), &function.env)?;

        let updated_at = SystemTime::now()
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap()
            .as_secs();

        let sql = Statement::with_params(
            "INSERT INTO domain_functions (\
                id, domain, path, methods, env, \
                timeout_ms, max_memory_mb, updated_at\
            ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
            vec![
                json!(id),
                json!(domain.name.as_ref()),
                json!(path_str),
                json!(methods_json),
                json!(env_json),
                function.timeout_ms.map_or(json!(null), |t| json!(t)),
                function.max_memory_mb.map_or(json!(null), |m| json!(m)),
                json!(updated_at),
            ],
        );
        sqls.push(sql);

        ui::action(&format!("  Added function: {path_str}"));
    }

    corrosion::execute_transactions(ssh, &sqls)?;
    Ok(())
}

fn sync_domain_transforms(ssh: &SshSession, domain: &Domain) -> Result<()> {
    if domain.transforms.is_empty() {
        return Ok(());
    }

    ui::status("Syncing WASM transforms...");

    // Delete existing transforms for this domain
    let delete_sql = Statement::with_params(
        "DELETE FROM domain_transforms WHERE domain = ?",
        vec![json!(domain.name.as_ref())],
    );
    corrosion::execute_transactions(ssh, &[delete_sql])?;

    let mut sqls = Vec::new();
    for (idx, transform) in domain.transforms.iter().enumerate() {
        let path_str = transform.path.display().to_string();
        let id = format!("{}:{}:{}", domain.name, path_str, idx);

        let env_json = resolve_env(transform.env_file.as_ref(), &transform.env)?;

        let updated_at = SystemTime::now()
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap()
            .as_secs();

        let sql = Statement::with_params(
            "INSERT INTO domain_transforms (\
                id, domain, path, files_pattern, env, \
                timeout_ms, max_memory_mb, max_file_size_kb, \
                execution_order, updated_at\
            ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
            vec![
                json!(id),
                json!(domain.name.as_ref()),
                json!(path_str),
                json!(transform.files.as_ref()),
                json!(env_json),
                transform.timeout_ms.map_or(json!(null), |t| json!(t)),
                transform.max_memory_mb.map_or(json!(null), |m| json!(m)),
                transform.max_file_size_kb.map_or(json!(null), |s| json!(s)),
                json!(idx),
                json!(updated_at),
            ],
        );
        sqls.push(sql);

        ui::action(&format!(
            "  Added transform: {} ({})",
            path_str, transform.files
        ));
    }

    corrosion::execute_transactions(ssh, &sqls)?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;
    use std::sync::Arc;

    use super::resolve_env;

    #[test]
    fn resolve_env_merges_file_and_inline_with_inline_precedence() {
        let dir = std::env::temp_dir().join(format!("maki-env-{}", uuid::Uuid::now_v7()));
        std::fs::create_dir_all(&dir).unwrap();
        let env_file = dir.join(".env");
        std::fs::write(
            &env_file,
            "A=from_file\nB=from_file\n# a comment\n\nC=\"quoted\"\n",
        )
        .unwrap();

        let mut inline: HashMap<Arc<str>, Arc<str>> = HashMap::new();
        inline.insert(Arc::from("B"), Arc::from("from_inline")); // overrides file
        inline.insert(Arc::from("D"), Arc::from("inline_only"));

        let json = resolve_env(Some(&env_file), &inline).unwrap();
        let map: HashMap<String, String> = serde_json::from_str(&json).unwrap();

        assert_eq!(map.get("A").map(String::as_str), Some("from_file"));
        assert_eq!(map.get("B").map(String::as_str), Some("from_inline"));
        assert_eq!(map.get("C").map(String::as_str), Some("quoted")); // quotes stripped
        assert_eq!(map.get("D").map(String::as_str), Some("inline_only"));

        std::fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn resolve_env_without_file_uses_inline_only() {
        let mut inline: HashMap<Arc<str>, Arc<str>> = HashMap::new();
        inline.insert(Arc::from("X"), Arc::from("1"));
        let json = resolve_env(None, &inline).unwrap();
        let map: HashMap<String, String> = serde_json::from_str(&json).unwrap();
        assert_eq!(map.len(), 1);
        assert_eq!(map.get("X").map(String::as_str), Some("1"));
    }
}