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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
use std::collections::{BTreeMap, HashMap};
use std::fs::{create_dir_all, File};
use std::io::prelude::*;
use std::path::{Path, PathBuf};
use config::Config;
use log::info;
use serde_derive::{Deserialize, Serialize};
use shellexpand::tilde;
use crate::error::Error;
use crate::platform::directories::*;
#[derive(PartialEq, Clone, Debug, Deserialize, Serialize)]
pub struct Shared {
pub pueue_directory: PathBuf,
#[cfg(not(target_os = "windows"))]
pub use_unix_socket: bool,
#[cfg(not(target_os = "windows"))]
pub unix_socket_path: PathBuf,
pub host: String,
pub port: String,
pub daemon_cert: PathBuf,
pub daemon_key: PathBuf,
pub shared_secret_path: PathBuf,
}
#[derive(PartialEq, Clone, Debug, Deserialize, Serialize)]
pub struct Client {
pub read_local_logs: bool,
pub show_confirmation_questions: bool,
pub show_expanded_aliases: bool,
#[serde(default = "default_dark_mode")]
pub dark_mode: bool,
pub max_status_lines: Option<usize>,
}
#[derive(PartialEq, Clone, Debug, Deserialize, Serialize)]
pub struct Daemon {
pub default_parallel_tasks: usize,
pub pause_group_on_failure: bool,
pub pause_all_on_failure: bool,
pub callback: Option<String>,
#[serde(default = "default_callback_log_lines")]
pub callback_log_lines: usize,
pub groups: BTreeMap<String, usize>,
}
#[derive(PartialEq, Clone, Debug, Deserialize, Serialize)]
pub struct Settings {
pub client: Client,
pub daemon: Daemon,
pub shared: Shared,
}
impl Shared {
pub fn expand(old_path: &Path) -> PathBuf {
PathBuf::from(tilde(&old_path.to_string_lossy()).into_owned())
}
pub fn pueue_directory(&self) -> PathBuf {
Shared::expand(&self.pueue_directory)
}
#[cfg(not(target_os = "windows"))]
pub fn unix_socket_path(&self) -> PathBuf {
Shared::expand(&self.unix_socket_path)
}
pub fn daemon_cert(&self) -> PathBuf {
Shared::expand(&self.daemon_cert)
}
pub fn daemon_key(&self) -> PathBuf {
Shared::expand(&self.daemon_key)
}
pub fn shared_secret_path(&self) -> PathBuf {
Shared::expand(&self.shared_secret_path)
}
}
impl Settings {
pub fn read(from_file: &Option<PathBuf>) -> Result<Settings, Error> {
let config = Config::new();
parse_config(config, true, from_file)
}
pub fn read_with_defaults(
require_config: bool,
from_file: &Option<PathBuf>,
) -> Result<Settings, Error> {
let config = Settings::default_config()?;
parse_config(config, require_config, from_file)
}
pub fn default_config() -> Result<Config, Error> {
let mut config = Config::new();
let pueue_path = default_pueue_path()?;
config
.set_default("shared.pueue_directory", pueue_path.clone())
.unwrap();
#[cfg(not(target_os = "windows"))]
config.set_default("shared.use_unix_socket", true).unwrap();
#[cfg(not(target_os = "windows"))]
config
.set_default("shared.unix_socket_path", get_unix_socket_path()?)
.unwrap();
config.set_default("shared.host", "127.0.0.1").unwrap();
config.set_default("shared.port", "6924").unwrap();
config.set_default("shared.tls_enabled", true).unwrap();
config
.set_default(
"shared.daemon_key",
pueue_path.clone() + "/certs/daemon.key",
)
.unwrap();
config
.set_default(
"shared.daemon_cert",
pueue_path.clone() + "/certs/daemon.cert",
)
.unwrap();
config
.set_default("shared.shared_secret_path", pueue_path + "/shared_secret")
.unwrap();
config.set_default("client.read_local_logs", true).unwrap();
config
.set_default("client.show_expanded_aliases", false)
.unwrap();
config
.set_default("client.show_confirmation_questions", false)
.unwrap();
config.set_default("client.dark_mode", false).unwrap();
config
.set_default("client.max_status_lines", None::<i64>)
.unwrap();
config
.set_default("daemon.default_parallel_tasks", 1)
.unwrap();
config
.set_default("daemon.pause_group_on_failure", false)
.unwrap();
config
.set_default("daemon.pause_all_on_failure", false)
.unwrap();
config
.set_default("daemon.callback", None::<String>)
.unwrap();
config.set_default("daemon.callback_log_lines", 10).unwrap();
config
.set_default("daemon.groups", HashMap::<String, i64>::new())
.unwrap();
Ok(config)
}
pub fn save(&self, path: &Option<PathBuf>) -> Result<(), Error> {
let config_path = if let Some(path) = path {
path.clone()
} else {
default_config_directory()?.join("pueue.yml")
};
let config_dir = config_path
.parent()
.ok_or_else(|| Error::InvalidPath("Couldn't resolve config directory".into()))?;
if !config_dir.exists() {
create_dir_all(config_dir)?;
}
let content = match serde_yaml::to_string(self) {
Ok(content) => content,
Err(error) => {
return Err(Error::Generic(format!(
"Configuration file serialization failed:\n{}",
error
)))
}
};
let mut file = File::create(config_path)?;
file.write_all(content.as_bytes())?;
Ok(())
}
}
fn parse_config(
mut config: Config,
require_config: bool,
from_file: &Option<PathBuf>,
) -> Result<Settings, Error> {
if let Some(path) = from_file {
if !path.exists() || !path.is_file() {
return Err(Error::FileNotFound(format!(
"Couldn't find config at path {:?}",
path
)));
}
info!("Using config file at: {:?}", path);
let config_file = config::File::with_name(path.to_str().unwrap());
config.merge(config_file)?;
return Ok(config.try_into()?);
};
let mut config_found = false;
info!("Parsing config files");
for directory in get_config_directories()?.into_iter() {
let path = directory.join("pueue.yml");
info!("Checking path: {:?}", &path);
if path.exists() && path.is_file() {
info!("Found config file at: {:?}", path);
config_found = true;
let config_file = config::File::with_name(path.to_str().unwrap());
config.merge(config_file)?;
}
}
if require_config && !config_found {
return Err(Error::FileNotFound(
"Couldn't find a configuration file. Did you start the daemon yet?".into(),
));
}
Ok(config.try_into()?)
}
fn default_dark_mode() -> bool {
false
}
fn default_callback_log_lines() -> usize {
10
}