1
0
mirror of https://github.com/jht5945/buildj.git synced 2025-12-28 01:31:35 +08:00

chore: code style

This commit is contained in:
2020-09-20 17:36:27 +08:00
parent e227603ab2
commit 6fa4b60bc4
7 changed files with 56 additions and 95 deletions

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

@@ -10,18 +10,14 @@ 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 {

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,17 +1,6 @@
use std::{
env,
fs::{ self, File },
io::{ Read, ErrorKind },
path::Path,
process::Command,
};
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>>()
@@ -65,7 +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;
print_status_last_line(&format!("Calc {}", digest_alg), file_len, written, &mut print_status_context);
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)? {