use crate::share::CmdRes;
use super::{Opts, OptsApi};
use e_utils::{Error, Result};
pub fn api(opts: Opts) -> Result<CmdRes> {
#[allow(unused)]
let res = match opts.api {
OptsApi::None => return Err(Error::String("Api cannot eq None".to_string())),
OptsApi::Os => {
#[cfg(not(feature = "os"))]
return Err(Error::String("Feature not support os".to_string()));
#[cfg(feature = "os")]
{
match &*opts.task {
"check" => crate::os::check_os_active(),
"active" => {
if opts.command.len() > 1 {
crate::os::active_os(
&opts.command[0],
crate::os::ActiveLocalType::Temp(opts.command[1].clone()),
)
} else {
return Err(Error::String("Command Error must > 0 ".to_string()));
}
}
"deactive" => crate::os::deactivate_os(),
"rkms" => {
if opts.command.len() > 0 {
crate::os::register_kms(&opts.command[0])
} else {
return Err(Error::String("Command Error must > 0 ".to_string()));
}
}
"ckms" => crate::os::clear_kms(),
"clean_cache" => {
if opts.command.len() > 0 {
crate::os::clean_cache(crate::os::ActiveLocalType::Temp(opts.command[0].clone()))
} else {
return Err(Error::String("Command Error must > 0 ".to_string()));
}
}
"query_cache" => {
if opts.command.len() > 0 {
crate::os::query_cache(crate::os::ActiveLocalType::Temp(opts.command[0].clone()))
} else {
return Err(Error::String("Command Error must > 0 ".to_string()));
}
}
_ => return Err(Error::String("Task Error".to_string())),
}
}
}
OptsApi::Office => match &*opts.task {
_ => return Err(Error::String("Task Error".to_string())),
},
OptsApi::Clean => {
#[cfg(not(feature = "clean"))]
return Err(Error::String("Feature not support Clean".to_string()));
#[cfg(feature = "clean")]
{
match &*opts.task {
"empty_recycle_bin" => crate::clean::empty_recycle_bin(),
"empty_access_log" => crate::clean::empty_access_log(),
"empty_netshare" => crate::clean::empty_netshare("*"),
"empty_activity_history" => crate::clean::empty_activity_history(),
"empty_run_history" => crate::clean::empty_run_history(),
_ => return Err(Error::String("Task Error".to_string())),
}
}
}
OptsApi::Drive => {
#[cfg(not(feature = "drive"))]
return Err(Error::String("Feature not support Drive".to_string()));
#[cfg(feature = "drive")]
{
match &*opts.task {
"scan" => crate::drive::pnputil_scan(),
"add-file" => {
let mut res = crate::share::default_cmd_res();
let args: Vec<&str> = opts.args.iter().map(|x| x.as_ref()).collect();
let mut outres = crate::share::default_cmd_res();
for x in e_utils::fs::tree_folder(&args[0])?
.into_iter()
.filter(|x| std::path::Path::new(x).extension().unwrap_or_default() == "inf")
{
let mut new_args = args.clone();
if let Some(v) = new_args.get_mut(0) {
*v = &x;
}
let res = crate::drive::pnputil_add_driver(new_args);
outres.content += &format!("{:?};;", res.content);
}
outres.status = true;
outres
}
"add" => {
let mut res = crate::share::default_cmd_res();
let args: Vec<&str> = opts.args.iter().map(|x| x.as_ref()).collect();
let x = crate::drive::pnputil_add_driver(args);
x
}
"delete" => {
let args_src: Vec<&str> = opts.args.iter().map(|x| x.as_ref()).collect();
crate::drive::pnputil_delete_driver(args_src)
}
"delete-find" => {
let mut res = crate::share::default_cmd_res();
let commands = opts
.command
.iter()
.map(|x| x.as_ref())
.collect::<Vec<&str>>();
let devcon_node_list = crate::drive::findnodes(&opts, commands);
let args_src: Vec<&str> = opts.args.iter().map(|x| x.as_ref()).collect();
for node in &devcon_node_list {
let inf_path: std::path::PathBuf = (&node.inf_file).into();
if let Some(fname) = inf_path.file_name().and_then(|x| x.to_str()) {
let mut args = args_src.clone();
args.insert(0, fname);
let dres = crate::drive::pnputil_delete_driver(args);
let status = crate::drive::devcon_status(&node.id);
if status.status != crate::drive::DriveStatusType::None {
res.status = false;
}
} else {
res.status = false;
}
if !res.status {
res.content = serde_json::to_string(&vec![node]).unwrap_or_default();
return Ok(res);
}
}
if let Ok(x) = serde_json::to_string(&devcon_node_list) {
res.content = x;
res.status = true;
}
res
}
"findnodes" => {
let mut res = crate::share::default_cmd_res();
let commands = opts
.command
.iter()
.map(|x| x.as_ref())
.collect::<Vec<&str>>();
let devcon_node_list = crate::drive::findnodes(&opts, commands);
if let Ok(x) = serde_json::to_string(&devcon_node_list) {
res.content = x;
res.status = true;
}
res
}
"restart" => {
let mut res = crate::share::default_cmd_res();
let status_list = crate::drive::find_with_run(&opts, crate::drive::pnputil_restart);
for status in &status_list {
if status.status == crate::drive::DriveStatusType::Runing {
res.status = true;
break;
}
}
if let Ok(x) = serde_json::to_string(&status_list) {
res.content = x;
}
res
}
"enable" => {
let mut res = crate::share::default_cmd_res();
let args_src: Vec<&str> = opts.args.iter().map(|x| x.as_ref()).collect();
let status_list = crate::drive::find_with_run(&opts, crate::drive::pnputil_enable);
res.status = true;
for status in &status_list {
if status.status != crate::drive::DriveStatusType::Runing {
res.status = false;
break;
}
}
if let Ok(x) = serde_json::to_string(&status_list) {
res.content = x;
}
res
}
"disable" => {
let mut res = crate::share::default_cmd_res();
let args_src: Vec<&str> = opts.args.iter().map(|x| x.as_ref()).collect();
let status_list = crate::drive::find_with_run(&opts, crate::drive::pnputil_disable);
res.status = true;
for status in &status_list {
if status.status != crate::drive::DriveStatusType::Disabled {
res.status = false;
break;
}
}
if let Ok(x) = serde_json::to_string(&status_list) {
res.content = x;
}
res
}
"remove" => {
let mut res = crate::share::default_cmd_res();
let args_src: Vec<&str> = opts.args.iter().map(|x| x.as_ref()).collect();
let status_list = crate::drive::find_with_run(&opts, crate::drive::pnputil_remove);
res.status = true;
for status in &status_list {
if status.status != crate::drive::DriveStatusType::None {
res.status = false;
break;
}
}
if let Ok(x) = serde_json::to_string(&status_list) {
res.content = x;
}
res
}
"export" => {
let mut res = crate::share::default_cmd_res();
let args_src: Vec<&str> = opts.args.iter().map(|x| x.as_ref()).collect();
let x = crate::drive::pnputil_export_driver(args_src);
x
}
_ => {
let commands = opts
.command
.iter()
.map(|x| x.as_ref())
.collect::<Vec<&str>>();
crate::drive::devcon(commands)
}
}
}
}
};
#[allow(unreachable_code)]
Ok(res)
}
pub fn drive_init() -> Result<()> {
#[cfg(target_os = "windows")]
{
#[cfg(feature = "drive")]
crate::share::write_bytes_file(
crate::drive::get_drive_path().join(crate::drive::EDRIVE_NAME),
crate::drive::EDRIVE_EXE,
)?;
}
Ok(())
}