diff --git a/Cargo.toml b/Cargo.toml index 2dd3ef6..71f9cc8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "open" -version = "0.2.0" +version = "0.3.1" authors = ["Valerie Wolfe "] edition = "2018" diff --git a/src/config.rs b/src/config.rs index f4bacfe..1070ec6 100644 --- a/src/config.rs +++ b/src/config.rs @@ -8,7 +8,9 @@ use configparser::ini::Ini; pub struct Config { pub local: Option, - pub global: Option + pub global: Option, + pub local_path: Option, + pub global_path: Option } impl Config { @@ -53,10 +55,24 @@ impl Config { panic!("No configuration found."); } - // Prepare loop condition + // prepare path vars + let global_path: Option; + if global.is_some() { + global_path = Some(path_global.to_str().unwrap().to_string()); + } else { + global_path = None + } + let local_path: Option; + if local.is_some() { + local_path = Some(dir_local.join(".open").to_str().unwrap().to_string()); + } else { + local_path = None; + } let output = Config { global, - local + local, + local_path, + global_path }; return output; } @@ -80,4 +96,33 @@ impl Config { } return output; } + pub fn add(&mut self, section: &str, key: &str, value: String) { + let mut ini: Ini; + let local = self.local.is_some(); + if local { + ini = self.local.clone().unwrap(); + } else { + ini = self.global.clone().unwrap(); + } + ini.set(section, key, Some(value)); + if local{ + self.local = Some(ini); + } else { + self.global = Some(ini); + } + } + pub fn add_global(&mut self, section: &str, key: &str, value: String) { + let mut global = self.global.clone().unwrap(); + global.set(section, key, Some(value)); + self.global = Some(global); + } + pub fn write(&self) -> std::io::Result<()> { + let mut path = self.local_path.as_ref(); + if path.is_some() { + let result = self.local.as_ref().unwrap().write(path.unwrap().as_str()); + return result; + } + path = self.global_path.as_ref(); + self.global.as_ref().unwrap().write(path.unwrap().as_str()) + } } diff --git a/src/main.rs b/src/main.rs index bd68778..db9914e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,10 +11,72 @@ fn main() { // Prepare config file let i_dir = current_dir().unwrap(); let dir = i_dir.as_path(); - let config = Config::new(); + let mut config = Config::new(); + // Parse arguments and handle them. let args: Vec = args().collect(); + let mut error: Option = None; + let mut file_operand = false; + for arg in &args[1..] { + match arg.as_str() { + "-h" | + "--help" => { + println!("open +Valerie Wolfe +A Linux implementation of the \"open\" command on Mac OS written in Rust and easily configurable. + +USAGE: + open [FLAGS] [OPERAND] + +FLAGS: + -h, --help Prints this help text + -a, --add Add a handler for a operand type + -p, --path Prints the config path used +"); + return; + }, + "-a" | + "--add" => { + if args.len() < 4 { + println!("open: too few arguments."); + exit(1); + } + let ext = args.get(2).unwrap(); + let exe = args.get(3).unwrap(); + let tmp = args.get(4); + let shell = tmp.is_some() && tmp.unwrap() == "shell"; + println!("{} {} {}", ext, exe, shell); + config.add(ext, "command", exe.to_string()); + if shell { + config.add(ext, "shell", "true".to_string()); + } + config.write().ok(); + return; + }, + "-p" | + "--path" => { + let local = config.local_path; + if local.is_some() { + println!("{}", local.unwrap()); + } else { + println!("{}", config.global_path.unwrap()); + } + return; + }, + _ => { + if file_operand { + error = Some("open: too many file operands.".to_string()); + } else { + file_operand = true; + } + } + } + } + if error.is_some() { + println!("{}", error.unwrap()); + exit(1); + } let default = ".".to_string(); let arg_target = args.get(1); let i_target = arg_target.unwrap_or(&default); @@ -43,7 +105,6 @@ fn main() { let i_exe = config.get(ext, "command"); if i_exe.is_none() { match ext { - "open" => println!("open: no zero-parameter command specified."), "dir" => println!("open: no command specified for directories."), _ => println!("open: no command specified for \"{}\" files.", ext) }