Skip to content

Commit

Permalink
refactor: log
Browse files Browse the repository at this point in the history
  • Loading branch information
cangzhang committed Jan 20, 2024
1 parent 1120c80 commit 0de47d2
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 173 deletions.
142 changes: 0 additions & 142 deletions Cargo.lock

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

3 changes: 0 additions & 3 deletions crates/lcu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde_with = { version = "3.0", features = ["json"] }
serde_derive = "1.0"
tracing = "0.1.37"
tracing-subscriber = "0.3.17"
tracing-appender = "0.2.2"
regex = "1.8.1"
anyhow = "1.0.71"
bytes = "*"
Expand Down
2 changes: 1 addition & 1 deletion crates/lcu/src/builds.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use futures::StreamExt;

use kv_log_macro::info;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::{
Expand All @@ -8,7 +9,6 @@ use std::{
path::Path,
sync::{Arc, Mutex},
};
use tracing::info;

use crate::web::{self, ChampionsMap, FetchError};

Expand Down
2 changes: 1 addition & 1 deletion crates/lcu/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::str;
use std::sync::{Arc, Mutex};

#[allow(unused_imports)]
use tracing::{error, info};
use kv_log_macro::{error, info};

const APP_PORT_KEY: &str = "--app-port=";
const TOKEN_KEY: &str = "--remoting-auth-token=";
Expand Down
51 changes: 30 additions & 21 deletions crates/lcu/src/web.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
use std::{collections::HashMap, io::{Cursor, self}, path::Path, fs};
use std::{
collections::HashMap,
fs,
io::{self, Cursor},
path::Path,
};

use anyhow::{anyhow, Context};
use flate2::read::GzDecoder;
use futures::future::join_all;
use futures::future::try_join3;
use kv_log_macro::{error, info, warn};
use reqwest::header::USER_AGENT;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use tracing::error;
use flate2::read::GzDecoder;
use tar::Archive;
use kv_log_macro as log;
use futures::future::join_all;

use crate::{
builds::{self, BuildData, ItemBuild},
constants::VERSION,
source::SourceItem,
};

Expand Down Expand Up @@ -201,7 +204,7 @@ pub async fn fetch_latest_release() -> Result<LatestRelease, FetchError> {

match client
.get("https://api.github.com/repos/cangzhang/champ-r/releases/latest".to_string())
.header(USER_AGENT, format!("ChampR {VERSION}"))
.header(USER_AGENT, "ChampR_rs")
.send()
.await
{
Expand Down Expand Up @@ -275,10 +278,10 @@ pub async fn read_local_build_file(file_path: String) -> anyhow::Result<Value> {
Ok(parsed)
}

pub async fn read_from_local_folder(output_dir: &str) -> anyhow::Result<Vec<Vec<builds::BuildSection>>> {
use log::*;

let paths = std::fs::read_dir(output_dir)?
pub async fn read_from_local_folder(
output_dir: &str,
) -> anyhow::Result<Vec<Vec<builds::BuildSection>>> {
let paths = fs::read_dir(output_dir)?
.filter_map(Result::ok)
.filter(|entry| {
entry.path().is_file()
Expand Down Expand Up @@ -313,24 +316,28 @@ pub async fn read_from_local_folder(output_dir: &str) -> anyhow::Result<Vec<Vec<
Ok(files)
}

pub async fn download_tar_and_apply_for_source(source: &String, lol_dir: Option<String>, is_tencent: bool) -> anyhow::Result<()> {
pub async fn download_tar_and_apply_for_source(
source: &String,
lol_dir: Option<String>,
is_tencent: bool,
) -> anyhow::Result<()> {
let (_version, tar_url) = get_remote_package_data(source).await?;
log::info!("found download url for {}, {}", &source, &tar_url);

info!("found download url for {}, {}", &source, &tar_url);

let output_dir = format!(".npm/{source}");
let output_path = Path::new(&output_dir);

if let Err(err) = fs::create_dir_all(output_path) {
log::error!("create output dir: {:?}", err);
error!("create output dir: {:?}", err);
return Err(anyhow!("create output dir: {:?}", err));
}

download_and_extract_tgz(&tar_url, &output_dir).await?;
let dest_folder = format!("{}/package", &output_dir);
let files = read_from_local_folder(&dest_folder).await?;
log::info!("found {} builds for {}", files.len(), source);

info!("found {} builds for {}", files.len(), source);

if lol_dir.is_some() {
let dir = lol_dir.unwrap();
Expand All @@ -348,13 +355,14 @@ pub async fn download_tar_and_apply_for_source(source: &String, lol_dir: Option<
#[cfg(test)]
mod tests {
use super::*;

#[tokio::test]
async fn apply_builds_for_riot_server() -> anyhow::Result<()> {
femme::with_level(femme::LevelFilter::Info);

let source = String::from("op.gg");
download_tar_and_apply_for_source(&source, Some(String::from(".local_builds")), false).await?;
download_tar_and_apply_for_source(&source, Some(String::from(".local_builds")), false)
.await?;

Ok(())
}
Expand All @@ -364,7 +372,8 @@ mod tests {
femme::with_level(femme::LevelFilter::Info);

let source = String::from("op.gg");
download_tar_and_apply_for_source(&source, Some(String::from(".local_builds")), true).await?;
download_tar_and_apply_for_source(&source, Some(String::from(".local_builds")), true)
.await?;

Ok(())
}
Expand Down
Loading

0 comments on commit 0de47d2

Please sign in to comment.