This commit is contained in:
Joshua Perry 2024-01-01 12:58:14 +00:00
commit cc72659637
10 changed files with 608 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

267
Cargo.lock generated Normal file
View File

@ -0,0 +1,267 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "anstream"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d664a92ecae85fd0a7392615844904654d1d5f5514837f471ddef4a057aba1b6"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87"
[[package]]
name = "anstyle-parse"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648"
dependencies = [
"windows-sys",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7"
dependencies = [
"anstyle",
"windows-sys",
]
[[package]]
name = "clap"
version = "4.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcfab8ba68f3668e89f6ff60f5b205cea56aa7b769451a59f34b8682f51c056d"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb7fb5e4e979aec3be7791562fcba452f94ad85e954da024396433e0e25a79e9"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf9804afaaf59a91e75b022a30fb7229a7901f60c755489cc61c9b423b836442"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1"
[[package]]
name = "colorchoice"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
[[package]]
name = "heck"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]]
name = "itoa"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c"
[[package]]
name = "proc-macro2"
version = "1.0.71"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75cb1540fadbd5b8fbccc4dddad2734eba435053f725621c070711a14bb5f4b8"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rusty-dotfiles"
version = "0.1.0"
dependencies = [
"clap",
"serde_json",
]
[[package]]
name = "ryu"
version = "1.0.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f98d2aa92eebf49b69786be48e4477826b256916e84a57ff2a4f21923b48eb4c"
[[package]]
name = "serde"
version = "1.0.193"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.193"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.108"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d1c7e3eac408d115102c4c24ad393e0821bb3a5df4d506a80f85f7a742a526b"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "syn"
version = "2.0.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee659fb5f3d355364e1f3e5bc10fb82068efbf824a1e9d1c9504244a6469ad53"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "utf8parse"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef"
[[package]]
name = "windows_i686_gnu"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313"
[[package]]
name = "windows_i686_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04"

15
Cargo.toml Normal file
View File

