Compare commits
No commits in common. "main" and "v0.2.0" have entirely different histories.
6 changed files with 144 additions and 89 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1 @@
|
|||
Cargo.lock
|
||||
/target
|
||||
|
|
58
Cargo.lock
generated
Normal file
58
Cargo.lock
generated
Normal file
|
@ -0,0 +1,58 @@
|
|||
# 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",
|
||||
]
|
|
@ -1,12 +1,12 @@
|
|||
[package]
|
||||
name = "config"
|
||||
version = "0.4.2"
|
||||
version = "0.2.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"
|
||||
regex = "1.5.6"
|
||||
toml = "0.5.9"
|
||||
|
||||
[profile.release]
|
||||
|
|
|
@ -1,10 +1,5 @@
|
|||
|
||||
# config
|
||||
|
||||
`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
|
||||
`config` is a configuration manager written in Rust.
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
157
src/main.rs
157
src/main.rs
|
@ -1,8 +1,7 @@
|
|||
use std::{
|
||||
env::{ current_dir, var },
|
||||
env::{ args, current_dir, var },
|
||||
error::Error,
|
||||
fs::read_to_string,
|
||||
io::{ stdout, IsTerminal },
|
||||
path::PathBuf,
|
||||
process::{
|
||||
Command,
|
||||
|
@ -10,7 +9,7 @@ use std::{
|
|||
}
|
||||
};
|
||||
|
||||
use pico_args::Arguments;
|
||||
use regex::Regex;
|
||||
use toml::{
|
||||
Value,
|
||||
value::Map
|
||||
|
@ -19,99 +18,111 @@ use toml::{
|
|||
mod error;
|
||||
|
||||
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 flag_help {
|
||||
help_text();
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// get $HOME from env
|
||||
let home_dir = var("HOME").unwrap();
|
||||
let home_regex = Regex::new("^~")?;
|
||||
|
||||
// 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();
|
||||
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();
|
||||
let args: Vec<String> = args().skip(1).collect();
|
||||
let mut dir = false;
|
||||
let mut list = false;
|
||||
let mut path = false;
|
||||
|
||||
let i_target = args.subcommand().unwrap();
|
||||
if i_target.is_none() { break; }
|
||||
target = i_target;
|
||||
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 flag_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 is_terminal = stdout().is_terminal();
|
||||
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 flag_dir {
|
||||
if dir {
|
||||
if prop_path.is_none() {
|
||||
error::no_property(&fullname, "path");
|
||||
}
|
||||
let raw_path = prop_path.unwrap().as_str().unwrap();
|
||||
let expanded = if raw_path.starts_with("~") { raw_path.replacen("~", &home_dir, 1) } else { raw_path.to_string() };
|
||||
let expanded = home_regex.replace(raw_path, &home_dir);
|
||||
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}");
|
||||
let parent = home_regex.replace(i_parent, home_dir);
|
||||
println!("{}", parent);
|
||||
return Ok(());
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
// handle path flag
|
||||
if flag_path || !is_terminal {
|
||||
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}");
|
||||
let file_path = home_regex.replace(i_file_path, home_dir);
|
||||
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() {
|
||||
|
@ -122,7 +133,7 @@ fn main() -> Result<(), Box<dyn Error>>{
|
|||
} else {
|
||||
raw_command = prop_command.unwrap().as_str().unwrap().to_string();
|
||||
}
|
||||
let expansion = if raw_command.starts_with("~") { raw_command.replace("~", &home_dir) } else { raw_command.to_string() };
|
||||
let expansion = home_regex.replace(raw_command.as_str(), home_dir);
|
||||
let mut parts = expansion.split_whitespace();
|
||||
let command = parts.next().unwrap();
|
||||
|
||||
|
@ -134,7 +145,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();
|
||||
|
@ -163,21 +173,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 <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")
|
||||
);
|
||||
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