Skip to content

Commit

Permalink
wip fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
ThetaSinner committed Mar 30, 2024
1 parent 876f10b commit 2d74a14
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 11 deletions.
186 changes: 176 additions & 10 deletions checked_cli/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions checked_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ holochain_client = { version = "=0.5.0-dev.31", default-features = false }
anyhow = "1.0.81"
clap = { version = "4.5.2", features = ["derive", "cargo"] }
minisign = "0.7.6"
reqwest = "0.12.2"
tempfile = "3.10.1"
tokio = "1.37.0"
url = "2.5.0"

[target.'cfg(any(windows, unix))'.dependencies]
dirs = "5.0.1"
Expand Down
4 changes: 3 additions & 1 deletion checked_cli/src/bin/checked.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use checked_cli::prelude::*;
use clap::Parser;

fn main() -> anyhow::Result<()> {
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let cli = Cli::parse();

match cli.command {
Expand All @@ -10,6 +11,7 @@ fn main() -> anyhow::Result<()> {
}
Commands::Sign(sign_args) => sign(sign_args)?,
Commands::Verify(verify_args) => verify(verify_args)?,
Commands::Fetch(fetch_args) => fetch(fetch_args).await?,
}

Ok(())
Expand Down
9 changes: 9 additions & 0 deletions checked_cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ pub enum Commands {

/// Verify a file
Verify(VerifyArgs),

/// Fetch an asset from a URL and check signatures for it
Fetch(FetchArgs),
}

#[derive(clap::Args)]
Expand Down Expand Up @@ -86,3 +89,9 @@ pub struct VerifyArgs {
#[arg(long, short)]
pub signature: Option<PathBuf>,
}

#[derive(clap::Args)]
pub struct FetchArgs {
#[arg()]
pub url: String,
}
46 changes: 46 additions & 0 deletions checked_cli/src/fetch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use crate::cli::FetchArgs;
use anyhow::Context;

pub async fn fetch(fetch_args: FetchArgs) -> anyhow::Result<()> {
let fetch_url = url::Url::parse(&fetch_args.url).context("Invalid URL")?;
let tmp_file = tempfile::Builder::new()
.prefix("checked-")
.suffix(".unverified")
.tempfile()
.context("Could not create temporary file")?;

let client = reqwest::Client::new();

let resp = client
.head(fetch_url.clone())
.send()
.await?
.error_for_status()?;

let content_length = resp
.headers()
.get("content-length")
.and_then(|l| l.to_str().ok())
.and_then(|l| l.parse::<usize>().ok())
.unwrap_or(0);

println!("Content length: {}", content_length);

let mut res = reqwest::get(fetch_args.url).await?;

while let Some(c) = res.chunk()? {
tmp_file.as_file_mut().write_all(&c)?;
}

println!("Did initial request");

let content = res.bytes().await?;

println!("Reading body");

std::io::copy(&mut content.as_ref(), &mut tmp_file.as_file())?;

println!("Downloaded to {:?}", tmp_file.path());

Ok(())
}
2 changes: 2 additions & 0 deletions checked_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ pub mod generate;
mod password;
pub mod sign;
pub mod verify;
mod fetch;

pub mod prelude {
pub use crate::cli::{Cli, Commands, GenerateArgs, SignArgs};
pub use crate::generate::generate;
pub use crate::sign::sign;
pub use crate::verify::verify;
pub use crate::fetch::fetch;
}

0 comments on commit 2d74a14

Please sign in to comment.