1
0
mirror of https://github.com/jht5945/buildj.git synced 2026-01-13 00:30:04 +08:00

Compare commits

...

2 Commits

Author SHA1 Message Date
6fa4b60bc4 chore: code style 2020-09-20 17:36:27 +08:00
e227603ab2 feat: use rust_util 0.6.15 2020-09-20 16:59:50 +08:00
9 changed files with 63 additions and 104 deletions

6
Cargo.lock generated
View File

@@ -72,7 +72,7 @@ dependencies = [
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"reqwest 0.9.22 (registry+https://github.com/rust-lang/crates.io-index)",
"rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)",
"rust_util 0.6.14 (registry+https://github.com/rust-lang/crates.io-index)",
"rust_util 0.6.15 (registry+https://github.com/rust-lang/crates.io-index)",
"term 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
"urlencoding 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -1015,7 +1015,7 @@ dependencies = [
[[package]]
name = "rust_util"
version = "0.6.14"
version = "0.6.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -1619,7 +1619,7 @@ dependencies = [
"checksum reqwest 0.9.22 (registry+https://github.com/rust-lang/crates.io-index)" = "2c2064233e442ce85c77231ebd67d9eca395207dec2127fe0bbedde4bd29a650"
"checksum rust-argon2 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4ca4eaef519b494d1f2848fc602d18816fed808a981aedf4f1f00ceb7c9d32cf"
"checksum rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)" = "f76d05d3993fd5f4af9434e8e436db163a12a9d40e1a58a726f27a01dfd12a2a"
"checksum rust_util 0.6.14 (registry+https://github.com/rust-lang/crates.io-index)" = "7222f977acb4264fb55f1aa7cf11e09c735fe961b369aef92eea670949628498"
"checksum rust_util 0.6.15 (registry+https://github.com/rust-lang/crates.io-index)" = "754278eaff41b86ced9e2913b3f5ee8bd7c2446be81f0739a567e9d0ad6cdb3a"
"checksum rustc-demangle 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "4c691c0e608126e00913e33f0ccf3727d5fc84573623b8d65b2df340b5201783"
"checksum rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)" = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda"
"checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"

View File

@@ -12,4 +12,4 @@ urlencoding = "1.1.1"
dirs = "3.0.1"
rust-crypto = "0.2.36"
lazy_static = "1.4.0"
rust_util = "0.6.14"
rust_util = "0.6.15"

View File

@@ -106,17 +106,17 @@ pub fn find_build_json_in_parents() -> Option<String> {
}
pub fn find_build_json() -> Option<String> {
match find_build_json_in_current() {
Some(p) => Some(p),
None => match find_build_json_in_parents() {
Some(p) => {
warning!("Cannot find {} in current dir, find: {}", BUILD_JSON, p);
Some(p)
},
None => {
failure!("Cannot find {}", BUILD_JSON);
None
},
if let Some(p) = find_build_json_in_current() {
return Some(p);
}
match find_build_json_in_parents() {
Some(p) => {
warning!("Cannot find {} in current dir, find: {}", BUILD_JSON, p);
Some(p)
},
None => {
failure!("Cannot find {}", BUILD_JSON);
None
},
}
}

View File

@@ -1,5 +1,5 @@
use std::fs::File;
use rust_util::{ XResult, util_io::copy_io };
use rust_util::{ XResult, util_io };
use crate::misc::VERBOSE;
@@ -10,24 +10,20 @@ pub fn download_url(url: &str, dest: &mut File) -> XResult<()> {
let mut response = reqwest::get(url)?;
let header_content_length: i64 = match response.headers().get("content-length") {
None => -1_i64, Some(len_value) => {
let len_str = match len_value.to_str() {
Ok(len_str) => len_str, Err(err) => {
warning!("Get content length for {:?}, error: {}", len_value, err);
"-1"
},
};
match len_str.parse::<i64>() {
Ok(len) => len, Err(err) => {
warning!("Get content length for {:?}, error: {}", len_value, err);
-1
}
}
let len_str = len_value.to_str().unwrap_or_else(|err| {
warning!("Get content length for {:?}, error: {}", len_value, err);
"-1"
});
len_str.parse::<i64>().unwrap_or_else(|err| {
warning!("Get content length for {:?}, error: {}", len_value, err);
-1
})
},
};
if *VERBOSE {
debugging!("Content-Length: {}", header_content_length);
}
copy_io(&mut response, dest, header_content_length)?;
util_io::copy_io_default(&mut response, dest, header_content_length)?;
Ok(())
}

View File

@@ -1,13 +1,6 @@
use std::{
collections::HashMap,
env,
fs,
str,
path::Path,
process::Command,
};
use rust_util::util_os::*;
use crate::{ local_util, tool, misc::* };
use std::{ collections::HashMap, env, fs, str, path::Path, process::Command };
use rust_util::util_os;
use crate::{ local_util, tool, misc::{ VERBOSE } };
const PATH: &str = "PATH";
const JAVA_HOME: &str = "JAVA_HOME";
@@ -34,13 +27,13 @@ pub fn get_java_home(version: &str) -> Option<String> {
}
pub fn get_cloud_java(version: &str) -> bool {
if ! is_macos_or_linux() {
if ! util_os::is_macos_or_linux() {
return false;
}
let cloud_java_names = match &*BUILDJ_JAVA_NAME {
None => if is_macos() {
None => if util_os::is_macos() {
vec![OPENJDK_MACOS]
} else if is_linux() {
} else if util_os::is_linux() {
vec![JDK_LINUX, OPENJDK_LINUX]
} else {
vec![]
@@ -60,7 +53,7 @@ pub fn get_cloud_java(version: &str) -> bool {
}
pub fn get_macos_java_home(version: &str) -> Option<String> {
if ! is_macos() {
if ! util_os::is_macos() {
return None;
}
let output = Command::new(MACOS_LIBEXEC_JAVAHOME).arg("-version").arg(version).output().ok()?;

View File

@@ -1,18 +1,6 @@
use std::{
env,
fs::{ self, File },
io::{ Read, ErrorKind },
path::Path,
process::Command,
time::SystemTime,
};
use rust_util::{ XResult, new_box_ioerror, util_io::* };
use crypto::{
digest::Digest,
md5::Md5,
sha1::Sha1,
sha2::{ Sha256, Sha512 },
};
use std::{ env, fs::{ self, File }, io::{ Read, ErrorKind }, path::Path, process::Command };
use rust_util::{ XResult, new_box_ioerror, util_io::{ self, DEFAULT_BUF_SIZE, PrintStatusContext } };
use crypto::{ digest::Digest, md5::Md5, sha1::Sha1, sha2::{ Sha256, Sha512 } };
pub fn get_args_as_vec() -> Vec<String> {
env::args().collect::<Vec<String>>()
@@ -55,7 +43,7 @@ pub fn calc_file_digest(digest: &mut dyn Digest, digest_alg: &str, file_name: &s
let mut buf: [u8; DEFAULT_BUF_SIZE] = [0u8; DEFAULT_BUF_SIZE];
let mut f = File::open(file_name)?;
let file_len = f.metadata().map(|md| md.len() as i64).unwrap_or(-1_i64);
let start = SystemTime::now();
let mut print_status_context = PrintStatusContext::default();
let mut written = 0_i64;
loop {
let len = match f.read(&mut buf) {
@@ -66,8 +54,7 @@ pub fn calc_file_digest(digest: &mut dyn Digest, digest_alg: &str, file_name: &s
};
digest.input(&buf[..len]);
written += len as i64;
let cost = SystemTime::now().duration_since(start).unwrap();
print_status_last_line(&format!("Calc {}", digest_alg), file_len, written, cost);
util_io::print_status_last_line(&format!("Calc {}", digest_alg), file_len, written, &mut print_status_context);
}
}

View File

@@ -302,23 +302,17 @@ fn read_build_json_object() -> Option<json::JsonValue> {
return Some(o);
}
let build_json = match find_build_json() {
Some(p) => p, None => return None,
};
let build_json = find_build_json()?;
success!("Find {} @ {}", BUILD_JSON, build_json);
let build_json_content = match fs::read_to_string(build_json) {
Ok(content) => content, Err(err) => {
failure!("Read {} failed: {}", BUILD_JSON, err);
return None;
},
};
match json::parse(&build_json_content) {
Ok(object) => Some(object), Err(err) => {
failure!("Parse JSON failed: {}", err);
None
},
}
let build_json_content = fs::read_to_string(build_json).map_err(|err| {
failure!("Read {} failed: {}", BUILD_JSON, err);
err
}).ok()?;
json::parse(&build_json_content).map_err(|err| {
failure!("Parse JSON failed: {}", err);
err
}).ok()
}

View File

@@ -1,12 +1,12 @@
use std::env;
use rust_util::util_env::*;
use rust_util::util_env;
lazy_static! {
pub static ref VERBOSE: bool = is_env_on("BUILDJ_VERBOSE");
pub static ref NOAUTH: bool = is_env_on("BUILDJ_NOAUTH");
pub static ref NOBUILDIN: bool = is_env_on("BUILDJ_NOBUILDIN");
pub static ref AUTH_TOKEN: Option<String> = env::var("BUILDJ_AUTH_TOKEN").ok();
pub static ref JAVA_VERSION: Option<String> = env::var("BUILDJ_JAVA").ok();
pub static ref VERBOSE: bool = util_env::is_env_on("BUILDJ_VERBOSE");
pub static ref NOAUTH: bool = util_env::is_env_on("BUILDJ_NOAUTH");
pub static ref NOBUILDIN: bool = util_env::is_env_on("BUILDJ_NOBUILDIN");
pub static ref AUTH_TOKEN: Option<String> = env::var("BUILDJ_AUTH_TOKEN").ok();
pub static ref JAVA_VERSION: Option<String> = env::var("BUILDJ_JAVA").ok();
pub static ref BUILDER_VERSION: Option<String> = env::var("BUILDJ_BUILDER").ok();
}

View File

@@ -1,17 +1,6 @@
use std::{
fs::{ self, File },
path::Path,
};
use rust_util::{
XResult,
new_box_ioerror,
util_os::is_macos_or_linux,
};
use crate::{
http::{ download_url, get_url_content },
local_util::{ self, * },
misc::*,
};
use std::{ fs::{ self, File }, path::Path };
use rust_util::{ XResult, new_box_ioerror, util_os };
use crate::{ http, local_util, misc::{ AUTH_TOKEN, VERBOSE, NOAUTH } };
const M2_HOME: &str = "M2_HOME";
const MAVEN_HOME: &str = "MAVEN_HOME";
@@ -54,7 +43,7 @@ impl BuilderDesc {
}
pub fn get_builder_home(builder: &str, version: &str) -> Option<BuilderDesc> {
let local_builder_home_base_dir = match get_user_home_dir(LOCAL_BUILDER_HOME_BASE_DIR) {
let local_builder_home_base_dir = match local_util::get_user_home_dir(LOCAL_BUILDER_HOME_BASE_DIR) {
Ok(o) => o, Err(_) => return None,
};
let builder_name = match builder {
@@ -75,7 +64,7 @@ pub fn get_builder_home(builder: &str, version: &str) -> Option<BuilderDesc> {
}
pub fn get_cloud_builder(builder: &str, version: &str) -> bool {
if ! is_macos_or_linux() {
if ! util_os::is_macos_or_linux() {
return false;
}
let local_builder_home_base_dir = match local_util::get_user_home_dir(LOCAL_BUILDER_HOME_BASE_DIR) {
@@ -119,7 +108,7 @@ pub fn get_tool_package_secret() -> XResult<String> {
return Ok((*AUTH_TOKEN).as_ref().unwrap().clone());
}
let standard_config_file = get_user_home_dir(STANDARD_CONFIG_JSON)?;
let standard_config_file = local_util::get_user_home_dir(STANDARD_CONFIG_JSON)?;
let standard_config_json = fs::read_to_string(&standard_config_file)?;
let standard_config_object = json::parse(&standard_config_json)?;
@@ -133,7 +122,7 @@ pub fn get_tool_package_secret() -> XResult<String> {
}
pub fn set_tool_package_secret(secret: &str) -> XResult<()> {
let standard_config_file = get_user_home_dir(STANDARD_CONFIG_JSON)?;
let standard_config_file = local_util::get_user_home_dir(STANDARD_CONFIG_JSON)?;
match fs::metadata(&standard_config_file) {
Err(_) => {
@@ -196,7 +185,7 @@ pub fn get_tool_package_detail(name: &str, version: &str) -> XResult<String> {
url.push_str(&urlencoding::encode(name));
url.push_str("&ver=");
url.push_str(&urlencoding::encode(version));
Ok(get_url_content(url.as_str())?)
Ok(http::get_url_content(url.as_str())?)
}
pub fn get_and_extract_tool_package(base_dir: &str, dir_with_name: bool, name: &str, version: &str, extract_match: bool) -> XResult<bool> {
@@ -228,11 +217,11 @@ pub fn get_and_extract_tool_package(base_dir: &str, dir_with_name: bool, name: &
target_base_dir.push_str("/");
target_base_dir.push_str(&format!("{}-{}", n, v));
}
init_dir(&target_base_dir);
local_util::init_dir(&target_base_dir);
let target_file_name = format!("{}/{}", &target_base_dir, name.to_string());
information!("Start download: {} -> {}", &url.to_string(), &target_file_name);
download_url(&url.to_string(), &mut File::create(&target_file_name)?)?;
http::download_url(&url.to_string(), &mut File::create(&target_file_name)?)?;
information!("Start verify integrity: {} ...", &target_file_name);
if local_util::verify_file_integrity(&integrity.to_string(), &target_file_name)? {