From fab2654c10cf0d0f440970d55bbe5841d93a4ae3 Mon Sep 17 00:00:00 2001 From: Hristo Venev Date: Tue, 19 Mar 2019 01:05:24 +0200 Subject: Use curl command instead of library. Rust packaging is complicated and I don't like it. --- src/main.rs | 47 +++++++++++++++++++---------------------------- 1 file changed, 19 insertions(+), 28 deletions(-) (limited to 'src/main.rs') 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, 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 { - use curl::easy::Easy; - - let mut res = Vec::::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 { + 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) } -- cgit