extern crate ini;
use ini::Ini;
use std::fs;
use std::process::Command;
use std::io::Write;
use std::fs::DirEntry;
use std::path::Path;
use std::io;
pub fn doshell_out(ml:&str) -> String{
let output = Command::new("sh").arg("-c").arg(ml.to_string()).output().expect("err");
let res = String::from_utf8(output.stdout).unwrap();
return res.to_string();
}
pub fn write_conf(fname:&str,fcontent:&str){
let mut file = std::fs::File::create(fname).expect("create failed");
file.write_all(fcontent.as_bytes()).expect("write failed");
}
pub fn read_conf(fname:&str,section:&str,key:&str)-> String{
let conf = Ini::load_from_file(fname).unwrap();
let section = conf.section(Some(section)).unwrap();
let res = section.get(key).unwrap();
return res.to_string();
}
pub fn visit_dirs(dir: &Path, cb: &dyn Fn(DirEntry,&mut Vec<DirEntry>),vec: &mut Vec<DirEntry>) -> io::Result<()> {
if dir.is_dir() {
for entry in fs::read_dir(dir)? {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
visit_dirs(&path, cb, vec)?;
} else {
cb(entry,vec);
}
}
}
Ok(())
}