From c4abe1277f113e1b63b8a38d18855d0b15652fa2 Mon Sep 17 00:00:00 2001 From: crunk Date: Sat, 23 Sep 2023 15:26:01 +0200 Subject: [PATCH] reqwest example combined with cli tutorial --- README.md | 4 +++- src/main.rs | 28 ++++++++++++++++++---------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index c292871..6821a2f 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,8 @@ I actually never did this properly. currently reading [this guide](https://clig. ## Existential crisis * ideally a tool on your machine should be easy to install * python versions, venvs etc feels more like a problem here than in a web application -* attempt to write something in go? +* attempt to write something in rust? +* currently following some rust tutorials ## Readme driven development * have crunk-scheduler do all the conversion to html in rss. @@ -19,3 +20,4 @@ I actually never did this properly. currently reading [this guide](https://clig. - post a simple text - post markdown or html snippets - post files +* be able to set a time when something is scheduled. diff --git a/src/main.rs b/src/main.rs index 1a9a7f5..8216767 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +#![deny(warnings)] + use clap::Parser; /// Search for a pattern in a file and display the lines that contain it. @@ -5,18 +7,24 @@ use clap::Parser; struct Cli { url: String, - path: std::path::PathBuf, } -fn main() -> Result<(), Box> { +// This is using the `tokio` runtime. You'll need the following dependency: +// +// `tokio = { version = "1", features = ["full"] }` +#[cfg(not(target_arch = "wasm32"))] +#[tokio::main] +async fn main() -> Result<(), reqwest::Error> { + // Some simple CLI args requirements... let args = Cli::parse(); - let result = std::fs::read_to_string(&args.path); - let content = match result { - Ok(content) => content, - Err(error) => { - return Err(error.into()); - } - }; - println!("file content: {}", content); + let url = args.url; + + eprintln!("Fetching {:?}...", url); + let res = reqwest::get(url).await?; + + eprintln!("Response: {:?} {}", res.version(), res.status()); + eprintln!("Headers: {:#?}\n", res.headers()); + let body = res.text().await?; + println!("{}", body); Ok(()) }