aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock13
-rw-r--r--Cargo.toml1
-rw-r--r--configamajig/Cargo.toml15
-rw-r--r--configamajig/src/lib.rs150
4 files changed, 179 insertions, 0 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 3199d7a..5441bb4 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -185,6 +185,18 @@ dependencies = [
]
[[package]]
+name = "configamajig"
+version = "0.1.0"
+dependencies = [
+ "anyhow 1.0.22 (registry+https://github.com/rust-lang/crates.io-index)",
+ "dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.102 (registry+https://github.com/rust-lang/crates.io-index)",
+ "shared 0.1.0",
+ "toml 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)",
+ "uuid 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
name = "console"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1048,6 +1060,7 @@ name = "uuid"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
+ "rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.102 (registry+https://github.com/rust-lang/crates.io-index)",
]
diff --git a/Cargo.toml b/Cargo.toml
index 948d163..e65a7bd 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -19,6 +19,7 @@ which = "3.1"
[workspace]
members = [
+ "configamajig",
"shared",
"ticket",
"hooked",
diff --git a/configamajig/Cargo.toml b/configamajig/Cargo.toml
new file mode 100644
index 0000000..d18072d
--- /dev/null
+++ b/configamajig/Cargo.toml
@@ -0,0 +1,15 @@
+[package]
+name = "configamajig"
+version = "0.1.0"
+authors = ["Michael Gattozzi <mgattozzi@gmail.com>"]
+edition = "2018"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+anyhow = "1.0"
+dirs = "2.0"
+toml = "0.5"
+shared = { path = "../shared" }
+serde = { version = "1.0", features = ["derive"] }
+uuid = { version = "0.8", features = ["serde", "v4"] }
diff --git a/configamajig/src/lib.rs b/configamajig/src/lib.rs
new file mode 100644
index 0000000..6585be6
--- /dev/null
+++ b/configamajig/src/lib.rs
@@ -0,0 +1,150 @@
+//! config management lib for dev-suite
+use anyhow::{
+ format_err,
+ Result,
+};
+use dirs::config_dir;
+use serde::{
+ Deserialize,
+ Serialize,
+};
+use shared::find_root;
+use std::{
+ fs,
+ path::PathBuf,
+};
+use uuid::Uuid;
+
+/// Creates a new user config if it does not exist
+pub fn create_user_config(name: impl Into<String>) -> Result<()> {
+ let conf_dir = config_dir()
+ .ok_or_else(|| format_err!("Unable to get the config dir for the OS"))?
+ .join("dev-suite");
+ if !conf_dir.exists() {
+ fs::create_dir_all(&conf_dir)?;
+ }
+ let conf_path = conf_dir.join("user-config.toml");
+ if !conf_path.exists() {
+ let user_config = UserConfig::new(name);
+ fs::write(&conf_path, toml::to_string_pretty(&user_config)?)?;
+ }
+ Ok(())
+}
+
+/// Creates a new repo config if it does not exist
+pub fn create_repo_config() -> Result<()> {
+ let conf_dir = find_root()?.join(".dev-suite");
+ if !conf_dir.exists() {
+ fs::create_dir_all(&conf_dir)?;
+ }
+ let conf_path = conf_dir.join("repo-config.toml");
+ if !conf_path.exists() {
+ let repo_config = RepoConfig::new();
+ fs::write(&conf_path, toml::to_string_pretty(&repo_config)?)?;
+ }
+ Ok(())
+}
+
+/// Get the path for the user config
+fn user_config_path() -> Result<PathBuf> {
+ Ok(
+ config_dir()
+ .ok_or_else(|| format_err!("Unable to get the config dir for the OS"))?
+ .join("dev-suite")
+ .join("user-config.toml"),
+ )
+}
+
+/// Get the path for the repo config
+fn repo_config_path() -> Result<PathBuf> {
+ Ok(find_root()?.join(".dev-suite").join("repo-config.toml"))
+}
+
+/// Reads in the user config
+pub fn get_user_config() -> Result<UserConfig> {
+ Ok(toml::from_slice(&fs::read(&user_config_path()?)?)?)
+}
+/// Reads in the repo config
+pub fn get_repo_config() -> Result<RepoConfig> {
+ Ok(toml::from_slice(&fs::read(&repo_config_path()?)?)?)
+}
+
+/// Writes the user config to disk
+// We don't want the user to use the old value again
+#[allow(clippy::needless_pass_by_value)]
+pub fn set_user_config(user_config: UserConfig) -> Result<()> {
+ fs::write(&user_config_path()?, toml::to_string_pretty(&user_config)?)?;
+ Ok(())
+}
+
+/// Writes the user config to disk
+// We don't want the user to use the old value again
+#[allow(clippy::needless_pass_by_value)]
+pub fn set_repo_config(repo_config: RepoConfig) -> Result<()> {
+ fs::write(&repo_config_path()?, toml::to_string_pretty(&repo_config)?)?;
+ Ok(())
+}
+
+/// User Config struct
+#[derive(Serialize, Deserialize, Debug)]
+pub struct UserConfig {
+ name: String,
+ uuid: Uuid,
+}
+
+impl UserConfig {
+ /// Create a new UserConfig from a given name and assign UUID to them
+ pub fn new(name: impl Into<String>) -> Self {
+ Self {
+ name: name.into(),
+ uuid: Uuid::new_v4(),
+ }
+ }
+}
+
+/// Repo Config struct
+#[derive(Serialize, Deserialize, Debug, Default)]
+pub struct RepoConfig {
+ maintainers: Vec<(String, Uuid)>,
+}
+
+impl RepoConfig {
+ /// Create a new RepoConfig
+ pub fn new() -> Self {
+ Self {
+ maintainers: Vec::new(),
+ }
+ }
+}
+
+/// Show repo config
+pub fn show_repo_config() -> Result<()> {
+ let conf = get_repo_config()?;
+ for m in conf.maintainers {
+ println!("{} - {}", m.0, m.1);
+ }
+ Ok(())
+}
+
+/// Show repo config
+pub fn show_user_config() -> Result<()> {
+ let conf = get_user_config()?;
+ println!("{} - {}", conf.name, conf.uuid);
+ Ok(())
+}
+
+/// Add current user to this repo's list of maintainers
+pub fn add_self_to_maintainers() -> Result<()> {
+ let mut repo_conf = get_repo_config()?;
+ let user_conf = get_user_config()?;
+ if repo_conf
+ .maintainers
+ .iter()
+ .any(|x| x.0 == user_conf.name && x.1 == user_conf.uuid)
+ {
+ Ok(())
+ } else {
+ repo_conf.maintainers.push((user_conf.name, user_conf.uuid));
+ set_repo_config(repo_conf)
+ }
+}