@ -0,0 +1,15 @@
[package]
name = "rusty-dotfiles"
version = "0.1.0"
edition = "2021"
publish = ["gitea"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
clap = { version = "4.4.12", features = ["derive"] }
serde_json = "1.0.108"
[[bin]]
name = "dotfiles-collect"
path = "src/bin/collect.rs"

59
config.json Normal file
View File

@ -0,0 +1,59 @@
[
{"boot": [
{"grub": [
{"themes":
["GradientGuy"]}
]}
]},
{"etc": [
"installList.txt",
"mkinitcpio.conf",
"pacman.conf",
{"default":
["grub"]},
{"pacman.d": ["hooks"]},
{"iwd":
["main.conf"]},
{"udev": [
{"rules.d": [
"60-thrustmaster.rules",
"80-yubikey.rules"
]}
]}
]},
{"usr": [
{"share": [
{"applications": [
"spotify.desktop",
"org.gnome.Evolution.desktop"
]},
{"icons": [
{"default": ["index.theme"]}
]}
]}
]},
{"home": [
{"$USER": [
".zshrc",
{".config": [
"btop",
"dunst",
"eww",
"hypr",
"kitty",
"qt5ct",
"rofi",
"spotify-tui",
"swaylock",
"zsh",
"gtk-3.0",
"cron",
{"systemd": [
{"user": ["spotifyd.service"]}
]},
"nvim",
{".vpn": ["templates"]}
]}
]}
]}
]

3
src/bin/collect.rs Normal file
View File

@ -0,0 +1,3 @@
fn main() {
rusty_dotfiles::collect::run();
}

37
src/collect.rs Normal file
View File

@ -0,0 +1,37 @@
pub fn run() {
use clap::Parser;
let args = super::Args::parse();
let mut config = super::Config::new(args);
config.run();
config.paths.iter().for_each(|path| {
let mut destination = config.destination_path.to_string();
destination.push_str(path.as_str());
match path.chars().last() {
Some('/') => create_dir(&config.destination_path),
Some(_) => copy_file(path.as_str(), &destination),
None => super::errors::empty_path_error(),
}
});
}
fn create_dir(destination: &str) {
use std::fs;
match fs::create_dir(&destination) {
Ok(_) => (),
Err(_) => super::errors::create_dir_error(&destination),
}
}
fn copy_file(path: &str, destination: &str) {
use std::fs;
match fs::copy(path, &destination) {
Ok(_) => (),
Err(_) => super::errors::copy_file_error(path),
}
}

13
src/errors.rs Normal file
View File

@ -0,0 +1,13 @@
pub fn json_parsing_error() -> ! {
eprintln!("Error parsing JSON String. Check to see if your syntax is valid.");
std::process::exit(1)
}
pub fn create_dir_error(dir: &str) {
eprintln!("There was an issue creating the directory {}. It may already exist.", dir);
}
pub fn copy_file_error(path: &str) {
eprintln!("There was an issue copying the path {}", path);
}
pub fn empty_path_error() {
eprintln!("A path seems to be empty");
}

3
src/header.rs Normal file
View File

@ -0,0 +1,3 @@
//TODO: mod init
// convert terminal header into rust

4
src/install.rs Normal file
View File

@ -0,0 +1,4 @@
//TODO: mod install
//TODO: clone dotfiles
//TODO: copy dotfiles to location in config

206
src/lib.rs Normal file
View File

@ -0,0 +1,206 @@
pub mod collect;
pub mod errors;
pub mod header;
pub mod install;
use clap::Parser;
/// Defines the arguments passed to the program.
///
/// # Examples
///
/// ```
/// use clap::Parser;
///
/// let args = Args::parse();
/// assert_eq!("config.json", args.config_file_path);
/// ```
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
pub struct Args {
/// The file path to the config file
#[arg(short, long, default_value = "config.json")]
pub config_file_path: String,
/// The path to the directory to do work in
#[arg(short, long, default_value = "~/dotfiles")]
pub destination_dir_path: String,
/// Whether to treat the user's home folder as their username or "$USER"
#[arg(short, long, default_value_t = false)]
pub use_username: bool,
}
/// The root of a Linux System
const ROOT: &str = "/";
/// Defines the Configuration for the progam
pub struct Config {
/// The Config as a JSON Object
pub config: serde_json::Value,
/// The paths currently stored for this configuration
pub paths: Vec<String>,
/// The root path of all work done
pub root: String,
/// Whether to treat the user's home folder as their username or "$USER"
pub use_username: bool,
/// The path to the directory to do work in
pub destination_path: String,
}
impl Config {
/// Returns a new instance of Config
///
/// # Arguments
///
/// * `config_file_path` - The file path to the config file
///
/// # Examples
///
/// ```
/// let config = Config::new("config.json");
/// assert_eq!(ROOT, config.root);
/// ```
pub fn new(args: Args) -> Self {
let root = "/";
let paths = vec![root.to_string()];
let config = parse_config(&args.config_file_path);
Self {
paths,
config,
root: root.to_string(),
use_username: args.use_username,
destination_path: args.destination_dir_path,
}
}
/// Initializes the Config instance
///
/// # Examples
///
/// ```
/// let config = Config::new("config.json");
/// config.run();
/// assert!(config.paths.len() > 1);
/// ```
pub fn run(&mut self) {
let mut config_root = self.root.clone();
let mut config_tree = self.config.clone();
self.generate_paths(&mut config_tree, &mut config_root)
}
/// Recursively generates file paths from a JSON Object
///
/// # Arguments
///
/// * `json_file_tree` - The file_tree of paths to generate as a JSON Object
/// * `current_root` - The current operating directory path
fn generate_paths(&mut self, json_file_tree: &serde_json::Value, current_root: &mut String) {
match json_file_tree {
serde_json::Value::Object(dir) => self.parse_dir(current_root.clone() ,dir),
serde_json::Value::Array(dir_list) => self.parse_list(dir_list, current_root),
serde_json::Value::String(file) => self.parse_file(file, current_root.clone()),
_ => (),
}
}
/// Parses a directory from a JSON Object
///
/// # Arguments
///
/// * `current_root` - The current operating directory path
/// * `dir` - The JSON Object to be parsed as a Map
fn parse_dir(&mut self, mut current_root: String, dir: &serde_json::Map<String, serde_json::Value>) {
let dirname = match dir.keys().next() {
Some(dirname) => convert_if_user_folder(dirname, self.use_username),
None => "".to_string(),
};
current_root.push_str(&dirname);
current_root.push_str(ROOT);
self.paths.push(current_root.to_string());
let json_file_tree = match dir.values().next() {
Some(value) => value,
None => errors::json_parsing_error(),
};
self.generate_paths(json_file_tree, &mut current_root)
}
/// Parses a vector of directories from JSON Objects
///
/// # Arguments
///
///* 'dir_list' - The vector to be parsed
/// * `current_root` - The current operating directory path
fn parse_list(&mut self, dir_list: &Vec<serde_json::Value>, current_root: &mut String) {
let mut current_root = current_root.clone();
dir_list.iter().for_each(|dir|
self.generate_paths(dir, &mut current_root)
);
}
/// Parses a file from a JSON String
///
/// # Arguments
///
/// * `file` - The string to be parsed
/// * `current_root` - The current operating directory path
fn parse_file(&mut self, file: &str, mut current_root: String) {
current_root.push_str(file);
self.paths.push(current_root.to_string())
}
}
/// Parses the JSON file at file_path and returns a serde_json::Value.
/// Exits program upon error.
///
/// # Arguments
///
/// * `file_path` - The file path of the config to be parsed
///
/// # Examples
///
/// ```
/// let config = parse_config("config.json");
/// println!("{:?}", config);
/// ```
pub fn parse_config(file_path: &str) -> serde_json::Value {
let json_string = read_file_from_path(file_path);
let value = match serde_json::from_str(&json_string) {
Ok(config) => config,
Err(_) => errors::json_parsing_error(),
};
value
}
/// Reads a file to a String.
/// Exits program upon error.
///
/// # Arguments
///
/// * `file_path` - The file path of the file to be read
///
/// # Examples
///
/// ```
/// let config = read_file_from_path("config.json");
/// println!("{}", config);
/// ```
fn read_file_from_path(file_path: &str) -> String {
use std::fs;
match fs::read_to_string(file_path) {
Ok(config) => config,
Err(_) => {
eprintln!("Error reading file. Check that the file exists and/or the supplied path is correct.");
std::process::exit(127)
},
}
}
fn convert_if_user_folder(dirname: &str, want_username: bool) -> String {
use std::env;
let env_user = env::var("USER").unwrap_or("\n".to_string());
match dirname {
name if !want_username & (name == env_user) => "$USER".to_string(),
"$USER" if want_username => env_user,
_ => dirname.to_string(),
}
}