#[macro_use]
extern crate log;
use std::{fs, process, io};
use std::path::{Path, PathBuf};
use std::io::{Write, Read};
use std::fs::{File, OpenOptions};
pub fn mkdir(path: &str) -> bool {
if !path_exists(path) {
match fs::create_dir_all(path) {
Ok(_) => {
info!("Created {}", path);
true
}
Err(e) => {
error!("Error creating file: {}", e);
false
}
}
} else {
false
}
}
pub fn rm(path: &str) -> bool {
let new_path = Path::new(path);
if new_path.exists() {
match fs::remove_file(path) {
Ok(_) => {
info!("Removed file {}", path);
true
},
Err(e) => {
error!("Error removing {} {}", path, e);
false
}
}
} else {
false
}
}
pub fn rmdir(path: &str) -> bool {
let new_path = Path::new(path);
if new_path.exists() {
match fs::remove_dir(path) {
Ok(_) => {
info!("Removed directory at {}", path);
true
},
Err(e) => {
error!("The directory {} is not empty. {}", path, e);
false
}
}
} else {
error!("Directory {} does not exist", path);
true
}
}
pub fn rm_r(path: &str) -> bool {
let new_path = Path::new(path);
if new_path.exists() {
match fs::remove_dir_all(path) {
Ok(_) => {
info!("Removed directory at {}", path);
true
},
Err(e) => {
error!("The directory {} is not empty. {}", path, e);
false
}
}
} else {
error!("Directory does not exist");
true
}
}
pub fn path_exists(path: &str) -> bool {
let new_path = Path::new(path);
if new_path.exists() {
info!("{} exists", path);
true
} else {
info!("{} does not exist", path);
false
}
}
pub fn directory_is_empty(path: &str) -> bool {
let new_path = Path::new(path);
if new_path.exists() {
if new_path.is_dir() {
let mut i = 0;
for entry in fs::read_dir(path) {
for _ in entry {
i += 1;
}
}
if i == 0 {
true
} else {
false
}
} else {
error!("The path {} passed is not a directory", path);
false
}
} else {
error!("The path {} passed does not exist.", path);
false
}
}
pub fn mv(path_one: &str, path_two: &str) -> bool {
let p1 = Path::new(path_one);
if p1.exists() {
match fs::rename(path_one, path_two) {
Ok(_) => {
info!("Moved from {} to {}.", path_one, path_two);
true
},
Err(e) => {
error!("File moving error: {}", e);
false
}
}
} else {
false
}
}
pub fn create_file(path: &str) -> bool {
match fs::File::create(path) {
Ok(_f) => {
info!("Successfully wrote file to {}", path);
true
}
Err(e) => {
error!("{}", e);
false
}
}
}
pub fn create_file_bytes(path: &str, bytes_to_write: &[u8]) -> bool {
match fs::File::create(path) {
Ok(mut buffer) => {
match buffer.write_all(bytes_to_write) {
Ok(_) => {
info!("Wrote buffer to {}", path);
true
},
Err(e) => {
error!("{}", e);
false
}
}
}
Err(e) => {
error!("{}", e);
false
}
}
}
pub fn write_file(path: &str, contents: &str) -> bool {
match File::create(path) {
Ok(mut f) => {
f.write_all(contents.as_ref()).unwrap();
true
}
Err(e) => {
error!("Cannot write file to location '{}' {}", path, e);
false
}
}
}
pub fn write_file_append(path: &str, contents: &str) -> bool {
match OpenOptions::new()
.write(true)
.create(true)
.append(true)
.open(path) {
Ok(mut f) => {
f.write_all(contents.as_ref()).unwrap();
true
}
Err(e) => {
error!("Cannot write file {}", e);
false
}
}
}
pub fn read_file(path: &str) -> String {
let mut contents = String::new();
match File::open(path) {
Ok(mut f) => {
f.read_to_string(&mut contents).unwrap();
}
Err(e) => error!("Cannot read file {}", e)
}
contents
}
pub fn cd(cd_path: &Path) -> Option<()> {
match std::env::set_current_dir(&cd_path) {
Ok(_) => {
info!("Changed current dir to {}", cd_path.display());
Some(())
},
Err(e) => {
error!("Could not set current dir to {}: {}", cd_path.display(), e);
None
}
}
}
pub fn run_command(program: &str, args: Vec<&str>) -> Option<i32> {
match process::Command::new(program).args(args).status() {
Ok(s) => {
s.code()
}
Err(e) => {
error!("There was an error {}", e);
None
}
}
}
pub fn ls(path: &str) -> Option<Vec<PathBuf>> {
match fs::read_dir(path) {
Ok(p) => {
let results = p.map(|res| res.map(|e| e.path()))
.collect::<Result<Vec<_>, io::Error>>();
match results {
Ok(r) => {
println!("{:?}", r);
Some(r)
},
Err(e) => {
println!("{}", e);
None
}
}
}
Err(e) => {
println!("{:?}", e);
None
}
}
}