Compare commits

..

4 commits
v0.4.0 ... main

2 changed files with 27 additions and 14 deletions

View file

@ -1,6 +1,6 @@
[package]
name = "config"
version = "0.4.0"
version = "0.4.2"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View file

@ -2,6 +2,7 @@ use std::{
env::{ current_dir, var },
error::Error,
fs::read_to_string,
io::{ stdout, IsTerminal },
path::PathBuf,
process::{
Command,
@ -21,8 +22,14 @@ fn main() -> Result<(), Box<dyn Error>>{
// get args
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
if args.contains(["-h", "--help"]) {
if flag_help {
help_text();
return Ok(());
}
@ -50,19 +57,23 @@ fn main() -> Result<(), Box<dyn Error>>{
let mut section: &Map<String, Value> = config;
let mut target = args.subcommand().unwrap();
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 target.is_some() {
loop {
let entry = target.as_ref().unwrap();
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();
target = args.subcommand().unwrap();
let i_target = args.subcommand().unwrap();
if i_target.is_none() { break; }
target = i_target;
}
}
// handle list flag
if args.contains(["-l", "--list"]) {
if flag_list {
for (key, _) in section.iter() {
println!("{key}");
}
@ -71,9 +82,11 @@ fn main() -> Result<(), Box<dyn Error>>{
if target.is_none() { error::no_args(); }
let is_terminal = stdout().is_terminal();
let prop_path = section.get("path");
// handle dir flag
if args.contains(["-d", "--dir"]) {
if flag_dir {
if prop_path.is_none() {
error::no_property(&fullname, "path");
}
@ -90,7 +103,7 @@ fn main() -> Result<(), Box<dyn Error>>{
return Ok(());
}
// handle path flag
if args.contains(["-p", "--path"]) {
if flag_path || !is_terminal {
if prop_path.is_none() { error::no_property(&fullname, "path"); }
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() };
@ -155,7 +168,7 @@ fn help_text() {
Valerie Wolfe <sleeplessval@gmail.com>
A configuration manager written in Rust.
usage: config [flags] <name>
usage: config <name> [flags]
args:
<name> The name of the section to configure.