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
use crate::config::model::{LogDestination, LogLevel, SyslogFacility};
use once_cell::sync::OnceCell;
use std::path::{Path, PathBuf};

static CLI_OPTIONS: OnceCell<CliOptions> = OnceCell::new();

/// Initialises the static CLI options.
///
/// Must be called exactly once during startup.
pub fn init(opts: CliOptions) {
    CLI_OPTIONS.set(opts).expect("CLI options already initialized");
}

/// Returns a reference to the static CLI options.
pub fn get() -> &'static CliOptions {
    CLI_OPTIONS.get().expect("CLI options not initialized")
}

// Allow overriding the default configuration file path during the build.
const DEFAULT_CONFIG_FILE: &str = match option_env!("SPF_MILTER_CONFIG_FILE") {
    Some(s) => s,
    None => "/etc/spf-milter.conf",
};

/// A set of CLI options.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct CliOptions {
    // The configuration file path defaults to `/etc/spf-milter.conf` and is
    // always present, even if not set on the command-line.
    config_file: PathBuf,
    dry_run: bool,
    log_destination: Option<LogDestination>,
    log_level: Option<LogLevel>,
    socket: Option<String>,
    syslog_facility: Option<SyslogFacility>,
}

impl CliOptions {
    pub fn builder() -> CliOptionsBuilder {
        CliOptionsBuilder::new()
    }

    pub fn config_file(&self) -> &Path {
        &self.config_file
    }

    pub fn dry_run(&self) -> bool {
        self.dry_run
    }

    pub fn log_destination(&self) -> Option<LogDestination> {
        self.log_destination
    }

    pub fn log_level(&self) -> Option<LogLevel> {
        self.log_level
    }

    pub fn socket(&self) -> Option<&str> {
        self.socket.as_deref()
    }

    pub fn syslog_facility(&self) -> Option<SyslogFacility> {
        self.syslog_facility
    }
}

/// A builder for CLI options.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct CliOptionsBuilder {
    config_file: Option<PathBuf>,
    dry_run: bool,
    log_destination: Option<LogDestination>,
    log_level: Option<LogLevel>,
    socket: Option<String>,
    syslog_facility: Option<SyslogFacility>,
}

impl CliOptionsBuilder {
    pub fn new() -> Self {
        Default::default()
    }

    pub fn config_file<P: Into<PathBuf>>(mut self, value: P) -> Self {
        self.config_file = Some(value.into());
        self
    }

    pub fn dry_run(mut self, value: bool) -> Self {
        self.dry_run = value;
        self
    }

    pub fn log_destination(mut self, value: LogDestination) -> Self {
        self.log_destination = Some(value);
        self
    }

    pub fn log_level(mut self, value: LogLevel) -> Self {
        self.log_level = Some(value);
        self
    }

    pub fn socket<S: Into<String>>(mut self, value: S) -> Self {
        self.socket = Some(value.into());
        self
    }

    pub fn syslog_facility(mut self, value: SyslogFacility) -> Self {
        self.syslog_facility = Some(value);
        self
    }

    pub fn build(self) -> CliOptions {
        CliOptions {
            config_file: self
                .config_file
                .unwrap_or_else(|| PathBuf::from(DEFAULT_CONFIG_FILE)),
            dry_run: self.dry_run,
            log_destination: self.log_destination,
            log_level: self.log_level,
            socket: self.socket,
            syslog_facility: self.syslog_facility,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn cli_options_builder_ok() {
        let opts = CliOptions::builder().socket("inet:3000@localhost").build();

        assert_eq!(opts.socket(), Some("inet:3000@localhost"));
        assert_eq!(opts.config_file(), Path::new("/etc/spf-milter.conf"));
    }
}