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
#[cfg(windows)]
use crate::trace;
use std::{
path::{Path, PathBuf},
sync::Arc,
};
#[cfg(not(windows))]
fn build_socket_path(root_path: &Path) -> PathBuf {
let mut socket_path = root_path.to_path_buf();
socket_path.push("socket");
socket_path
}
#[cfg(windows)]
fn build_socket_path(root_path: &Path) -> PathBuf {
trace!("Config.root_path = {:?}", root_path);
let mut socket_path = PathBuf::new();
socket_path.push(r"\\.\pipe");
for item in root_path.iter() {
let maybe_first_char = item.to_string_lossy().chars().next();
if let Some(first_char) = maybe_first_char {
if first_char != '\\' {
socket_path.push(item);
}
}
}
socket_path.push("socket");
trace!("Config.socket_path = {:?}", socket_path);
socket_path
}
pub struct Config {
root_path: PathBuf,
db_key_path: PathBuf,
store_path: PathBuf,
pid_path: PathBuf,
socket_path: PathBuf,
stdout_path: PathBuf,
stderr_path: PathBuf,
}
impl Config {
pub(crate) fn finalize(mut self) -> Arc<Config> {
std::fs::create_dir_all(self.root_path.as_path())
.expect("can cannonicalize root path");
self.root_path = self
.root_path
.canonicalize()
.expect("can cannonicalize root path");
self.db_key_path = self.root_path.clone();
self.db_key_path.push("db_key");
self.store_path = self.root_path.clone();
self.store_path.push("store.sqlite");
self.pid_path = self.root_path.clone();
self.pid_path.push("pid");
self.socket_path = build_socket_path(&self.root_path);
self.stdout_path = self.root_path.clone();
self.stdout_path.push("stdout");
self.stderr_path = self.root_path.clone();
self.stderr_path.push("stderr");
Arc::new(self)
}
pub fn builder() -> ConfigBuilder {
ConfigBuilder::default()
}
pub fn get_root_path(&self) -> &Path {
self.root_path.as_path()
}
pub fn get_db_key_path(&self) -> &Path {
self.db_key_path.as_path()
}
pub fn get_store_path(&self) -> &Path {
self.store_path.as_path()
}
pub fn get_pid_path(&self) -> &Path {
self.pid_path.as_path()
}
pub fn get_socket_path(&self) -> &Path {
self.socket_path.as_path()
}
pub fn get_stdout_path(&self) -> &Path {
self.stdout_path.as_path()
}
pub fn get_stderr_path(&self) -> &Path {
self.stderr_path.as_path()
}
}
pub struct ConfigBuilder(Config);
impl Default for ConfigBuilder {
fn default() -> Self {
let pdir = directories::ProjectDirs::from("host", "Holo", "Lair")
.expect("can determine project dir");
Self(Config {
root_path: pdir.data_local_dir().to_path_buf(),
db_key_path: PathBuf::new(),
store_path: PathBuf::new(),
pid_path: PathBuf::new(),
socket_path: PathBuf::new(),
stdout_path: PathBuf::new(),
stderr_path: PathBuf::new(),
})
}
}
impl ConfigBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn build(self) -> Arc<Config> {
self.0.finalize()
}
pub fn set_root_path<P>(mut self, p: P) -> Self
where
P: Into<PathBuf>,
{
self.0.root_path = p.into();
self
}
}