From 65ca86a60c3311401e8a85e37249c0f5e1ca4008 Mon Sep 17 00:00:00 2001 From: Valerie Date: Thu, 17 Jun 2021 23:31:33 -0400 Subject: [PATCH 1/5] added the help flag and an error if too many arguments are passed --- Cargo.toml | 2 +- src/main.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 2dd3ef6..2baa15b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "open" -version = "0.2.0" +version = "0.3.0" authors = ["Valerie Wolfe "] edition = "2018" diff --git a/src/main.rs b/src/main.rs index b93ffc8..0eac478 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,8 +13,40 @@ fn main() { let dir = i_dir.as_path(); let config = Config::new(); + // Parse arguments and handle them. let args: Vec = args().collect(); + let mut error: Option = Some("open: missing file operand.".to_string()); + 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] [FILE] + +FLAGS: + -h, --help Prints this help text +"); + return; + }, + _ => { + if error.is_none() { + error = Some("open: too many arguments.".to_string()); + } else { + error = None; + } + } + } + } + if error.is_some() { + println!("{}", error.unwrap()); + return; + } + let default = ".".to_string(); let arg_target = args.get(1); let i_target = arg_target.unwrap_or(&default); From 1f89acec6316918983dddb480a5d9655ac640f34 Mon Sep 17 00:00:00 2001 From: Valerie Date: Fri, 18 Jun 2021 00:28:44 -0400 Subject: [PATCH 2/5] fixed an erroneous check to make sure a file operand is present because it isn't required --- src/main.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0eac478..4d12c30 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,8 @@ fn main() { // Parse arguments and handle them. let args: Vec = args().collect(); - let mut error: Option = Some("open: missing file operand.".to_string()); + let mut error: Option = None; + let mut file_operand = false; for arg in &args[1..] { match arg.as_str() { "-h" | @@ -34,10 +35,10 @@ FLAGS: return; }, _ => { - if error.is_none() { - error = Some("open: too many arguments.".to_string()); + if file_operand { + error = Some("open: too many file operands.".to_string()); } else { - error = None; + file_operand = true; } } } From 0eda79366357ac3a153d6bf4a85e7697efc5b646 Mon Sep 17 00:00:00 2001 From: Valerie Date: Fri, 18 Jun 2021 11:28:39 -0400 Subject: [PATCH 3/5] 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() { + +} From c99ef1a7b069bdc8126c9e31b2cb833f61de6cec Mon Sep 17 00:00:00 2001 From: Valerie Date: Wed, 23 Jun 2021 10:59:44 -0400 Subject: [PATCH 4/5] added -h/--help and -a/--add flags. --- src/config.rs | 12 ++++++------ src/main.rs | 7 ++----- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/config.rs b/src/config.rs index d4505f6..b004fb5 100644 --- a/src/config.rs +++ b/src/config.rs @@ -58,13 +58,13 @@ impl Config { // prepare path vars let global_path: Option; if global.is_some() { - global_path = Some(i_dir_global.to_string()); + 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.to_str().unwrap().to_string()); + local_path = Some(dir_local.join(".open").to_str().unwrap().to_string()); } else { local_path = None; } @@ -116,13 +116,13 @@ impl Config { global.set(section, key, Some(value)); self.global = Some(global); } - pub fn write(&self) { + pub fn write(&self) -> std::io::Result<()> { let mut path = self.local_path.as_ref(); if path.is_some() { - self.local.as_ref().unwrap().write(path.unwrap().as_str()).ok(); - return; + 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()).ok(); + self.global.as_ref().unwrap().write(path.unwrap().as_str()) } } diff --git a/src/main.rs b/src/main.rs index b1e9cc3..c31d7f4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -45,11 +45,12 @@ FLAGS: 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(); + config.write().ok(); return; } _ => { @@ -122,7 +123,3 @@ FLAGS: command.spawn().ok(); } } - -fn get_ext() { - -} From 945698353611dffb053370eeb251599d012f6d26 Mon Sep 17 00:00:00 2001 From: Valerie Date: Mon, 5 Jul 2021 13:34:37 -0400 Subject: [PATCH 5/5] added path flag --- Cargo.toml | 2 +- src/config.rs | 4 ++-- src/main.rs | 17 ++++++++++++++--- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2baa15b..71f9cc8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "open" -version = "0.3.0" +version = "0.3.1" authors = ["Valerie Wolfe "] edition = "2018" diff --git a/src/config.rs b/src/config.rs index b004fb5..1070ec6 100644 --- a/src/config.rs +++ b/src/config.rs @@ -9,8 +9,8 @@ use configparser::ini::Ini; pub struct Config { pub local: Option, pub global: Option, - local_path: Option, - global_path: Option + pub local_path: Option, + pub global_path: Option } impl Config { diff --git a/src/main.rs b/src/main.rs index c31d7f4..5df0c6f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,8 +30,9 @@ USAGE: open [FLAGS] [OPERAND] FLAGS: - -h, --help Prints this help text - -a, --add Add a handler for a operand type + -h, --help Prints this help text + -a, --add Add a handler for a operand type + -p, --path Prints the config path used "); return; }, @@ -52,7 +53,17 @@ FLAGS: } 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());