Merge pull request #1 from sleeplessval/flags

Merging flags branch
This commit is contained in:
Valerie 2021-07-05 15:18:22 -04:00 committed by GitHub
commit 206dc672ec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 112 additions and 6 deletions

View file

@ -1,6 +1,6 @@
[package]
name = "open"
version = "0.2.0"
version = "0.3.1"
authors = ["Valerie Wolfe <sleeplessval@gmail.com>"]
edition = "2018"

View file

@ -8,7 +8,9 @@ use configparser::ini::Ini;
pub struct Config {
pub local: Option<Ini>,
pub global: Option<Ini>
pub global: Option<Ini>,
pub local_path: Option<String>,
pub global_path: Option<String>
}
impl Config {
@ -53,10 +55,24 @@ impl Config {
panic!("No configuration found.");
}
// Prepare loop condition
// prepare path vars
let global_path: Option<String>;
if global.is_some() {
global_path = Some(path_global.to_str().unwrap().to_string());
} else {
global_path = None
}
let local_path: Option<String>;
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())
}
}

View file

@ -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<String> = args().collect();
let mut error: Option<String> = None;
let mut file_operand = false;
for arg in &args[1..] {
match arg.as_str() {
"-h" |
"--help" => {
println!("open
Valerie Wolfe <sleeplessval@gmail.com>
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)
}