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

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![