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

add buildj :::config set

This commit is contained in:
2019-08-16 23:29:02 +08:00
parent 644a57f900
commit c315cbad9d
2 changed files with 48 additions and 5 deletions

View File

@@ -72,15 +72,24 @@ fn do_with_buildin_arg_gradle(first_arg: &str, args: &Vec<String>) {
fn do_with_buildin_arg_config(_first_arg: &str, args: &Vec<String>) {
if args.len() <= 2 {
print_message(MessageType::ERROR, "No arguments, show or set.");
print_message(MessageType::ERROR, "No arguments, get or set.");
return;
}
match args[2].as_str() {
"show" => match get_tool_package_secret() {
Err(_) => print_message(MessageType::WARN, "No config found."),
Ok(secret) => print_message(MessageType::OK, &format!("Config secret: {}", secret)),
"get" => match get_tool_package_secret() {
Err(_) => print_message(MessageType::WARN, "No config found."),
Ok(secret) => print_message(MessageType::OK, &format!("Config secret: {}", secret)),
},
"set" => {
if args.len() < 4 {
print_message(MessageType::ERROR, "Need secret for set, :::set <secret>");
} else {
match set_tool_package_secret(&args[3]) {
Err(err) => print_message(MessageType::ERROR, &format!("Config secret failed: {}", err)),
Ok(_) => print_message(MessageType::OK, "Config secret success."),
}
}
},
// TODO set...
arg => print_message(MessageType::ERROR, &format!("Unknown argument: {}", arg))
}
}

View File

@@ -155,6 +155,40 @@ pub fn get_tool_package_secret() -> XResult<String> {
Ok(build_js_auth_token.to_string())
}
pub fn set_tool_package_secret(secret: &str) -> XResult<()> {
let standard_config_file = get_user_home_dir(STANDARD_CONFIG_JSON)?;
match fs::metadata(&standard_config_file) {
Err(_) => {
match fs::write(&standard_config_file, json::stringify_pretty(
object!{ "build.js" => object!{
"auth_token" => secret, }
}, 4)) {
Ok(_) => Ok(()),
Err(err) => Err(new_box_error(&format!("Write config failed: {}, error message: {}", standard_config_file, err))),
}
},
Ok(f) => {
if ! f.is_file() {
return Err(new_box_error(&format!("Config is not a file: {}", standard_config_file)));
}
let standard_config_json = fs::read_to_string(&standard_config_file)?;
let mut standard_config_object = json::parse(&standard_config_json)?;
if standard_config_object["build.js"].is_null() {
standard_config_object["build.js"] = object! {
"auth_token" => secret,
};
} else {
standard_config_object["build.js"]["auth_token"] = secret.into();
}
match fs::write(&standard_config_file, json::stringify_pretty(standard_config_object, 4)) {
Ok(_) => Ok(()),
Err(err) => Err(new_box_error(&format!("Write config failed: {}, error message: {}", &standard_config_file, err))),
}
}
}
}
pub fn get_tool_package_detail(name: &str, version: &str) -> XResult<String> {
let secret = match *NOAUTH {
true => {