-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.rs
296 lines (257 loc) · 9.16 KB
/
main.rs
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
mod cache;
mod cli;
mod config;
mod feed;
#[cfg(windows)]
mod dirs;
#[cfg(not(windows))]
mod xdg;
#[cfg(not(windows))]
use crate::xdg as dirs;
use std::path::{Path, PathBuf};
use std::process::ExitCode;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use std::{env, fs};
use atomicwrites::AtomicFile;
use eyre::{eyre, Report, WrapErr};
use futures::future;
use log::{debug, error, info};
use reqwest::Client as HttpClient;
use rss::Channel;
use simple_eyre::eyre;
use crate::cache::deserialise_cached_headers;
use crate::config::ConfigHash;
use crate::config::{ChannelConfig, Config};
use crate::dirs::Dirs;
use crate::feed::{process_feed, ProcessResult};
const RSSPLS_LOG: &str = "RSSPLS_LOG";
#[derive(Clone)]
pub struct Client {
/// Whether file URLs are enabled
file_urls: bool,
/// HTTP client
http: HttpClient,
}
#[tokio::main]
async fn main() -> ExitCode {
match try_main().await {
Ok(true) => ExitCode::SUCCESS,
Ok(false) => ExitCode::FAILURE,
Err(report) => {
error!("{:?}", report);
ExitCode::FAILURE
}
}
}
async fn try_main() -> eyre::Result<bool> {
simple_eyre::install()?;
match env::var_os(RSSPLS_LOG) {
None => env::set_var(RSSPLS_LOG, "info"),
Some(_) => {}
}
pretty_env_logger::try_init_custom_env(RSSPLS_LOG)?;
let cli = cli::parse_args().wrap_err("unable to parse CLI arguments")?;
let cli = match cli {
Some(cli) => cli,
// Help or version info was printed and we should return
None => return Ok(true),
};
let config = Config::read(cli.config_path)?;
// Determine output directory
let output_dir = match cli.output_path {
Some(path) => Some(path),
None => config
.rsspls
.output
.map(|ref path| {
dirs::home_dir()
.ok_or_else(|| eyre!("unable to determine home directory"))
.map(|home| expand_tilde(path, home))
})
.transpose()?,
}
.ok_or_else(|| {
eyre!("output directory must be supplied via --output or be present in configuration file")
})?;
// Ensure output directory exists
if !output_dir.exists() {
fs::create_dir_all(&output_dir).wrap_err_with(|| {
format!(
"unable to create output directory: {}",
output_dir.display()
)
})?;
info!("created output directory: {}", output_dir.display());
}
// Set up the HTTP client
let connect_timeout = Duration::from_secs(10);
let timeout = Duration::from_secs(30);
let mut client_builder = HttpClient::builder()
.connect_timeout(connect_timeout)
.timeout(timeout);
// Add proxy if provided
match config.rsspls.proxy {
Some(proxy) => {
debug!("using proxy from configuration file: {}", proxy);
client_builder = client_builder.proxy(reqwest::Proxy::all(proxy)?)
}
None => {
if let Ok(proxy) = env::var("http_proxy") {
debug!("using http proxy from 'http_proxy' env var: {}", proxy);
client_builder = client_builder.proxy(reqwest::Proxy::http(proxy)?)
}
if let Ok(proxy) = env::var("HTTPS_PROXY") {
debug!("using https proxy from 'HTTPS_PROXY' env var: {}", proxy);
client_builder = client_builder.proxy(reqwest::Proxy::https(proxy)?)
}
}
};
let client = Client {
file_urls: config.rsspls.file_urls,
http: client_builder
.build()
.wrap_err("unable to build HTTP client")?,
};
// Wrap up xdg::BaseDirectories for sharing between tasks. Mutex is used so that only one
// thread at a time will attempt to create cache directories.
let dirs = dirs::new()?;
let dirs = Arc::new(Mutex::new(dirs));
// Spawn the tasks
let config_hash = Arc::new(config.hash.clone());
let futures = config.feed.into_iter().map(|feed| {
let client = client.clone(); // Client uses Arc internally
let output_dir = output_dir.clone();
let dirs = Arc::clone(&dirs);
let config_hash = Arc::clone(&config_hash);
tokio::spawn(async move {
let res = process(
&feed,
&client,
ConfigHash(config_hash.as_str()),
output_dir,
dirs,
)
.await;
if let Err(ref report) = res {
// Eat errors when processing feeds so that we don't stop processing the others.
// Errors are reported, then we return a boolean indicating success or not, which
// is used to set the exit status of the program later.
error!("{:?}", report);
}
res.is_ok()
})
});
// Run all the futures at the same time
// The ? here will fail on an error if the JoinHandle fails
let ok = future::try_join_all(futures)
.await?
.into_iter()
.fold(true, |ok, succeeded| ok & succeeded);
Ok(ok)
}
async fn process(
feed: &ChannelConfig,
client: &Client,
config_hash: ConfigHash<'_>,
output_dir: PathBuf,
dirs: Dirs,
) -> Result<(), Report> {
// Generate paths up front so we report any errors before making requests
let filename = Path::new(&feed.filename);
let filename = filename
.file_name()
.map(Path::new)
.ok_or_else(|| eyre!("{} is not a valid file name", filename.display()))?;
let output_path = output_dir.join(filename);
let cache_filename = filename.with_extension("toml");
let cache_path = {
let dirs = dirs.lock().map_err(|_| eyre!("unable to acquire mutex"))?;
dirs.place_cache_file(&cache_filename)
.wrap_err("unable to create path to cache file")
}?;
let cached_headers = deserialise_cached_headers(&cache_path, config_hash);
process_feed(client, feed, config_hash, &cached_headers)
.await
.and_then(|ref process_result| {
match process_result {
ProcessResult::NotModified => Ok(()),
ProcessResult::Ok { channel, headers } => {
// TODO: channel.validate()
write_channel(channel, &output_path).wrap_err_with(|| {
format!("unable to write output file: {}", output_path.display())
})?;
// Update the cache
if let Some(headers) = headers {
debug!("write cache {}", cache_path.display());
fs::write(cache_path, headers).wrap_err("unable to write to cache")?;
}
Ok(())
}
}
})
.wrap_err_with(|| format!("error processing feed for {}", feed.config.url))
}
fn write_channel(channel: &Channel, output_path: &Path) -> Result<(), Report> {
// Write the new file into a temporary location, then move it into place
let file = AtomicFile::new(output_path, atomicwrites::AllowOverwrite);
file.write(|f| {
info!("write {}", output_path.display());
channel
.write_to(f)
.map(drop)
.wrap_err("unable to write feed")
})
.map_err(|err| match err {
atomicwrites::Error::Internal(atomic_err) => atomic_err.into(),
atomicwrites::Error::User(myerr) => myerr,
})
}
pub fn version_string() -> String {
format!("{} version {}", env!("CARGO_PKG_NAME"), version())
}
fn version() -> &'static str {
env!("CARGO_PKG_VERSION")
}
fn expand_tilde<P: Into<PathBuf>>(path: P, mut home: PathBuf) -> PathBuf {
let path = path.into();
// NOTE: starts_with only considers whole path components
if path.starts_with("~") {
if path == Path::new("~") {
home
} else {
home.push(path.strip_prefix("~").unwrap());
home
}
} else {
path
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[cfg(not(windows))]
fn test_home() {
let expanded = expand_tilde("asdf", PathBuf::from("/home/foo"));
assert_eq!(expanded, Path::new("asdf"));
let expanded = expand_tilde("~asdf", PathBuf::from("/home/foo"));
assert_eq!(expanded, Path::new("~asdf"));
let expanded = expand_tilde("~/some/where", PathBuf::from("/home/foo"));
assert_eq!(expanded, Path::new("/home/foo/some/where"));
let expanded = expand_tilde("~/some/where", PathBuf::from("/"));
assert_eq!(expanded, Path::new("/some/where"));
}
#[test]
#[cfg(windows)]
fn test_home_windows() {
let expanded = expand_tilde("asdf", PathBuf::from(r"C:\Users\Foo"));
assert_eq!(expanded, Path::new("asdf"));
let expanded = expand_tilde("~asdf", PathBuf::from(r"C:\Users\Foo"));
assert_eq!(expanded, Path::new("~asdf"));
let expanded = expand_tilde(r"~\some\where", PathBuf::from(r"C:\Users\Foo"));
assert_eq!(expanded, Path::new(r"C:\Users\Foo\some\where"));
let expanded = expand_tilde(r"~\some\where", PathBuf::from(r"C:\"));
assert_eq!(expanded, Path::new(r"C:\some\where"));
}
}