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
// SPF Milter – milter for SPF verification
// Copyright © 2020–2022 David Bürgin <dbuergin@gluet.ch>
//
// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along with
// this program. If not, see <https://www.gnu.org/licenses/>.

use crate::config::model::{LogDestination, LogLevel, Socket, SyslogFacility};
use std::path::{Path, PathBuf};

// 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, 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<Socket>,
    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<&Socket> {
        self.socket.as_ref()
    }

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

impl Default for CliOptions {
    fn default() -> Self {
        Self::builder().build()
    }
}

/// 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<Socket>,
    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(mut self, value: Socket) -> Self {
        self.socket = Some(value);
        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(|| DEFAULT_CONFIG_FILE.into()),
            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:localhost:3000".parse().unwrap())
            .build();

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