significant refactor and code reduction

This commit is contained in:
Valerie Wolfe 2023-07-11 09:43:52 -04:00
parent 91b9a842a6
commit 9f53d08f7e
2 changed files with 66 additions and 80 deletions

View file

@ -9,17 +9,17 @@ pub fn no_args() {
} }
pub fn missing_config(path: &PathBuf) { pub fn missing_config(path: &PathBuf) {
println!("config: no config file found at {:?}", path); println!("config: no config file found at {path:?}");
exit(2); exit(2);
} }
pub fn no_section(section: &String) { pub fn no_section(section: &String) {
println!("config: nothing specified for \"{}\"", section); println!("config: nothing specified for \"{section}\"");
exit(3); exit(3);
} }
pub fn no_property(section: &String, property: &str) { pub fn no_property(section: &String, property: &str) {
println!("config: \"{}\" has no property \"{}\"", section, property); println!("config: \"{section}\" has no property \"{property}\"");
exit(4); exit(4);
} }

View file

@ -1,5 +1,5 @@
use std::{ use std::{
env::{ args, current_dir, var }, env::{ current_dir, var },
error::Error, error::Error,
fs::read_to_string, fs::read_to_string,
path::PathBuf, path::PathBuf,
@ -9,6 +9,7 @@ use std::{
} }
}; };
use pico_args::Arguments;
use toml::{ use toml::{
Value, Value,
value::Map value::Map
@ -17,84 +18,62 @@ use toml::{
mod error; mod error;
fn main() -> Result<(), Box<dyn Error>>{ fn main() -> Result<(), Box<dyn Error>>{
let home_dir = var("HOME").unwrap(); // get args
let mut args = Arguments::from_env();
let mut config_path = PathBuf::from(&home_dir); // handle help flag
config_path.push(".config/config.toml"); if args.contains(["-h", "--help"]) {
if !config_path.exists() { help_text();
error::missing_config(&config_path); return Ok(());
} }
// get $HOME from env
let home_dir = var("HOME").unwrap();
// build + validate config path
let mut config_path = PathBuf::from(&home_dir);
config_path.push(".config/config.toml");
if !config_path.exists() { error::missing_config(&config_path); }
// build config table from toml
let raw_conf = read_to_string(config_path)?; let raw_conf = read_to_string(config_path)?;
let toml_conf: Value = toml::from_str(raw_conf.as_str())?; let toml_conf: Value = toml::from_str(raw_conf.as_str())?;
let config = toml_conf.as_table().unwrap(); let config = toml_conf.as_table().unwrap();
// get $EDITOR env
let i_editor = var("EDITOR"); let i_editor = var("EDITOR");
let editor: Option<String> = if i_editor.is_ok() { Some(i_editor.unwrap()) } else { None }; let editor: Option<String> = if i_editor.is_ok() { Some(i_editor.unwrap()) } else { None };
let shell_default = if config.contains_key("shell") { config.get("shell").unwrap().as_bool().unwrap() } else { false }; let shell_default = if config.contains_key("shell") { config.get("shell").unwrap().as_bool().unwrap() } else { false };
let args: Vec<String> = args().skip(1).collect(); // get argument target
let mut dir = false; let mut section: &Map<String, Value> = config;
let mut list = false; let mut target = args.subcommand().unwrap();
let mut path = false; let mut fullname = String::new();
while target.is_some() {
let entry = target.unwrap().clone();
if !fullname.is_empty() { fullname += "."; }
fullname += &entry;
let i_section = section.get(&entry);
if i_section.is_none() { error::no_section(&entry); }
section = i_section.unwrap().as_table().unwrap();
if args.len() == 0 { target = args.subcommand().unwrap();
error::no_args();
}
match args[0].as_str() {
"-d" |
"--dir" => dir = true,
"-l" |
"--list" => list = true,
"-p" |
"--path" => path = true,
"-h" |
"--help" => {
help_text();
return Ok(());
}
_ => {}
} }
let start_index = if dir || list || path { 1 } else { 0 }; // handle list flag
if args.contains(["-l", "--list"]) {
if list { for (key, _) in section.iter() {
let mut section = config; println!("{key}");
if args.len() > 1 {
let mut args = args.clone();
args.remove(0);
for arg in args {
let i_sub = section.get(arg.as_str());
if i_sub.is_none() { return Ok(()); }
let sub = i_sub.unwrap().as_table();
if sub.is_none() { return Ok(()); }
section = sub.unwrap();
}
}
for (key, value) in section.iter() {
if value.is_table() {
println!("{}", key);
}
} }
return Ok(()); return Ok(());
} }
let mut section: &Map<String, Value> = config; if target.is_none() { error::no_args(); }
let mut fullname: String = "".to_string();
for i in start_index..args.len() {
let entry = &args[i];
fullname = if fullname.is_empty() { args[i].clone() } else { fullname + "." + &args[i] };
let i_section = section.get(entry);
if i_section.is_none() {
error::no_section(&fullname);
}
section = i_section.unwrap().as_table().unwrap();
}
//let section = i_section.unwrap().as_table().unwrap();
let prop_path = section.get("path"); let prop_path = section.get("path");
if dir { // handle dir flag
if args.contains(["-d", "--dir"]) {
if prop_path.is_none() { if prop_path.is_none() {
error::no_property(&fullname, "path"); error::no_property(&fullname, "path");
} }
@ -102,25 +81,24 @@ fn main() -> Result<(), Box<dyn Error>>{
let expanded = if raw_path.starts_with("~") { raw_path.replacen("~", &home_dir, 1) } else { raw_path.to_string() }; let expanded = if raw_path.starts_with("~") { raw_path.replacen("~", &home_dir, 1) } else { raw_path.to_string() };
let file_path = PathBuf::from(expanded.to_string()); let file_path = PathBuf::from(expanded.to_string());
if file_path.is_dir() { if file_path.is_dir() {
println!("{}", expanded); println!("{expanded}");
return Ok(());
} else { } else {
let i_parent = file_path.parent().unwrap().to_str().unwrap(); let i_parent = file_path.parent().unwrap().to_str().unwrap();
let parent = if i_parent.starts_with("~") { i_parent.replacen("~", &home_dir, 1) } else { i_parent.to_string() }; let parent = if i_parent.starts_with("~") { i_parent.replacen("~", &home_dir, 1) } else { i_parent.to_string() };
println!("{}", parent); println!("{parent}");
return Ok(());
} }
return Ok(());
} }
if path { // handle path flag
if prop_path.is_none() { if args.contains(["-p", "--path"]) {
error::no_property(&fullname, "path"); if prop_path.is_none() { error::no_property(&fullname, "path"); }
}
let i_file_path = prop_path.unwrap().as_str().unwrap(); let i_file_path = prop_path.unwrap().as_str().unwrap();
let file_path = if i_file_path.starts_with("~") { i_file_path.replace("~", &home_dir) } else { i_file_path.to_string() }; let file_path = if i_file_path.starts_with("~") { i_file_path.replace("~", &home_dir) } else { i_file_path.to_string() };
println!("{}", file_path); println!("{file_path}");
return Ok(()); return Ok(());
} }
// get properties from config section
let prop_command = section.get("command"); let prop_command = section.get("command");
let raw_command: String; let raw_command: String;
if prop_command.is_none() { if prop_command.is_none() {
@ -143,6 +121,7 @@ fn main() -> Result<(), Box<dyn Error>>{
shell = shell_default; shell = shell_default;
} }
// build command and start process
let mut process = Command::new(command); let mut process = Command::new(command);
process.current_dir(current_dir()?); process.current_dir(current_dir()?);
let mut arguments: Vec<&str> = parts.collect(); let mut arguments: Vec<&str> = parts.collect();
@ -171,14 +150,21 @@ fn main() -> Result<(), Box<dyn Error>>{
} }
fn help_text() { fn help_text() {
println!("config v{}", env!("CARGO_PKG_VERSION")); println!(
println!("Valerie Wolfe <sleeplessval@gmail.com>"); "config v{}
println!("A configuration manager written in Rust.\n"); Valerie Wolfe <sleeplessval@gmail.com>
println!("USAGE:"); A configuration manager written in Rust.
println!("\tconfig NAME\n");
println!("FLAGS:"); usage: config [flags] <name>
println!("\t-h, --help\tPrints this help text");
println!("\t-d, --dir\tGets the directory of the config file"); args:
println!("\t-l, --list\tLists config sections"); <name> The name of the section to configure.
println!("\t-p, --path\tGets the path to the config file");
flags:
-h, --help Prints this help text
-d, --dir Gets the directory of the config file
-l, --list Lists config sections
-p, --path Gets the path to the config file",
env!("CARGO_PKG_VERSION")
);
} }