aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2021-03-30 12:09:49 +0200
committerGustav Sörnäs <gustav@sornas.net>2021-03-30 15:25:56 +0200
commit25687fb7c98f7d7d8b1f0a04d32092f394ec4c44 (patch)
treecd192a793dd1e8a07f1c0c9cba98527b3808a134
parentd8c269f66a4a5a17155bec5e2cb5ad97f3ba00fd (diff)
downloadmum-25687fb7c98f7d7d8b1f0a04d32092f394ec4c44.tar.gz
mumlib::config: dirs
-rw-r--r--Cargo.lock121
-rw-r--r--mumctl/src/main.rs2
-rw-r--r--mumlib/Cargo.toml1
-rw-r--r--mumlib/src/config.rs72
4 files changed, 147 insertions, 49 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 3bb9e3e..93f7118 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -32,6 +32,18 @@ dependencies = [
]
[[package]]
+name = "arrayref"
+version = "0.3.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a4c527152e37cf757a3f78aae5a06fbeefdb07ccc535c980a3208ee3060dd544"
+
+[[package]]
+name = "arrayvec"
+version = "0.5.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b"
+
+[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -49,6 +61,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
[[package]]
+name = "base64"
+version = "0.13.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd"
+
+[[package]]
name = "bincode"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -84,6 +102,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[package]]
+name = "blake2b_simd"
+version = "0.5.11"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "afa748e348ad3be8263be728124b24a24f268266f6f5d58af9d75f6a40b5c587"
+dependencies = [
+ "arrayref",
+ "arrayvec",
+ "constant_time_eq",
+]
+
+[[package]]
name = "bumpalo"
version = "3.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -180,6 +209,12 @@ dependencies = [
]
[[package]]
+name = "constant_time_eq"
+version = "0.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc"
+
+[[package]]
name = "core-foundation"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -246,6 +281,17 @@ dependencies = [
]
[[package]]
+name = "crossbeam-utils"
+version = "0.8.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e7e9d99fa91428effe99c5c6d4634cdeba32b8cf784fc428a2a687f61a952c49"
+dependencies = [
+ "autocfg",
+ "cfg-if",
+ "lazy_static",
+]
+
+[[package]]
name = "darling"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -383,6 +429,26 @@ dependencies = [
]
[[package]]
+name = "dirs"
+version = "3.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "142995ed02755914747cc6ca76fc7e4583cd18578746716d0508ea6ed558b9ff"
+dependencies = [
+ "dirs-sys",
+]
+
+[[package]]
+name = "dirs-sys"
+version = "0.3.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8e93d7f5705de3e49895a2b5e0b8855a1c27f080192ae9c32a6432d50741a57a"
+dependencies = [
+ "libc",
+ "redox_users",
+ "winapi",
+]
+
+[[package]]
name = "fern"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -497,13 +563,24 @@ dependencies = [
[[package]]
name = "getrandom"
+version = "0.1.16"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce"
+dependencies = [
+ "cfg-if",
+ "libc",
+ "wasi 0.9.0+wasi-snapshot-preview1",
+]
+
+[[package]]
+name = "getrandom"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8"
dependencies = [
"cfg-if",
"libc",
- "wasi",
+ "wasi 0.10.2+wasi-snapshot-preview1",
]
[[package]]
@@ -811,6 +888,7 @@ name = "mumlib"
version = "0.3.0"
dependencies = [
"colored",
+ "dirs",
"fern",
"log",
"serde",
@@ -1065,7 +1143,7 @@ dependencies = [
"cfg-if",
"instant",
"libc",
- "redox_syscall",
+ "redox_syscall 0.2.5",
"smallvec",
"winapi",
]
@@ -1192,7 +1270,7 @@ version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7"
dependencies = [
- "getrandom",
+ "getrandom 0.2.2",
]
[[package]]
@@ -1206,6 +1284,12 @@ dependencies = [
[[package]]
name = "redox_syscall"
+version = "0.1.57"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce"
+
+[[package]]
+name = "redox_syscall"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94341e4e44e24f6b591b59e47a8a027df12e008d73fd5672dbea9cc22f4507d9"
@@ -1214,6 +1298,17 @@ dependencies = [
]
[[package]]
+name = "redox_users"
+version = "0.3.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "de0737333e7a9502c789a36d7c7fa6092a49895d4faa31ca5df163857ded2e9d"
+dependencies = [
+ "getrandom 0.1.16",
+ "redox_syscall 0.1.57",
+ "rust-argon2",
+]
+
+[[package]]
name = "regex"
version = "1.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1238,6 +1333,18 @@ dependencies = [
]
[[package]]
+name = "rust-argon2"
+version = "0.8.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4b18820d944b33caa75a71378964ac46f58517c92b6ae5f762636247c09e78fb"
+dependencies = [
+ "base64",
+ "blake2b_simd",
+ "constant_time_eq",
+ "crossbeam-utils",
+]
+
+[[package]]
name = "rustc-hash"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1396,7 +1503,7 @@ dependencies = [
"cfg-if",
"libc",
"rand",
- "redox_syscall",
+ "redox_syscall 0.2.5",
"remove_dir_all",
"winapi",
]
@@ -1548,6 +1655,12 @@ dependencies = [
[[package]]
name = "wasi"
+version = "0.9.0+wasi-snapshot-preview1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
+
+[[package]]
+name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs
index 6535f79..2d9cd18 100644
--- a/mumctl/src/main.rs
+++ b/mumctl/src/main.rs
@@ -186,7 +186,7 @@ fn main() {
if !config::cfg_exists() {
println!(
"Config file not found. Create one in {}? [Y/n]",
- config::get_creatable_cfg_path()
+ config::default_cfg_path().display(),
);
let stdin = std::io::stdin();
let response = stdin.lock().lines().next();
diff --git a/mumlib/Cargo.toml b/mumlib/Cargo.toml
index 240e017..43bd8c5 100644
--- a/mumlib/Cargo.toml
+++ b/mumlib/Cargo.toml
@@ -12,6 +12,7 @@ license = "MIT"
[dependencies]
colored = "2.0"
+dirs = "3"
fern = "0.6"
log = "0.4"
serde = { version = "1.0", features = ["derive"] }
diff --git a/mumlib/src/config.rs b/mumlib/src/config.rs
index b0ce3f7..74b4b9c 100644
--- a/mumlib/src/config.rs
+++ b/mumlib/src/config.rs
@@ -1,9 +1,10 @@
use crate::DEFAULT_PORT;
+use log::*;
use serde::{Deserialize, Serialize};
use std::convert::TryFrom;
use std::fs;
use std::net::{SocketAddr, ToSocketAddrs};
-use std::path::Path;
+use std::path::{Path, PathBuf};
use toml::value::Array;
use toml::Value;
@@ -21,12 +22,8 @@ pub struct Config {
impl Config {
pub fn write_default_cfg(&self, create: bool) -> Result<(), std::io::Error> {
- let path = if create {
- get_creatable_cfg_path()
- } else {
- get_cfg_path()
- };
- let path = std::path::Path::new(&path);
+ let path = default_cfg_path();
+
// Possible race here. It's fine since it shows when:
// 1) the file doesn't exist when checked and is then created
// 2) the file exists when checked but is then removed
@@ -35,12 +32,13 @@ impl Config {
// should work. Unless the file is removed AND the permissions
// change, but then we don't have permissions so we can't
// do anything anyways.
+
if !create && !path.exists() {
return Ok(());
}
fs::write(
- path,
+ &path,
toml::to_string(&TOMLConfig::from(self.clone())).unwrap(), //TODO handle panic
)
}
@@ -80,36 +78,15 @@ impl ServerConfig {
}
}
-pub fn get_cfg_path() -> String {
- if let Ok(var) = std::env::var("XDG_CONFIG_HOME") {
- let path = format!("{}/mumdrc", var);
- if Path::new(&path).exists() {
- return path;
- }
- } else if let Ok(var) = std::env::var("HOME") {
- let path = format!("{}/.config/mumdrc", var);
- if Path::new(&path).exists() {
- return path;
- }
- }
-
- "/etc/mumdrc".to_string()
-}
-
-pub fn get_creatable_cfg_path() -> String {
- if let Ok(var) = std::env::var("XDG_CONFIG_HOME") {
- let path = format!("{}/mumdrc", var);
- if !Path::new(&path).exists() {
- return path;
- }
- } else if let Ok(var) = std::env::var("HOME") {
- let path = format!("{}/.config/mumdrc", var);
- if !Path::new(&path).exists() {
- return path;
+pub fn default_cfg_path() -> PathBuf {
+ match dirs::config_dir() {
+ Some(mut p) => {
+ p.push("mumdrc");
+ p
}
+ //TODO This isn't cross platform.
+ None => PathBuf::from("/etc/mumdrc")
}
-
- "/etc/mumdrc".to_string()
}
pub fn cfg_exists() -> bool {
@@ -170,12 +147,19 @@ impl From<Config> for TOMLConfig {
}
pub fn read_default_cfg() -> Config {
- Config::try_from(
- toml::from_str::<TOMLConfig>(&match fs::read_to_string(get_cfg_path()) {
- Ok(f) => f,
- Err(_) => return Config::default(),
- })
- .expect("invalid TOML in config file"), //TODO handle panic
- )
- .expect("invalid config in TOML") //TODO handle panic
+ let path = default_cfg_path();
+ match fs::read_to_string(&path) {
+ Ok(s) => {
+ let toml_config: TOMLConfig = toml::from_str(&s).expect("Invalid TOML in config file"); //TODO handle panic
+ return Config::try_from(toml_config).expect("Invalid config in TOML"); //TODO handle panic
+ },
+ Err(e) => {
+ if matches!(e.kind(), std::io::ErrorKind::NotFound) && !path.exists() {
+ warn!("Config file not found");
+ } else {
+ error!("Error reading config file: {}", e);
+ }
+ return Config::default();
+ }
+ }
}