Compare commits
No commits in common. "main" and "cleanup" have entirely different histories.
2 changed files with 14 additions and 27 deletions
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "config"
|
name = "config"
|
||||||
version = "0.4.2"
|
version = "0.4.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
33
src/main.rs
33
src/main.rs
|
@ -2,7 +2,6 @@ use std::{
|
||||||
env::{ current_dir, var },
|
env::{ current_dir, var },
|
||||||
error::Error,
|
error::Error,
|
||||||
fs::read_to_string,
|
fs::read_to_string,
|
||||||
io::{ stdout, IsTerminal },
|
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
process::{
|
process::{
|
||||||
Command,
|
Command,
|
||||||
|
@ -22,14 +21,8 @@ fn main() -> Result<(), Box<dyn Error>>{
|
||||||
// get args
|
// get args
|
||||||
let mut args = Arguments::from_env();
|
let mut args = Arguments::from_env();
|
||||||
|
|
||||||
// collect args
|
|
||||||
let flag_help = args.contains(["-h", "--help"]);
|
|
||||||
let flag_list = args.contains(["-l", "--list"]);
|
|
||||||
let flag_dir = args.contains(["-d", "--dir"]);
|
|
||||||
let flag_path = args.contains(["-p", "--path"]);
|
|
||||||
|
|
||||||
// handle help flag
|
// handle help flag
|
||||||
if flag_help {
|
if args.contains(["-h", "--help"]) {
|
||||||
help_text();
|
help_text();
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
@ -57,23 +50,19 @@ fn main() -> Result<(), Box<dyn Error>>{
|
||||||
let mut section: &Map<String, Value> = config;
|
let mut section: &Map<String, Value> = config;
|
||||||
let mut target = args.subcommand().unwrap();
|
let mut target = args.subcommand().unwrap();
|
||||||
let mut fullname = String::new();
|
let mut fullname = String::new();
|
||||||
if target.is_some() {
|
while target.is_some() {
|
||||||
loop {
|
let entry = target.unwrap().clone();
|
||||||
let entry = target.as_ref().unwrap();
|
|
||||||
if !fullname.is_empty() { fullname += "."; }
|
if !fullname.is_empty() { fullname += "."; }
|
||||||
fullname += entry;
|
fullname += &entry;
|
||||||
let i_section = section.get(entry);
|
let i_section = section.get(&entry);
|
||||||
if i_section.is_none() { error::no_section(&entry); }
|
if i_section.is_none() { error::no_section(&entry); }
|
||||||
section = i_section.unwrap().as_table().unwrap();
|
section = i_section.unwrap().as_table().unwrap();
|
||||||
|
|
||||||
let i_target = args.subcommand().unwrap();
|
target = args.subcommand().unwrap();
|
||||||
if i_target.is_none() { break; }
|
|
||||||
target = i_target;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle list flag
|
// handle list flag
|
||||||
if flag_list {
|
if args.contains(["-l", "--list"]) {
|
||||||
for (key, _) in section.iter() {
|
for (key, _) in section.iter() {
|
||||||
println!("{key}");
|
println!("{key}");
|
||||||
}
|
}
|
||||||
|
@ -82,11 +71,9 @@ fn main() -> Result<(), Box<dyn Error>>{
|
||||||
|
|
||||||
if target.is_none() { error::no_args(); }
|
if target.is_none() { error::no_args(); }
|
||||||
|
|
||||||
let is_terminal = stdout().is_terminal();
|
|
||||||
|
|
||||||
let prop_path = section.get("path");
|
let prop_path = section.get("path");
|
||||||
// handle dir flag
|
// handle dir flag
|
||||||
if flag_dir {
|
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");
|
||||||
}
|
}
|
||||||
|
@ -103,7 +90,7 @@ fn main() -> Result<(), Box<dyn Error>>{
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
// handle path flag
|
// handle path flag
|
||||||
if flag_path || !is_terminal {
|
if args.contains(["-p", "--path"]) {
|
||||||
if prop_path.is_none() { 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() };
|
||||||
|
@ -168,7 +155,7 @@ fn help_text() {
|
||||||
Valerie Wolfe <sleeplessval@gmail.com>
|
Valerie Wolfe <sleeplessval@gmail.com>
|
||||||
A configuration manager written in Rust.
|
A configuration manager written in Rust.
|
||||||
|
|
||||||
usage: config <name> [flags]
|
usage: config [flags] <name>
|
||||||
|
|
||||||
args:
|
args:
|
||||||
<name> The name of the section to configure.
|
<name> The name of the section to configure.
|
||||||
|
|
Loading…
Reference in a new issue