Compare commits

..

9 commits
v0.2.0 ... main

6 changed files with 91 additions and 146 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
Cargo.lock
/target

58
Cargo.lock generated
View file

@ -1,58 +0,0 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aho-corasick"
version = "0.7.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f"
dependencies = [
"memchr",
]
[[package]]
name = "config"
version = "0.2.0"
dependencies = [
"regex",
"toml",
]
[[package]]
name = "memchr"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]]
name = "regex"
version = "1.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d83f127d94bdbcda4c8cc2e50f6f84f4b611f69c902699ca385a39c3a75f9ff1"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.6.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49b3de9ec5dc0a3417da371aab17d729997c15010e7fd24ff707773a33bddb64"
[[package]]
name = "serde"
version = "1.0.137"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61ea8d54c77f8315140a05f4c7237403bf38b72704d031543aa1d16abbf517d1"
[[package]]
name = "toml"
version = "0.5.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d82e1a7758622a465f8cee077614c73484dac5b836c02ff6a40d5d1010324d7"
dependencies = [
"serde",
]

View file

@ -1,12 +1,12 @@
[package]
name = "config"
version = "0.2.0"
version = "0.4.2"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
regex = "1.5.6"
pico-args = "0.5.0"
toml = "0.5.9"
[profile.release]

View file

@ -1,5 +1,10 @@
# config
`config` is a configuration manager written in Rust.
`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

View file

@ -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: \"{}\" has no property \"{}\"", section, property);
println!("config: \"{section}\" has no property \"{property}\"");
exit(4);
}

View file

@ -1,7 +1,8 @@
use std::{
env::{ args, current_dir, var },
env::{ current_dir, var },
error::Error,
fs::read_to_string,
io::{ stdout, IsTerminal },
path::PathBuf,
process::{
Command,
@ -9,7 +10,7 @@ use std::{
}
};
use regex::Regex;
use pico_args::Arguments;
use toml::{
Value,
value::Map
@ -18,111 +19,99 @@ use toml::{
mod error;
fn main() -> Result<(), Box<dyn Error>>{
let home_dir = var("HOME").unwrap();
let home_regex = Regex::new("^~")?;
// get args
let mut args = Arguments::from_env();
let mut config_path = PathBuf::from(&home_dir);
config_path.push(".config/config.toml");
if !config_path.exists() {
error::missing_config(&config_path);
// 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 flag_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); }
// 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 };
let args: Vec<String> = args().skip(1).collect();
let mut dir = false;
let mut list = false;
let mut path = false;
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(());
}
_ => {}
}
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(());
}
// get argument target
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 mut target = args.subcommand().unwrap();
let mut fullname = String::new();
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(&fullname);
}
if i_section.is_none() { error::no_section(&entry); }
section = i_section.unwrap().as_table().unwrap();
let i_target = args.subcommand().unwrap();
if i_target.is_none() { break; }
target = i_target;
}
//let section = i_section.unwrap().as_table().unwrap();
}
// handle list flag
if flag_list {
for (key, _) in section.iter() {
println!("{key}");
}
return Ok(());
}
if target.is_none() { error::no_args(); }
let is_terminal = stdout().is_terminal();
let prop_path = section.get("path");
if dir {
// handle dir flag
if flag_dir {
if prop_path.is_none() {
error::no_property(&fullname, "path");
}
let raw_path = prop_path.unwrap().as_str().unwrap();
let expanded = home_regex.replace(raw_path, &home_dir);
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);
return Ok(());
println!("{expanded}");
} else {
let i_parent = file_path.parent().unwrap().to_str().unwrap();
let parent = home_regex.replace(i_parent, home_dir);
println!("{}", parent);
let parent = if i_parent.starts_with("~") { i_parent.replacen("~", &home_dir, 1) } else { i_parent.to_string() };
println!("{parent}");
}
return Ok(());
}
}
if path {
if prop_path.is_none() {
error::no_property(&fullname, "path");
}
// handle path flag
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 = home_regex.replace(i_file_path, home_dir);
println!("{}", file_path);
let file_path = if i_file_path.starts_with("~") { i_file_path.replace("~", &home_dir) } else { i_file_path.to_string() };
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() {
@ -133,7 +122,7 @@ fn main() -> Result<(), Box<dyn Error>>{
} else {
raw_command = prop_command.unwrap().as_str().unwrap().to_string();
}
let expansion = home_regex.replace(raw_command.as_str(), home_dir);
let expansion = if raw_command.starts_with("~") { raw_command.replace("~", &home_dir) } else { raw_command.to_string() };
let mut parts = expansion.split_whitespace();
let command = parts.next().unwrap();
@ -145,6 +134,7 @@ 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();
@ -173,14 +163,21 @@ fn main() -> Result<(), Box<dyn Error>>{
}
fn help_text() {
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");
println!(
"config v{}
Valerie Wolfe <sleeplessval@gmail.com>
A configuration manager written in Rust.
usage: config <name> [flags]
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")
);
}