started working on add flag

This commit is contained in:
Valerie Wolfe 2021-06-18 11:28:39 -04:00
parent 1f89acec63
commit 0eda793663
2 changed files with 80 additions and 14 deletions

View file

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

View file

@ -1,7 +1,7 @@
use std::{ use std::{
env::{args, current_dir}, env::{args, current_dir},
path::Path, path::Path,
process::{Command, Stdio} process::{Command, exit, Stdio}
}; };
mod config; mod config;
@ -11,7 +11,7 @@ fn main() {
// Prepare config file // Prepare config file
let i_dir = current_dir().unwrap(); let i_dir = current_dir().unwrap();
let dir = i_dir.as_path(); let dir = i_dir.as_path();
let config = Config::new(); let mut config = Config::new();
// Parse arguments and handle them. // Parse arguments and handle them.
let args: Vec<String> = args().collect(); let args: Vec<String> = args().collect();
@ -27,13 +27,31 @@ Valerie Wolfe <sleeplessval@gmail.com>
A Linux implementation of the \"open\" command on Mac OS written in Rust and easily configurable. A Linux implementation of the \"open\" command on Mac OS written in Rust and easily configurable.
USAGE: USAGE:
open [FLAGS] [FILE] open [FLAGS] [OPERAND]
FLAGS: FLAGS:
-h, --help Prints this help text -h, --help Prints this help text
-a, --add Add a handler for a operand type
"); ");
return; 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 { if file_operand {
error = Some("open: too many file operands.".to_string()); error = Some("open: too many file operands.".to_string());
@ -45,16 +63,15 @@ FLAGS:
} }
if error.is_some() { if error.is_some() {
println!("{}", error.unwrap()); println!("{}", error.unwrap());
return; exit(1);
} }
let default = ".".to_string(); let default = ".".to_string();
let arg_target = args.get(1); let arg_target = args.get(1);
let i_target = arg_target.unwrap_or(&default); let i_target = arg_target.unwrap_or(&default);
let target = Path::new(i_target); let target = Path::new(i_target);
if !target.exists() { if !target.exists() {
println!("\"{}\" does not exist.", i_target); println!("open: \"{}\" does not exist.", i_target);
return; exit(1);
} }
let i_ext = target.extension(); let i_ext = target.extension();
let i_filename: String; let i_filename: String;
@ -76,11 +93,11 @@ FLAGS:
let i_exe = config.get(ext, "command"); let i_exe = config.get(ext, "command");
if i_exe.is_none() { if i_exe.is_none() {
match ext { match ext {
"open" => {}, "open" => println!("open: no zero-operand command specified."),
"dir" => println!("No command specified for directories."), "dir" => println!("open: no command specified for directories."),
_ => println!("No command specified for \"{}\" files.", ext) _ => println!("open: no command specified for \"{}\" files.", ext)
} }
return; exit(1);
} }
let exe = i_exe.unwrap(); let exe = i_exe.unwrap();
let mut parts = exe.split(" "); let mut parts = exe.split(" ");
@ -105,3 +122,7 @@ FLAGS:
command.spawn().ok(); command.spawn().ok();
} }
} }
fn get_ext() {
}