diff --git a/src/main.rs b/src/main.rs index 179c270..021b0cc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -72,15 +72,24 @@ fn do_with_buildin_arg_gradle(first_arg: &str, args: &Vec) { fn do_with_buildin_arg_config(_first_arg: &str, args: &Vec) { 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 "); + } 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)) } } diff --git a/src/tool.rs b/src/tool.rs index e8a2f92..08be7f9 100644 --- a/src/tool.rs +++ b/src/tool.rs @@ -155,6 +155,40 @@ pub fn get_tool_package_secret() -> XResult { 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 { let secret = match *NOAUTH { true => {