Skip to main content

eol_deployer/
config.rs

1use crate::cli::CLI;
2use std::collections::HashMap;
3
4#[derive(Debug)]
5pub struct Config {
6    pub repo_url: String,
7    pub mount_path: String,
8}
9
10impl Config {
11    pub fn from_env_and_cli(cli: &CLI) -> Result<Self, Box<dyn std::error::Error>> {
12        // Parse .env file into a HashMap (don't crash if it doesn't exist)
13        let mut env_vars = HashMap::new();
14        if std::path::Path::new(".env").exists() {
15            if let Ok(env_content) = std::fs::read_to_string(".env") {
16                for line in env_content.lines() {
17                    let line = line.trim();
18                    // Skip empty lines and comments
19                    if line.is_empty() || line.starts_with('#') {
20                        continue;
21                    }
22                    if let Some((key, value)) = line.split_once('=') {
23                        env_vars.insert(key.trim().to_string(), value.trim().to_string());
24                    }
25                }
26            }
27        }
28
29        // Priority: CLI args > .env file > error
30        let repo_url = cli
31            .repo_url
32            .clone()
33            .or_else(|| env_vars.get("REPO_URL").cloned())
34            .ok_or("REPO_URL not provided. Use --repo-url flag or set REPO_URL in .env file")?;
35
36        let mount_path = cli
37            .mount_path
38            .clone()
39            .or_else(|| env_vars.get("MOUNT_PATH").cloned())
40            .ok_or(
41                "MOUNT_PATH not provided. Use --mount-path flag or set MOUNT_PATH in .env file",
42            )?;
43
44        Ok(Config {
45            repo_url,
46            mount_path,
47        })
48    }
49
50    pub fn show_configuration_help() {
51        println!("Configuration options:");
52        println!("  1. Command line flags:");
53        println!(
54            "     ./app v1.2.3 --name my-project --repo-url https://github.com/org/repo.git --mount-path /opt/configs"
55        );
56        println!();
57        println!("  2. Create a .env file:");
58        println!("     REPO_URL=https://github.com/your-org/traefik-config.git");
59        println!("     MOUNT_PATH=/opt/traefik-configs");
60        println!();
61        println!("Command line flags take precedence over .env file values.");
62    }
63}