Commit 56efeb14 authored by Wohlgemuth, Jason's avatar Wohlgemuth, Jason
Browse files

feat: Add download_binary function

parent 9082a113
Loading
Loading
Loading
Loading
Loading
+713 −29

File changed.

Preview size limit exceeded, changes collapsed.

+54 −0
Original line number Diff line number Diff line
%% Cell type:code id: tags:

``` rust
:dep pipe-lib = { path = "../pipe-lib" }
```

%% Cell type:code id: tags:

``` rust
:dep reqwest
:dep tokio = { version = "1.39.2", features = ["rt", "time", "rt-multi-thread"] }
:dep bytes
```

%% Cell type:code id: tags:

``` rust
use std::io::copy;
use std::io::Cursor;
use std::fs::File;
use std::path::PathBuf;
use tokio;

pub fn download_binary(url: String) -> Result<bytes::Bytes, String> {
    async fn download(url: String) -> Result<bytes::Bytes, String> {
        let client = reqwest::Client::new();
        let response = client.get(url.clone()).send();
        let filename = PathBuf::from(url.clone()).file_name().unwrap().to_str().unwrap().to_string();
        match response.await {
            | Ok(data) => {
                match data.bytes().await {
                    | Ok(content) => {
                        let mut output = File::create(filename.clone()).unwrap();
                        copy(&mut Cursor::new(content.clone()), &mut output);
                        println!("Downloaded {filename}");
                        Ok(content)
                    },
                    | Err(_) => Err(format!("No content downloaded from {url}"))
                }
            },
            | Err(_) => Err(format!("Failed to download {url}"))
        }
    }
    let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap();
    runtime.block_on(download(url.clone()))
}
```

%% Cell type:code id: tags:

``` rust
let url = "https://code.ornl.gov/api/v4/projects/15383/packages/generic/x86_64-unknown-linux-gnu/v0.0.3/pipe".to_string();
let content = download_binary(url);
println!("{:?}", content);
```

%% Output

    Downloaded pipe

%% Cell type:code id: tags:

``` rust
```
+3 −1
Original line number Diff line number Diff line
@@ -14,7 +14,7 @@ fancy-regex = "0.13.0"
is-terminal = "0.4.9"
owo-colors = "4.0.0"
serde_yml = "0.0.12"
tokio = { version = "1.39.2", features = ["rt", "time"] }
tokio = { version = "1.39.2", features = ["rt", "time", "rt-multi-thread"] }
valuable = "0.1.0"
valuable-derive = "0.1.0"
clap-verbosity-flag = { workspace = true }
@@ -30,3 +30,5 @@ tracing-subscriber = { workspace = true }
uriparse = { workspace = true }
sha2 = "0.10.8"
which = "6.0.3"
reqwest = "0.12.8"
bytes = "1.7.2"
+28 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@
#![allow(clippy::large_enum_variant)]
#![allow(clippy::wrong_self_convention)]
use bon::builder;
use bytes;
use clap::ValueEnum;
use clap_verbosity_flag::Level;
use color_eyre::eyre::{Report, Result};
@@ -9,6 +10,7 @@ use derive_more::{Display, FromStr};
use fancy_regex::Regex;
use is_terminal::IsTerminal;
use owo_colors::{OwoColorize, Style, Styled};
use reqwest;
use serde::ser::StdError;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Number, Value};
@@ -23,6 +25,8 @@ use std::{
    env::{set_var, vars},
    io,
};
use std::io::copy;
use std::io::Cursor;

use sha2::{Digest, Sha256};
use tokio::time::{sleep, Duration, Sleep};
@@ -1278,6 +1282,30 @@ pub fn add_forward_slash(path: PathBuf) -> PathBuf {
        path.to_path_buf()
    }
}
pub fn download_binary(url: String) -> Result<(), String> {
    async fn download(url: String) -> Result<(), String> {
        let client = reqwest::Client::new();
        let response = client.get(url.clone()).send();
        let filename = PathBuf::from(url.clone()).file_name().unwrap().to_str().unwrap().to_string();
        match response.await {
            | Ok(data) => {
                match data.bytes().await {
                    | Ok(content) => {
                        let mut output = File::create(filename.clone()).unwrap();
                        let _ = copy(&mut Cursor::new(content.clone()), &mut output);
                        debug!(filename=filename, "=> {} Downloaded", Label::output());
                        Ok(())
                    },
                    | Err(_) => Err(format!("No content downloaded from {url}"))
                }
            },
            | Err(_) => Err(format!("Failed to download {url}"))
        }
    }
    let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap();
    let _ = runtime.block_on(download(url.clone()));
    Ok(())
}
/// Get SHA256 hash of a file
pub fn get_checksum(path: PathBuf) -> String {
    if let Ok(content) = read_file(path) {