use pretty_en_logger, fill download

This commit is contained in:
2020-06-19 08:26:11 +08:00
parent 2294a2f5d3
commit bd71cf84a8
6 changed files with 34 additions and 14 deletions

12
Cargo.lock generated
View File

@@ -259,8 +259,8 @@ name = "hardownload"
version = "0.1.0"
dependencies = [
"clap",
"env_logger",
"log",
"pretty_env_logger",
"reqwest",
"serde",
"serde_json",
@@ -644,6 +644,16 @@ version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "237a5ed80e274dbc66f86bd59c1e25edc039660be53194b5fe0a482e0f2612ea"
[[package]]
name = "pretty_env_logger"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "926d36b9553851b8b0005f1275891b392ee4d2d833852c417ed025477350fb9d"
dependencies = [
"env_logger",
"log",
]
[[package]]
name = "proc-macro2"
version = "1.0.18"

View File

@@ -13,4 +13,4 @@ reqwest = { version = "0.10", features = ["blocking", "json"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
log = "0.4.8"
env_logger = "0.7.1"
pretty_env_logger = "0.4.0"

View File

@@ -4,9 +4,9 @@ pub type CommandError = Result<(), Box<dyn std::error::Error>>;
pub trait Command {
fn subcommand<'a>(&self) -> App<'a, 'a>;
fn name(&self) -> &str;
fn subcommand<'a>(&self) -> App<'a, 'a>;
fn run(&self, arg_matches: &ArgMatches, _: &ArgMatches) -> CommandError;
}

View File

@@ -16,9 +16,9 @@ impl CommandDefault {
pub fn run(arg_matches: &ArgMatches) -> CommandError {
let verbose_count = arg_matches.occurrences_of("verbose");
println!("[INFO] Verbose count: {}", verbose_count);
println!("[INFO] This is default command cli ...");
// TODO ...
println!("Verbose count: {}", verbose_count);
error!("Default command is not implemented!");
Ok(())
}
}

View File

@@ -1,4 +1,5 @@
use std::fs;
use std::collections::HashSet;
use clap::{ Arg, ArgMatches, SubCommand, App, };
use crate::cmd::{ Command, CommandError, };
use crate::har::*;
@@ -6,16 +7,13 @@ use crate::har::*;
pub struct CommandDownload;
impl Command for CommandDownload {
fn name(&self) -> &str { "download" }
fn subcommand<'a>(&self) -> App<'a, 'a> {
SubCommand::with_name(self.name()).about("Download subcommand")
.arg(Arg::with_name("FILE_NAME").required(true).help("Har file name"))
}
fn name(&self) -> &str {
"download"
}
fn run(&self, _arg_matches: &ArgMatches, sub_arg_matches: &ArgMatches) -> CommandError {
let file_name = sub_arg_matches.value_of("FILE_NAME").unwrap();
let har_str = fs::read_to_string(file_name)?;
@@ -40,8 +38,13 @@ impl Command for CommandDownload {
return;
},
};
let mut ignore_header_names: HashSet<&str> = HashSet::new();
ignore_header_names.insert("if-none-match");
ignore_header_names.insert("if-modified-since");
for header in &reqeust.headers {
client = client.header(&header.name, &header.value);
if !ignore_header_names.contains(header.name.as_str()) {
client = client.header(&header.name, &header.value);
}
}
let response = match client.send() {
Ok(r) => r, Err(e) => {
@@ -51,9 +54,16 @@ impl Command for CommandDownload {
};
let code = response.status().as_u16();
if code != 200 {
// TODO ...
error!("Response url: {}, not success: {}", reqeust.url, code);
return;
}
let _bs = match response.bytes() {
Ok(b) => b, Err(e) => {
error!("Response url: {}, error: {}", reqeust.url, e);
return;
},
};
todo!(); // write file
}))
});

View File

@@ -11,7 +11,7 @@ use cmd_download::CommandDownload;
use cmd_default::CommandDefault;
fn main() -> CommandError {
env_logger::init();
pretty_env_logger::init();
info!("hardownload started");
let commands: Vec<Box<dyn Command>> = vec![