1
0
mirror of https://github.com/jht5945/buildj.git synced 2025-12-29 10:20:04 +08:00

style: use rust_util s macros

This commit is contained in:
2020-08-02 12:08:07 +08:00
parent ddf1c54d5d
commit de23b593fe
3 changed files with 19 additions and 34 deletions

View File

@@ -6,14 +6,7 @@ use std::{
path::Path, path::Path,
process::Command, process::Command,
}; };
use rust_util::{ use rust_util::util_os::*;
iff,
util_os::*,
util_msg::{
print_error,
print_debug,
},
};
use super::{ use super::{
local_util, local_util,
tool, tool,
@@ -67,7 +60,7 @@ pub fn get_cloud_java(version: &str) -> bool {
return true; return true;
} }
} }
print_error(&format!("Get java failed, version: {}", version)); failure!("Get java failed, version: {}", version);
false false
} }
@@ -78,7 +71,7 @@ pub fn get_macos_java_home(version: &str) -> Option<String> {
let output = Command::new(MACOS_LIBEXEC_JAVAHOME).arg("-version").arg(version).output().ok()?; let output = Command::new(MACOS_LIBEXEC_JAVAHOME).arg("-version").arg(version).output().ok()?;
let output_in_utf8 = str::from_utf8(&output.stderr).ok()?; let output_in_utf8 = str::from_utf8(&output.stderr).ok()?;
if *VERBOSE { if *VERBOSE {
print_debug(&format!("java_home outputs: {}", output_in_utf8)); debugging!("java_home outputs: {}", output_in_utf8);
} }
if output_in_utf8.contains("Unable to find any JVMs") { if output_in_utf8.contains("Unable to find any JVMs") {
None None
@@ -94,7 +87,7 @@ pub fn get_local_java_home(version: &str) -> Option<String> {
if let Ok(dir_entry) = path { if let Ok(dir_entry) = path {
if let Some(p)= dir_entry.path().to_str() { if let Some(p)= dir_entry.path().to_str() {
if *VERBOSE { if *VERBOSE {
print_debug(&format!("Try match path: {}", p)); debugging!("Try match path: {}", p);
} }
let mut path_name = p; let mut path_name = p;
if p.ends_with('/') { if p.ends_with('/') {
@@ -111,7 +104,7 @@ pub fn get_local_java_home(version: &str) -> Option<String> {
}; };
if let Some(matched_path) = matched_path_opt { if let Some(matched_path) = matched_path_opt {
if *VERBOSE { if *VERBOSE {
print_debug(&format!("Matched JDK path found: {}", matched_path)); debugging!("Matched JDK path found: {}", matched_path);
} }
return if local_util::is_path_exists(matched_path, "Contents/Home") { return if local_util::is_path_exists(matched_path, "Contents/Home") {
Some(format!("{}/{}", matched_path, "Contents/Home")) Some(format!("{}/{}", matched_path, "Contents/Home"))
@@ -128,7 +121,7 @@ pub fn get_local_java_home(version: &str) -> Option<String> {
pub fn extract_jdk_and_wait(file_name: &str) { pub fn extract_jdk_and_wait(file_name: &str) {
if let Ok(local_java_home_base_dir) = local_util::get_user_home_dir(LOCAL_JAVA_HOME_BASE_DIR) { if let Ok(local_java_home_base_dir) = local_util::get_user_home_dir(LOCAL_JAVA_HOME_BASE_DIR) {
local_util::extract_package_and_wait(&local_java_home_base_dir, file_name).unwrap_or_else(|err| { local_util::extract_package_and_wait(&local_java_home_base_dir, file_name).unwrap_or_else(|err| {
print_error(&format!("Extract file: {}, failed: {}", file_name, err)); failure!("Extract file: {}, failed: {}", file_name, err);
}); });
} }
} }

View File

@@ -10,7 +10,6 @@ use std::{
use rust_util::{ use rust_util::{
XResult, XResult,
new_box_ioerror, new_box_ioerror,
util_msg::print_error,
util_io::*, util_io::*,
}; };
@@ -51,7 +50,7 @@ pub fn verify_file_integrity(integrity: &str, file_name: &str) -> XResult<bool>
}; };
let integrity_verify_result = digest_hex == calc_digest_hex.as_str(); let integrity_verify_result = digest_hex == calc_digest_hex.as_str();
if ! integrity_verify_result { if ! integrity_verify_result {
print_error(&format!("Verify integrity failed, expected: {}, actual: {}", digest_hex, calc_digest_hex)); failure!("Verify integrity failed, expected: {}, actual: {}", digest_hex, calc_digest_hex);
} }
Ok(integrity_verify_result) Ok(integrity_verify_result)
}, },
@@ -131,7 +130,7 @@ pub fn init_home_dir(home_sub_dir: &str) {
pub fn init_dir(dir: &str) { pub fn init_dir(dir: &str) {
if ! Path::new(dir).exists() { if ! Path::new(dir).exists() {
fs::create_dir_all(dir).unwrap_or_else(|err| { fs::create_dir_all(dir).unwrap_or_else(|err| {
print_error(&format!("Init dir {} failed: {}", dir, err)); failure!("Init dir {} failed: {}", dir, err);
}); });
} }
} }

View File

@@ -6,13 +6,6 @@ use rust_util::{
XResult, XResult,
new_box_ioerror, new_box_ioerror,
util_os::is_macos_or_linux, util_os::is_macos_or_linux,
util_msg::{
print_ok,
print_info,
print_warn,
print_error,
print_debug,
},
}; };
use super::{ use super::{
http::{ download_url, get_url_content, }, http::{ download_url, get_url_content, },
@@ -68,7 +61,7 @@ pub fn get_builder_home(builder: &str, version: &str) -> Option<BuilderDesc> {
"maven" => BuilderName::Maven, "maven" => BuilderName::Maven,
"gradle" => BuilderName::Gradle, "gradle" => BuilderName::Gradle,
_ => { _ => {
print_error(&format!("Unknown builder: {}", builder)); failure!("Unknown builder: {}", builder);
return None; return None;
}, },
}; };
@@ -90,7 +83,7 @@ pub fn get_cloud_builder(builder: &str, version: &str) -> bool {
}; };
match get_and_extract_tool_package(&local_builder_home_base_dir, true, builder, version, true) { match get_and_extract_tool_package(&local_builder_home_base_dir, true, builder, version, true) {
Ok(_) => true, Err(err) => { Ok(_) => true, Err(err) => {
print_error(&format!("Get builder: {} failed, version: {}, error: {}", builder, version, err)); failure!("Get builder: {} failed, version: {}, error: {}", builder, version, err);
false false
}, },
} }
@@ -99,7 +92,7 @@ pub fn get_cloud_builder(builder: &str, version: &str) -> bool {
pub fn get_local_builder_home_sub(builder_name: BuilderName, local_builder_home_dir: &str) -> Option<BuilderDesc> { pub fn get_local_builder_home_sub(builder_name: BuilderName, local_builder_home_dir: &str) -> Option<BuilderDesc> {
match get_local_builder_home_sub_first_sub_dir(local_builder_home_dir) { match get_local_builder_home_sub_first_sub_dir(local_builder_home_dir) {
None => { None => {
print_error(&format!("Cannot find builder home in: {}", local_builder_home_dir)); failure!("Cannot find builder home in: {}", local_builder_home_dir);
None None
}, },
Some(p) => Some(BuilderDesc{name: builder_name, home: p, bin: None}), Some(p) => Some(BuilderDesc{name: builder_name, home: p, bin: None}),
@@ -121,7 +114,7 @@ pub fn get_local_builder_home_sub_first_sub_dir(local_builder_home_dir: &str) ->
pub fn get_tool_package_secret() -> XResult<String> { pub fn get_tool_package_secret() -> XResult<String> {
if (*AUTH_TOKEN).is_some() { if (*AUTH_TOKEN).is_some() {
if *VERBOSE { if *VERBOSE {
print_debug("Use auth token from env 'BUILDJ_AUTH_TOKEN'"); debugging!("Use auth token from env 'BUILDJ_AUTH_TOKEN'");
} }
return Ok((*AUTH_TOKEN).as_ref().unwrap().clone()); return Ok((*AUTH_TOKEN).as_ref().unwrap().clone());
} }
@@ -175,12 +168,12 @@ pub fn set_tool_package_secret(secret: &str) -> XResult<()> {
pub fn get_tool_package_detail(name: &str, version: &str) -> XResult<String> { pub fn get_tool_package_detail(name: &str, version: &str) -> XResult<String> {
let secret: Option<String> = if *NOAUTH { let secret: Option<String> = if *NOAUTH {
print_warn("Running in no auth mode!"); warning!("Running in no auth mode!");
None None
} else { } else {
match get_tool_package_secret() { match get_tool_package_secret() {
Ok(r) => Some(r), Err(err) => { Ok(r) => Some(r), Err(err) => {
print_warn(&format!("Get package detail secret failed: {}, from file: ~/{}", err, STANDARD_CONFIG_JSON)); warning!("Get package detail secret failed: {}, from file: ~/{}", err, STANDARD_CONFIG_JSON);
None None
}, },
} }
@@ -210,7 +203,7 @@ pub fn get_and_extract_tool_package(base_dir: &str, dir_with_name: bool, name: &
let tool_package_detail = get_tool_package_detail(name, version)?; let tool_package_detail = get_tool_package_detail(name, version)?;
let build_json_object = json::parse(&tool_package_detail)?; let build_json_object = json::parse(&tool_package_detail)?;
if *VERBOSE { if *VERBOSE {
print_debug(&format!("Get tool {}:{}, result JSON: {}", name, version, json::stringify_pretty(build_json_object.clone(), 4))); debugging!("Get tool {}:{}, result JSON: {}", name, version, json::stringify_pretty(build_json_object.clone(), 4));
} }
if build_json_object["status"] != 200 { if build_json_object["status"] != 200 {
return Err(new_box_ioerror(&format!("Error in get tool package detail: {}", build_json_object["message"]))); return Err(new_box_ioerror(&format!("Error in get tool package detail: {}", build_json_object["message"])));
@@ -238,17 +231,17 @@ pub fn get_and_extract_tool_package(base_dir: &str, dir_with_name: bool, name: &
init_dir(&target_base_dir); init_dir(&target_base_dir);
let target_file_name = format!("{}/{}", &target_base_dir, name.to_string()); let target_file_name = format!("{}/{}", &target_base_dir, name.to_string());
print_info(&format!("Start download: {} -> {}", &url.to_string(), &target_file_name)); information!("Start download: {} -> {}", &url.to_string(), &target_file_name);
download_url(&url.to_string(), &mut File::create(&target_file_name)?)?; download_url(&url.to_string(), &mut File::create(&target_file_name)?)?;
print_info(&format!("Start verify integrity: {} ...", &target_file_name)); information!("Start verify integrity: {} ...", &target_file_name);
if local_util::verify_file_integrity(&integrity.to_string(), &target_file_name)? { if local_util::verify_file_integrity(&integrity.to_string(), &target_file_name)? {
print_ok("Verify integrity success."); success!("Verify integrity success.");
} else { } else {
return Err(new_box_ioerror("Verify integrity failed!")); return Err(new_box_ioerror("Verify integrity failed!"));
} }
print_info(&format!("Start extract file: {}", &target_file_name)); success!("Start extract file: {}", &target_file_name);
local_util::extract_package_and_wait(&target_base_dir, &name.to_string())?; local_util::extract_package_and_wait(&target_base_dir, &name.to_string())?;
Ok(true) Ok(true)