Compare commits
No commits in common. "8c3a6f63aa66d0d18dcbddb4baae80859a0ff847" and "c46b407569b0ac7bf1d4bafaf710f676f46c651b" have entirely different histories.
8c3a6f63aa
...
c46b407569
4 changed files with 79 additions and 71 deletions
|
@ -1,12 +1,11 @@
|
|||
[package]
|
||||
name = "config"
|
||||
version = "0.4.0"
|
||||
version = "0.3.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
pico-args = "0.5.0"
|
||||
toml = "0.5.9"
|
||||
|
||||
[profile.release]
|
||||
|
|
|
@ -3,8 +3,3 @@
|
|||
|
||||
`config` is a command line configuration manager written in Rust.
|
||||
|
||||
## Libraries
|
||||
|
||||
- [pico-args](https://crates.io/crates/pico-args) — argument parsing
|
||||
- [toml](https://crates.io/crates/toml) — TOML file parsing
|
||||
|
||||
|
|
|
@ -9,17 +9,17 @@ pub fn no_args() {
|
|||
}
|
||||
|
||||
pub fn missing_config(path: &PathBuf) {
|
||||
println!("config: no config file found at {path:?}");
|
||||
println!("config: no config file found at {:?}", path);
|
||||
exit(2);
|
||||
}
|
||||
|
||||
pub fn no_section(section: &String) {
|
||||
println!("config: nothing specified for \"{section}\"");
|
||||
println!("config: nothing specified for \"{}\"", section);
|
||||
exit(3);
|
||||
}
|
||||
|
||||
pub fn no_property(section: &String, property: &str) {
|
||||
println!("config: \"{section}\" has no property \"{property}\"");
|
||||
println!("config: \"{}\" has no property \"{}\"", section, property);
|
||||
exit(4);
|
||||
}
|
||||
|
||||
|
|
136
src/main.rs
136
src/main.rs
|
@ -1,5 +1,5 @@
|
|||
use std::{
|
||||
env::{ current_dir, var },
|
||||
env::{ args, current_dir, var },
|
||||
error::Error,
|
||||
fs::read_to_string,
|
||||
path::PathBuf,
|
||||
|
@ -9,7 +9,6 @@ use std::{
|
|||
}
|
||||
};
|
||||
|
||||
use pico_args::Arguments;
|
||||
use toml::{
|
||||
Value,
|
||||
value::Map
|
||||
|
@ -18,62 +17,84 @@ use toml::{
|
|||
mod error;
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>>{
|
||||
// get args
|
||||
let mut args = Arguments::from_env();
|
||||
|
||||
// handle help flag
|
||||
if args.contains(["-h", "--help"]) {
|
||||
help_text();
|
||||
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); }
|
||||
if !config_path.exists() {
|
||||
error::missing_config(&config_path);
|
||||
}
|
||||
|
||||
// build config table from toml
|
||||
let raw_conf = read_to_string(config_path)?;
|
||||
let toml_conf: Value = toml::from_str(raw_conf.as_str())?;
|
||||
let config = toml_conf.as_table().unwrap();
|
||||
|
||||
// get $EDITOR env
|
||||
let i_editor = var("EDITOR");
|
||||
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 };
|
||||
|
||||
// get argument target
|
||||
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();
|
||||
let args: Vec<String> = args().skip(1).collect();
|
||||
let mut dir = false;
|
||||
let mut list = false;
|
||||
let mut path = false;
|
||||
|
||||
target = args.subcommand().unwrap();
|
||||
if args.len() == 0 {
|
||||
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(());
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
// handle list flag
|
||||
if args.contains(["-l", "--list"]) {
|
||||
for (key, _) in section.iter() {
|
||||
println!("{key}");
|
||||
let start_index = if dir || list || path { 1 } else { 0 };
|
||||
|
||||
if list {
|
||||
let mut section = config;
|
||||
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(());
|
||||
}
|
||||
|
||||
if target.is_none() { error::no_args(); }
|
||||
let mut section: &Map<String, Value> = config;
|
||||
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");
|
||||
// handle dir flag
|
||||
if args.contains(["-d", "--dir"]) {
|
||||
if dir {
|
||||
if prop_path.is_none() {
|
||||
error::no_property(&fullname, "path");
|
||||
}
|
||||
|
@ -81,24 +102,25 @@ 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 file_path = PathBuf::from(expanded.to_string());
|
||||
if file_path.is_dir() {
|
||||
println!("{expanded}");
|
||||
println!("{}", expanded);
|
||||
return Ok(());
|
||||
} else {
|
||||
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() };
|
||||
println!("{parent}");
|
||||
println!("{}", parent);
|
||||
return Ok(());
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
// handle path flag
|
||||
if args.contains(["-p", "--path"]) {
|
||||
if prop_path.is_none() { error::no_property(&fullname, "path"); }
|
||||
if path {
|
||||
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() };
|
||||
println!("{file_path}");
|
||||
println!("{}", file_path);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// get properties from config section
|
||||
let prop_command = section.get("command");
|
||||
let raw_command: String;
|
||||
if prop_command.is_none() {
|
||||
|
@ -121,7 +143,6 @@ fn main() -> Result<(), Box<dyn Error>>{
|
|||
shell = shell_default;
|
||||
}
|
||||
|
||||
// build command and start process
|
||||
let mut process = Command::new(command);
|
||||
process.current_dir(current_dir()?);
|
||||
let mut arguments: Vec<&str> = parts.collect();
|
||||
|
@ -150,21 +171,14 @@ fn main() -> Result<(), Box<dyn Error>>{
|
|||
}
|
||||
|
||||
fn help_text() {
|
||||
println!(
|
||||
"config v{}
|
||||
Valerie Wolfe <sleeplessval@gmail.com>
|
||||
A configuration manager written in Rust.
|
||||
|
||||
usage: config [flags] <name>
|
||||
|
||||
args:
|
||||
<name> The name of the section to configure.
|
||||
|
||||
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")
|
||||
);
|
||||
println!("config v{}", env!("CARGO_PKG_VERSION"));
|
||||
println!("Valerie Wolfe <sleeplessval@gmail.com>");
|
||||
println!("A configuration manager written in Rust.\n");
|
||||
println!("USAGE:");
|
||||
println!("\tconfig NAME\n");
|
||||
println!("FLAGS:");
|
||||
println!("\t-h, --help\tPrints this help text");
|
||||
println!("\t-d, --dir\tGets the directory of the config file");
|
||||
println!("\t-l, --list\tLists config sections");
|
||||
println!("\t-p, --path\tGets the path to the config file");
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue