feat: move config file to ~/.config/mqtt-chat.ini

This commit is contained in:
Eric Lynema 2025-07-15 13:19:32 -04:00
parent 4d136b577a
commit b9bad2f1a3
3 changed files with 62 additions and 5 deletions

49
Cargo.lock generated
View File

@ -153,6 +153,27 @@ dependencies = [
"winapi",
]
[[package]]
name = "dirs"
version = "5.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44c45a9d03d6676652bcb5e724c7e988de1acad23a711b5217ab9cbecbec2225"
dependencies = [
"dirs-sys",
]
[[package]]
name = "dirs-sys"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c"
dependencies = [
"libc",
"option-ext",
"redox_users",
"windows-sys 0.48.0",
]
[[package]]
name = "either"
version = "1.15.0"
@ -346,6 +367,16 @@ version = "0.2.174"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1171693293099992e19cddea4e8b849964e9846f4acee11b3948bcc337be8776"
[[package]]
name = "libredox"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1580801010e535496706ba011c15f8532df6b42297d2e471fec38ceadd8c0638"
dependencies = [
"bitflags",
"libc",
]
[[package]]
name = "lock_api"
version = "0.4.13"
@ -424,6 +455,12 @@ version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d05e27ee213611ffe7d6348b942e8f942b37114c00cc03cec254295a4a17852e"
[[package]]
name = "option-ext"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d"
[[package]]
name = "parking_lot"
version = "0.12.4"
@ -551,6 +588,17 @@ dependencies = [
"bitflags",
]
[[package]]
name = "redox_users"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba009ff324d1fc1b900bd1fdb31564febe58a8ccc8a6fdbb93b543d33b13ca43"
dependencies = [
"getrandom",
"libredox",
"thiserror",
]
[[package]]
name = "ring"
version = "0.17.14"
@ -918,6 +966,7 @@ version = "0.1.0"
dependencies = [
"configparser",
"crossterm",
"dirs",
"futures",
"rand",
"ratatui",

View File

@ -6,6 +6,7 @@ edition = "2021"
[dependencies]
configparser = "3.0.4"
crossterm = { version = "0.27.0", features = ["event-stream"] }
dirs = "5.0.1"
futures = "0.3.30"
rand = "0.8.5"
ratatui = { version = "0.26.1", features = ["crossterm"] }

View File

@ -5,12 +5,13 @@ use crossterm::{
};
use ratatui::{prelude::*, widgets::*};
use rumqttc::{AsyncClient, MqttOptions, QoS};
use std::{io, time::Duration, path::Path};
use std::{io, time::Duration, path::PathBuf};
use tokio::sync::mpsc;
use tokio_stream::StreamExt;
use rand::Rng;
use serde::{Deserialize, Serialize};
use configparser::ini::Ini;
use std::fs;
#[derive(Debug, Serialize, Deserialize)]
struct ChatMessage {
@ -83,17 +84,23 @@ async fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Re
let (tx, mut rx) = mpsc::channel::<(String, String)>(100);
let mut config = Ini::new();
let config_path = "config.ini";
let mut config_path = PathBuf::new();
if let Some(home_dir) = dirs::home_dir() {
config_path.push(home_dir);
config_path.push(".config");
fs::create_dir_all(&config_path).unwrap();
config_path.push("mqtt-chat.ini");
}
if !Path::new(config_path).exists() {
if !config_path.exists() {
config.set("mqtt", "server", Some("172.16.0.3".to_owned()));
config.set("mqtt", "port", Some("1883".to_owned()));
config.set("mqtt", "username", Some("".to_owned()));
config.set("mqtt", "password", Some("".to_owned()));
config.write(config_path).unwrap();
config.write(&config_path).unwrap();
}
let map = config.load(config_path).unwrap();
let map = config.load(&config_path).unwrap();
let server = map.get("mqtt").unwrap().get("server").unwrap().clone().unwrap();
let port = map.get("mqtt").unwrap().get("port").unwrap().clone().unwrap().parse::<u16>().unwrap();
let username = map.get("mqtt").unwrap().get("username").unwrap().clone().unwrap();