feat: initialize a utils lib with config functions handling config files in local and standard OS directories
This commit is contained in:
parent
5269dd7789
commit
cf700d3348
2
.gitignore
vendored
2
.gitignore
vendored
@ -21,3 +21,5 @@ target/
|
|||||||
*.sln
|
*.sln
|
||||||
*.sw?
|
*.sw?
|
||||||
|
|
||||||
|
# Ignore .env files
|
||||||
|
.env
|
||||||
|
19
Cargo.lock
generated
19
Cargo.lock
generated
@ -1080,6 +1080,15 @@ dependencies = [
|
|||||||
"crypto-common",
|
"crypto-common",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "directories"
|
||||||
|
version = "5.0.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9a49173b84e034382284f27f1af4dcbbd231ffa358c0fe316541a7337f376a35"
|
||||||
|
dependencies = [
|
||||||
|
"dirs-sys 0.4.1",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "dirs"
|
name = "dirs"
|
||||||
version = "4.0.0"
|
version = "4.0.0"
|
||||||
@ -3917,6 +3926,7 @@ version = "0.1.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"dotenv",
|
"dotenv",
|
||||||
"libc",
|
"libc",
|
||||||
|
"utils",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -5034,6 +5044,15 @@ version = "0.7.6"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9"
|
checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "utils"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"anyhow",
|
||||||
|
"directories",
|
||||||
|
"dotenv",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "uuid"
|
name = "uuid"
|
||||||
version = "0.8.2"
|
version = "0.8.2"
|
||||||
|
@ -3,5 +3,6 @@ resolver = "2"
|
|||||||
members = [
|
members = [
|
||||||
"crates/app",
|
"crates/app",
|
||||||
"crates/sesam-vitale",
|
"crates/sesam-vitale",
|
||||||
"crates/desktop"
|
"crates/desktop",
|
||||||
|
"crates/utils",
|
||||||
]
|
]
|
||||||
|
14
README.md
14
README.md
@ -7,6 +7,20 @@ Logiciel de Pharmacie libre et open-source.
|
|||||||
- `app`: Interface du logiciel, servie par un serveur web propulsé par Axum. Utilisable en mode endpoint ou encapsulé dans le client `desktop`
|
- `app`: Interface du logiciel, servie par un serveur web propulsé par Axum. Utilisable en mode endpoint ou encapsulé dans le client `desktop`
|
||||||
- `desktop`: Client desktop propulsé par Tauri, encapsulant le serveur web `app`
|
- `desktop`: Client desktop propulsé par Tauri, encapsulant le serveur web `app`
|
||||||
- `sesam-vitale`: Bibliothèque de gestion des services SESAM-Vitale (Lecture des cartes CPS et Vitale, téléservices ...)
|
- `sesam-vitale`: Bibliothèque de gestion des services SESAM-Vitale (Lecture des cartes CPS et Vitale, téléservices ...)
|
||||||
|
- `utils`: Bibliothèque de fonctions utilitaires
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
### Fichiers de configuration
|
||||||
|
|
||||||
|
Certaines librairies nécessitent de définir certaines paramètres de configuration pour fonctionner correctement, en particulier le moteur SESAM-Vitale.
|
||||||
|
|
||||||
|
Ces paramètres sont définis dans un fichier de configuration `.env` situé dans un des dossiers suivant (par ordre de priorité) :
|
||||||
|
- dans le dossier courant (`./.env`)
|
||||||
|
- dans le dossier du manifeste (par exemple `crates/sesam-vitale/.env`)
|
||||||
|
- dans le dossier de configuration standard de l'OS (par exemple, sur linux, `~/.config/krys4lide/.env` - [plus d'info](https://github.com/dirs-dev/directories-rs?tab=readme-ov-file#projectdirs))
|
||||||
|
|
||||||
|
Des exemples de fichiers de configuration sont disponibles à la racine du projet : `.env.linux.example` et `.env.win.example`.
|
||||||
|
|
||||||
## Development
|
## Development
|
||||||
|
|
||||||
|
@ -4,8 +4,8 @@ version = "0.1.0"
|
|||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
dotenv = "0.15"
|
|
||||||
libc = "0.2"
|
libc = "0.2"
|
||||||
|
utils = { path = "../utils" }
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
dotenv = "0.15"
|
dotenv = "0.15"
|
||||||
|
@ -1,15 +1,15 @@
|
|||||||
/// High level API for the SSV library,
|
/// High level API for the SSV library,
|
||||||
/// based on the low level bindings in libssv.rs.
|
/// based on the low level bindings in libssv.rs.
|
||||||
extern crate dotenv;
|
|
||||||
use libc::{c_void, size_t};
|
use libc::{c_void, size_t};
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::ffi::CString;
|
use std::ffi::CString;
|
||||||
use std::path::PathBuf;
|
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
|
|
||||||
use crate::cps::lire_carte;
|
use crate::cps::lire_carte;
|
||||||
use crate::libssv::{SSV_InitLIB2, SSV_LireConfig};
|
use crate::libssv::{SSV_InitLIB2, SSV_LireConfig};
|
||||||
|
|
||||||
|
use ::utils::config::load_config;
|
||||||
|
|
||||||
fn ssv_init_lib_2() {
|
fn ssv_init_lib_2() {
|
||||||
let ini_str = env::var("SESAM_INI_PATH").expect("SESAM_INI_PATH must be set");
|
let ini_str = env::var("SESAM_INI_PATH").expect("SESAM_INI_PATH must be set");
|
||||||
let ini = CString::new(ini_str).expect("CString::new failed");
|
let ini = CString::new(ini_str).expect("CString::new failed");
|
||||||
@ -41,12 +41,11 @@ fn ssv_lire_config() {
|
|||||||
pub fn demo() {
|
pub fn demo() {
|
||||||
// TODO : this is probably not working on release, because I'm not sure it exists a CARGO_MANIFEST_DIR and so it can find the `.env`
|
// TODO : this is probably not working on release, because I'm not sure it exists a CARGO_MANIFEST_DIR and so it can find the `.env`
|
||||||
// Maybe we could use a system standard config path to store a config file
|
// Maybe we could use a system standard config path to store a config file
|
||||||
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
|
|
||||||
let manifest_path = PathBuf::from(manifest_dir);
|
|
||||||
dotenv::from_path(manifest_path.join(".env")).ok();
|
|
||||||
|
|
||||||
println!("------- Demo for the SSV library --------");
|
println!("------- Demo for the SSV library --------");
|
||||||
|
|
||||||
|
load_config().unwrap();
|
||||||
|
|
||||||
ssv_init_lib_2();
|
ssv_init_lib_2();
|
||||||
|
|
||||||
let code_pin = "1234";
|
let code_pin = "1234";
|
||||||
|
9
crates/utils/Cargo.toml
Normal file
9
crates/utils/Cargo.toml
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
[package]
|
||||||
|
name = "utils"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
anyhow = "1.0"
|
||||||
|
directories = "5.0"
|
||||||
|
dotenv = "0.15"
|
48
crates/utils/src/config.rs
Normal file
48
crates/utils/src/config.rs
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
use std::{env, path::PathBuf};
|
||||||
|
|
||||||
|
use anyhow::{bail, Context, Result};
|
||||||
|
use directories::ProjectDirs;
|
||||||
|
use dotenv::from_path;
|
||||||
|
|
||||||
|
const CONFIG_FILE_NAME: &str = ".env";
|
||||||
|
|
||||||
|
pub fn get_config_dirs() -> Vec<PathBuf> {
|
||||||
|
let mut config_dirs = vec![
|
||||||
|
PathBuf::from(""), // Current directory
|
||||||
|
];
|
||||||
|
if let Ok(manifest_dir) = env::var("CARGO_MANIFEST_DIR") {
|
||||||
|
config_dirs.push(PathBuf::from(manifest_dir));
|
||||||
|
}
|
||||||
|
if let Some(proj_dirs) = ProjectDirs::from("org", "P4pillon", "Krys4lide") {
|
||||||
|
config_dirs.push(proj_dirs.config_dir().to_path_buf());
|
||||||
|
}
|
||||||
|
config_dirs
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_config_files() -> Result<Vec<PathBuf>> {
|
||||||
|
let config_dirs = get_config_dirs();
|
||||||
|
let mut config_files = Vec::new();
|
||||||
|
for config_dir in config_dirs.iter() {
|
||||||
|
let config_file = config_dir.join(CONFIG_FILE_NAME);
|
||||||
|
if config_file.exists() {
|
||||||
|
config_files.push(config_file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if config_files.is_empty() {
|
||||||
|
bail!(
|
||||||
|
"No config file {CONFIG_FILE_NAME} found in the following directories: {config_dirs:#?}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Ok(config_files)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn load_config() -> Result<()> {
|
||||||
|
let config_files = get_config_files()?;
|
||||||
|
// Load the first config file found
|
||||||
|
// TODO: add a verbose log to list all config files found
|
||||||
|
println!(
|
||||||
|
"DEBUG: Config files found (1st loaded): {:#?}",
|
||||||
|
config_files
|
||||||
|
);
|
||||||
|
from_path(config_files[0].as_path()).context("Failed to load config file")
|
||||||
|
}
|
1
crates/utils/src/lib.rs
Normal file
1
crates/utils/src/lib.rs
Normal file
@ -0,0 +1 @@
|
|||||||
|
pub mod config;
|
Loading…
Reference in New Issue
Block a user