about summary refs log tree commit diff
path: root/gatherer/src/config.rs
blob: 509389f05db865415e90397eba1bff1c6b12dcb5 (plain)
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
use std::ops::Deref;

use camino::{Utf8Path, Utf8PathBuf};
use scurvy::{
	Argument, Scurvy, arg,
	formula::{BooleanFormula, U8Formula, Utf8PathFormula},
};

#[derive(Clone, Debug)]
pub struct Config {
	socket_path: Utf8PathBuf,
	database_path: Utf8PathBuf,
	network_interface: String,
	graph_path: Utf8PathBuf,
	graph_update: u8,
	graph_on_boot: bool,
}

impl Config {
	pub fn get() -> Self {
		let args = vec![
			arg!(["config", "c"] = "path"),
			arg!(["socket", "s"] = "path"),
			arg!(["database", "db"] = "path"),
			arg!("net" = "interface"),
			arg!("graph-directory" = "path"),
			arg!("graph-update" = "minutes"),
			arg!("graph-on-boot" = "bool"),
		];
		let scurvy = Scurvy::make(args).with_configuration_key("config", "/etc/gatherer.conf");

		let socket_path = scurvy.parse_req("socket", Utf8PathFormula);
		let database_path = scurvy.parse_req("database", Utf8PathFormula);
		let interface = scurvy.get_req(["net", "NetworkInterface"]).to_owned();
		let graph_path = scurvy.parse_req(["graph-directory", "GraphDirectory"], Utf8PathFormula);
		let graph_update = scurvy.parse_req(
			["graph-update", "GraphUpdate"],
			U8Formula::new().bounds(1..=60),
		);
		let graph_on_boot = scurvy
			.parse(["graph-on-boot", "GraphOnBoot"], BooleanFormula)
			.map(|b| *b.deref())
			.unwrap_or(false);

		Self {
			socket_path,
			database_path,
			network_interface: interface,
			graph_path,
			graph_update,
			graph_on_boot,
		}
	}

	pub fn socket_path(&self) -> &Utf8Path {
		&self.socket_path
	}

	pub fn database_path(&self) -> &Utf8Path {
		&self.database_path
	}

	pub fn network_interface(&self) -> &str {
		&self.network_interface
	}

	pub fn graph_path(&self) -> &Utf8Path {
		&self.graph_path
	}

	pub fn graph_update(&self) -> u8 {
		self.graph_update
	}

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