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

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

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();