add download

This commit is contained in:
2020-06-18 01:44:44 +08:00
parent c58d9a8770
commit 36a54427db
5 changed files with 104 additions and 44 deletions

32
src/cmd_download.rs Normal file
View File

@@ -0,0 +1,32 @@
use std::fs;
use clap::{ Arg, ArgMatches, SubCommand, App, };
use crate::cmd::{ Command, CommandError, };
use crate::har::*;
pub struct CommandDownload;
impl Command for CommandDownload {
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)?;
let har: Har = serde_json::from_str(&har_str)?;
// println!("{:?}", har);
har.log.entries.iter().for_each(|e| {
println!("{:<100} {}", e.request.url, e.server_ip_address);
});
Ok(())
}
}

View File

@@ -1,20 +0,0 @@
use clap::{ ArgMatches, SubCommand, App, };
use crate::cmd::{ Command, CommandError, };
pub struct CommandSample;
impl Command for CommandSample {
fn subcommand<'a>(&self) -> App<'a, 'a> {
SubCommand::with_name(self.name()).about("Sample subcommand")
}
fn name(&self) -> &str {
"sample"
}
fn run(&self, _arg_matches: &ArgMatches, _sub_arg_matches: &ArgMatches) -> CommandError {
println!("This is test command!");
Ok(())
}
}

View File

@@ -2,48 +2,95 @@ use serde::{Serialize, Deserialize};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct HarCreator {
name: String,
version: String,
pub name: String,
pub version: String,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct HarPageTimings {
#[serde(rename = "onContentLoad")]
on_content_load: f64,
pub on_content_load: f64,
#[serde(rename = "onLoad")]
on_load: f64,
pub on_load: f64,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct HarPage {
#[serde(rename = "startedDateTime")]
started_date_time: String,
id: String,
title: String,
pub started_date_time: String,
pub id: String,
pub title: String,
#[serde(rename = "pageTimings")]
page_timings: HarPageTimings,
pub page_timings: HarPageTimings,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct HarHeader {
pub name: String,
pub value: String,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct HarQueryParam {
pub name: String,
pub value: String,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct HarCookie {
pub name: String,
pub value: String,
pub expires: Option<String>,
#[serde(rename = "httpOnly")]
pub http_only: bool,
pub secure: bool,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct HarRequest {
pub method: String,
pub url: String,
#[serde(rename = "httpVersion")]
pub http_version: String,
pub headers: Vec<HarHeader>,
#[serde(rename = "queryString")]
pub query_string: Vec<HarQueryParam>,
pub cookies: Vec<HarCookie>,
#[serde(rename = "headersSize")]
pub headers_size: i64,
#[serde(rename = "bodySize")]
pub body_size: i64,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct HarEntry {
// startedDateTime
time: f64,
// request
#[serde(rename = "startedDateTime")]
pub started_date_time: String,
pub time: f64,
pub request: HarRequest,
// response
// cache
// timings
// serverIPAddress
#[serde(rename = "serverIPAddress")]
pub server_ip_address: String,
// _initiator
// _priority
// _resourceType
// connection
pageref: String, // @see Harpage::id
#[serde(rename = "_priority")]
pub priority: String,
#[serde(rename = "_resourceType")]
pub resource_ype: String,
pub connection: Option<String>,
pub pageref: Option<String>, // @see Harpage::id
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct HarLog {
version: String,
creator: HarCreator,
pages: Vec<HarPage>,
entries: Vec<HarEntry>,
pub version: String,
pub creator: HarCreator,
pub pages: Vec<HarPage>,
pub entries: Vec<HarEntry>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Har {
pub log: HarLog,
}

View File

@@ -1,17 +1,17 @@
use clap::App;
mod cmd;
mod cmd_sample;
mod cmd_download;
mod cmd_default;
mod har;
use cmd::{ Command, CommandError, };
use cmd_sample::CommandSample;
use cmd_download::CommandDownload;
use cmd_default::CommandDefault;
fn main() -> CommandError {
let commands = vec![
CommandSample{},
let commands: Vec<Box<dyn Command>> = vec![
Box::new(CommandDownload{}),
];
let mut app = App::new(env!("CARGO_PKG_NAME"))
.version(env!("CARGO_PKG_VERSION"))