From 0eda79366357ac3a153d6bf4a85e7697efc5b646 Mon Sep 17 00:00:00 2001 From: Valerie Date: Fri, 18 Jun 2021 11:28:39 -0400 Subject: [PATCH] started working on add flag --- src/config.rs | 51 ++++++++++++++++++++++++++++++++++++++++++++++++--- src/main.rs | 43 ++++++++++++++++++++++++++++++++----------- 2 files changed, 80 insertions(+), 14 deletions(-) diff --git a/src/config.rs b/src/config.rs index f4bacfe..d4505f6 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, + local_path: Option, + 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(i_dir_global.to_string()); + } else { + global_path = None + } + let local_path: Option; + if local.is_some() { + local_path = Some(dir_local.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) { + let mut path = self.local_path.as_ref(); + if path.is_some() { + self.local.as_ref().unwrap().write(path.unwrap().as_str()).ok(); + return; + } + path = self.global_path.as_ref(); + self.global.as_ref().unwrap().write(path.unwrap().as_str()).ok(); + } } diff --git a/src/main.rs b/src/main.rs index 4d12c30..b1e9cc3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ use std::{ env::{args, current_dir}, path::Path, - process::{Command, Stdio} + process::{Command, exit, Stdio} }; mod config; @@ -11,7 +11,7 @@ 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(); @@ -27,13 +27,31 @@ Valerie Wolfe A Linux implementation of the \"open\" command on Mac OS written in Rust and easily configurable. USAGE: - open [FLAGS] [FILE] + open [FLAGS] [OPERAND] FLAGS: -h, --help Prints this help text + -a, --add Add a handler for a operand type "); 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"; + config.add(ext, "command", exe.to_string()); + if shell { + config.add(ext, "shell", "true".to_string()); + } + config.write(); + return; + } _ => { if file_operand { error = Some("open: too many file operands.".to_string()); @@ -45,16 +63,15 @@ FLAGS: } if error.is_some() { println!("{}", error.unwrap()); - return; + exit(1); } - let default = ".".to_string(); let arg_target = args.get(1); let i_target = arg_target.unwrap_or(&default); let target = Path::new(i_target); if !target.exists() { - println!("\"{}\" does not exist.", i_target); - return; + println!("open: \"{}\" does not exist.", i_target); + exit(1); } let i_ext = target.extension(); let i_filename: String; @@ -76,11 +93,11 @@ FLAGS: let i_exe = config.get(ext, "command"); if i_exe.is_none() { match ext { - "open" => {}, - "dir" => println!("No command specified for directories."), - _ => println!("No command specified for \"{}\" files.", ext) + "open" => println!("open: no zero-operand command specified."), + "dir" => println!("open: no command specified for directories."), + _ => println!("open: no command specified for \"{}\" files.", ext) } - return; + exit(1); } let exe = i_exe.unwrap(); let mut parts = exe.split(" "); @@ -105,3 +122,7 @@ FLAGS: command.spawn().ok(); } } + +fn get_ext() { + +}