#[cfg(feature = "clean")]
use e_app::clean::{
empty_access_log, empty_activity_history, empty_netshare, empty_recycle_bin, empty_run_history,
};
#[cfg(feature = "drive")]
use e_app::drive::{
devcon, devcon_status, find_with_run, findnodes, pnputil_add_driver, pnputil_delete_driver,
pnputil_disable, pnputil_enable, pnputil_export_driver, pnputil_remove, pnputil_restart,
pnputil_scan, DriveStatusType, EDRIVE_NAME,
};
use e_app::input::Opts;
use e_app::input::OptsApi;
#[cfg(feature = "os")]
use e_app::os::{
active_os, check_os_active, clean_cache, clear_kms, deactivate_os, query_cache, register_kms,
ActiveLocalType,
};
use e_app::share::CmdRes;
use e_utils::fs::tree_folder;
use e_utils::Error;
use e_utils::Result;
fn main() -> Result<()> {
let opts = Opts::new(None as Option<Vec<String>>)?;
if opts.init {
init()?;
}
let res = api(opts)?;
println!("{}", res.to_str()?);
Ok(())
}
#[allow(unused)]
fn m_opts(res: CmdRes, opts: Opts) -> CmdRes {
let mut res = res;
res
}
fn api(opts: Opts) -> Result<CmdRes> {
#[allow(unused)]
let res = match opts.api {
OptsApi::None => return Err(Error::String("Api cannot empty".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" => m_opts(check_os_active(), opts),
"active" => {
if opts.command.len() > 1 {
m_opts(
active_os(
&opts.command[0],
ActiveLocalType::Temp(opts.command[1].clone()),
),
opts,
)
} else {
return Err(Error::String("Command Error must > 0 ".to_string()));
}
}
"deactive" => m_opts(deactivate_os(), opts),
"rkms" => {
if opts.command.len() > 0 {
m_opts(register_kms(&opts.command[0]), opts)
} else {
return Err(Error::String("Command Error must > 0 ".to_string()));
}
}
"ckms" => m_opts(clear_kms(), opts),
"clean_cache" => {
if opts.command.len() > 0 {
m_opts(
clean_cache(ActiveLocalType::Temp(opts.command[0].clone())),
opts,
)
} else {
return Err(Error::String("Command Error must > 0 ".to_string()));
}
}
"query_cache" => {
if opts.command.len() > 0 {
m_opts(
query_cache(ActiveLocalType::Temp(opts.command[0].clone())),
opts,
)
} 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" => m_opts(empty_recycle_bin(), opts),
"empty_access_log" => m_opts(empty_access_log(), opts),
"empty_netshare" => m_opts(empty_netshare("*"), opts),
"empty_activity_history" => m_opts(empty_activity_history(), opts),
"empty_run_history" => m_opts(empty_run_history(), opts),
_ => 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" => m_opts(pnputil_scan(), opts),
"add-file" => {
let mut res = e_app::share::default_cmd_res();
let args: Vec<&str> = opts.args.iter().map(|x| x.as_ref()).collect();
let mut outres = e_app::share::default_cmd_res();
for x in 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 = pnputil_add_driver(new_args);
outres.content += &format!("{:?};;",res.content);
}
outres.status = true;
outres
}
"add" => {
let mut res = e_app::share::default_cmd_res();
let args: Vec<&str> = opts.args.iter().map(|x| x.as_ref()).collect();
let x = pnputil_add_driver(args);
x
}
"delete" => {
let args_src: Vec<&str> = opts.args.iter().map(|x| x.as_ref()).collect();
let res = pnputil_delete_driver(args_src);
m_opts(res, opts)
}
"delete-find" => {
let mut res = e_app::share::default_cmd_res();
let commands = opts
.command
.iter()
.map(|x| x.as_ref())
.collect::<Vec<&str>>();
let devcon_node_list = 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 = pnputil_delete_driver(args);
let status = devcon_status(&node.id);
if status.status != 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(m_opts(res, opts));
}
}
if let Ok(x) = serde_json::to_string(&devcon_node_list) {
res.content = x;
res.status = true;
}
m_opts(res, opts)
}
"findnodes" => {
let mut res = e_app::share::default_cmd_res();
let commands = opts
.command
.iter()
.map(|x| x.as_ref())
.collect::<Vec<&str>>();
let devcon_node_list = findnodes(&opts, commands);
if let Ok(x) = serde_json::to_string(&devcon_node_list) {
res.content = x;
res.status = true;
}
m_opts(res, opts)
}
"restart" => {
let mut res = e_app::share::default_cmd_res();
let status_list = find_with_run(&opts, pnputil_restart);
for status in &status_list {
if status.status == DriveStatusType::Runing {
res.status = true;
break;
}
}
if let Ok(x) = serde_json::to_string(&status_list) {
res.content = x;
}
m_opts(res, opts)
}
"enable" => {
let mut res = e_app::share::default_cmd_res();
let args_src: Vec<&str> = opts.args.iter().map(|x| x.as_ref()).collect();
let status_list = find_with_run(&opts, pnputil_enable);
res.status = true;
for status in &status_list {
if status.status != DriveStatusType::Runing {
res.status = false;
break;
}
}
if let Ok(x) = serde_json::to_string(&status_list) {
res.content = x;
}
m_opts(res, opts)
}
"disable" => {
let mut res = e_app::share::default_cmd_res();
let args_src: Vec<&str> = opts.args.iter().map(|x| x.as_ref()).collect();
let status_list = find_with_run(&opts, pnputil_disable);
res.status = true;
for status in &status_list {
if status.status != DriveStatusType::Disabled {
res.status = false;
break;
}
}
if let Ok(x) = serde_json::to_string(&status_list) {
res.content = x;
}
m_opts(res, opts)
}
"remove" => {
let mut res = e_app::share::default_cmd_res();
let args_src: Vec<&str> = opts.args.iter().map(|x| x.as_ref()).collect();
let status_list = find_with_run(&opts, pnputil_remove);
res.status = true;
for status in &status_list {
if status.status != DriveStatusType::None {
res.status = false;
break;
}
}
if let Ok(x) = serde_json::to_string(&status_list) {
res.content = x;
}
m_opts(res, opts)
}
"export" => {
let mut res = e_app::share::default_cmd_res();
let args_src: Vec<&str> = opts.args.iter().map(|x| x.as_ref()).collect();
let x = pnputil_export_driver(args_src);
x
}
_ => {
let commands = opts
.command
.iter()
.map(|x| x.as_ref())
.collect::<Vec<&str>>();
m_opts(devcon(commands), opts)
}
}
}
}
};
#[allow(unreachable_code)]
Ok(res)
}
pub fn init() -> Result<()> {
#[cfg(target_os = "windows")]
{
#[cfg(feature = "drive")]
e_app::share::write_bytes_file(
e_app::drive::get_drive_path().join(EDRIVE_NAME),
e_app::drive::EDRIVE_EXE,
)?;
}
Ok(())
}