diff options
author | Hristo Venev <hristo@venev.name> | 2019-03-19 01:05:24 +0200 |
---|---|---|
committer | Hristo Venev <hristo@venev.name> | 2019-03-19 01:05:24 +0200 |
commit | fab2654c10cf0d0f440970d55bbe5841d93a4ae3 (patch) | |
tree | 9ebaabebea3cc67c8ff5dda3676f830bebc91ca3 /src/main.rs | |
parent | d260190b3074d764cf2423b7187dc58cd37b9a7c (diff) |
Use curl command instead of library.
Rust packaging is complicated and I don't like it.
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 47 |
1 files changed, 19 insertions, 28 deletions
diff --git a/src/main.rs b/src/main.rs index deea336..f0fa41b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,6 +34,7 @@ pub struct Device { update_config: config::UpdateConfig, sources: Vec<Source>, current: wg::Config, + curl_command: String, } impl Device { @@ -46,6 +47,7 @@ impl Device { update_config: c.update, sources: c.sources.into_iter().map(Source::new).collect(), current, + curl_command: c.curl_command, } } @@ -98,8 +100,7 @@ impl Device { continue; } - let r = fetch_source(&src.config.url); - let r = match r { + let r = match fetch_source(&self.curl_command, &src.config.url) { Ok(r) => { eprintln!("<6>Updated [{}]", &src.config.url); src.data = Some(r); @@ -144,34 +145,24 @@ impl Device { } } -fn fetch_source(url: &str) -> io::Result<proto::Source> { - use curl::easy::Easy; - - let mut res = Vec::<u8>::new(); - - { - let mut req = Easy::new(); - req.url(url)?; - - { - let mut tr = req.transfer(); - tr.write_function(|data| { - res.extend_from_slice(data); - Ok(data.len()) - })?; - tr.perform()?; - } - - let code = req.response_code()?; - if code != 0 && code != 200 { - return Err(io::Error::new( - io::ErrorKind::Other, - format!("HTTP error {}", code), - )); - } +fn fetch_source(curl_command: &str, url: &str) -> io::Result<proto::Source> { + use std::process::{Command, Stdio}; + + let out = Command::new(curl_command) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::null()) + .arg("--fail") + .arg("--fail-early") + .arg("--") + .arg(url) + .output()?; + + if !out.status.success() { + return Err(io::Error::new(io::ErrorKind::Other, format!("Failed to download [{}]", url))); } - let mut de = serde_json::Deserializer::from_slice(&res); + let mut de = serde_json::Deserializer::from_slice(&out.stdout); let r = serde::Deserialize::deserialize(&mut de)?; Ok(r) } |