🧑💻 Redefine función para directorios absolutos
This commit is contained in:
parent
b2420af278
commit
d810117fa6
3 changed files with 95 additions and 45 deletions
|
@ -110,11 +110,13 @@
|
|||
//! }
|
||||
//! ```
|
||||
|
||||
use crate::util;
|
||||
|
||||
use config::builder::DefaultState;
|
||||
use config::{Config, ConfigBuilder, File};
|
||||
|
||||
use std::env;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::path::PathBuf;
|
||||
use std::sync::LazyLock;
|
||||
|
||||
// Nombre del directorio de configuración por defecto.
|
||||
|
@ -125,25 +127,12 @@ const DEFAULT_RUN_MODE: &str = "default";
|
|||
|
||||
/// Valores originales cargados desde los archivos de configuración como pares `clave = valor`.
|
||||
pub static CONFIG_VALUES: LazyLock<ConfigBuilder<DefaultState>> = LazyLock::new(|| {
|
||||
// Determina el directorio de configuración:
|
||||
// - Usa CONFIG_DIR si está definido en el entorno (p.ej.: CONFIG_DIR=/etc/myapp ./myapp).
|
||||
// - Si no, intenta DEFAULT_CONFIG_DIR dentro del proyecto (en CARGO_MANIFEST_DIR).
|
||||
// - Si nada de esto aplica, entonces usa DEFAULT_CONFIG_DIR relativo al ejecutable.
|
||||
let config_dir: PathBuf = if let Ok(env_dir) = env::var("CONFIG_DIR") {
|
||||
env_dir.into()
|
||||
} else if let Ok(manifest_dir) = env::var("CARGO_MANIFEST_DIR") {
|
||||
let manifest_config = Path::new(&manifest_dir).join(DEFAULT_CONFIG_DIR);
|
||||
if manifest_config.exists() {
|
||||
manifest_config
|
||||
} else {
|
||||
DEFAULT_CONFIG_DIR.into()
|
||||
}
|
||||
} else {
|
||||
DEFAULT_CONFIG_DIR.into()
|
||||
};
|
||||
// CONFIG_DIR (si existe) o DEFAULT_CONFIG_DIR. Si no se puede resolver, se usa tal cual.
|
||||
let dir = env::var_os("CONFIG_DIR").unwrap_or_else(|| DEFAULT_CONFIG_DIR.into());
|
||||
let config_dir = util::resolve_absolute_dir(&dir).unwrap_or_else(|_| PathBuf::from(&dir));
|
||||
|
||||
// Determina el modo de ejecución según la variable de entorno PAGETOP_RUN_MODE. Por defecto usa
|
||||
// DEFAULT_RUN_MODE si no está definida (p.ej.: PAGETOP_RUN_MODE=production ./myapp).
|
||||
// Modo de ejecución según la variable de entorno PAGETOP_RUN_MODE. Si no está definida, se usa
|
||||
// por defecto, DEFAULT_RUN_MODE (p.ej.: PAGETOP_RUN_MODE=production).
|
||||
let rm = env::var("PAGETOP_RUN_MODE").unwrap_or_else(|_| DEFAULT_RUN_MODE.into());
|
||||
|
||||
Config::builder()
|
||||
|
|
53
src/util.rs
53
src/util.rs
|
@ -2,35 +2,44 @@
|
|||
|
||||
use crate::trace;
|
||||
|
||||
use std::env;
|
||||
use std::io;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
// FUNCIONES ÚTILES ********************************************************************************
|
||||
|
||||
/// Devuelve la ruta absoluta a un directorio existente.
|
||||
/// Resuelve y valida la ruta de un directorio existente, devolviendo una ruta absoluta.
|
||||
///
|
||||
/// * Si `relative_path` es una ruta absoluta, entonces se ignora `root_path`.
|
||||
/// * Si la ruta final es relativa, se convierte en absoluta respecto al directorio actual.
|
||||
/// * Devuelve error si la ruta no existe o no es un directorio.
|
||||
/// - Si la ruta es relativa, se resuelve respecto al directorio del proyecto según la variable de
|
||||
/// entorno `CARGO_MANIFEST_DIR` (si existe) o, en su defecto, respecto al directorio actual de
|
||||
/// trabajo.
|
||||
/// - Normaliza y valida la ruta final (resuelve `.`/`..` y enlaces simbólicos).
|
||||
/// - Devuelve error si la ruta no existe o no es un directorio.
|
||||
///
|
||||
/// # Ejemplo
|
||||
/// # Ejemplos
|
||||
///
|
||||
/// ```rust,no_run
|
||||
/// use pagetop::prelude::*;
|
||||
///
|
||||
/// let root = "/home/user";
|
||||
/// let rel = "documents";
|
||||
/// println!("{:#?}", util::absolute_dir(root, rel));
|
||||
/// // Ruta relativa, se resuelve respecto a CARGO_MANIFEST_DIR o al directorio actual (`cwd`).
|
||||
/// println!("{:#?}", util::resolve_absolute_dir("documents"));
|
||||
///
|
||||
/// // Ruta absoluta, se normaliza y valida tal cual.
|
||||
/// println!("{:#?}", util::resolve_absolute_dir("/var/www"));
|
||||
/// ```
|
||||
pub fn absolute_dir<P, Q>(root_path: P, relative_path: Q) -> io::Result<PathBuf>
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
Q: AsRef<Path>,
|
||||
{
|
||||
// Une ambas rutas:
|
||||
// - Si `relative_path` es absoluta, el `join` la devuelve tal cual, descartando `root_path`.
|
||||
// - Si el resultado es aún relativo, lo será respecto al directorio actual.
|
||||
let candidate = root_path.as_ref().join(relative_path.as_ref());
|
||||
pub fn resolve_absolute_dir<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
|
||||
let path = path.as_ref();
|
||||
|
||||
let candidate = if path.is_absolute() {
|
||||
path.to_path_buf()
|
||||
} else {
|
||||
// Directorio base CARGO_MANIFEST_DIR si está disponible; o current_dir() en su defecto.
|
||||
env::var_os("CARGO_MANIFEST_DIR")
|
||||
.map(PathBuf::from)
|
||||
.or_else(|| env::current_dir().ok())
|
||||
.unwrap_or_else(|| PathBuf::from("."))
|
||||
.join(path)
|
||||
};
|
||||
|
||||
// Resuelve `.`/`..`, enlaces simbólicos y obtiene la ruta absoluta en un único paso.
|
||||
let absolute_dir = candidate.canonicalize()?;
|
||||
|
@ -47,6 +56,16 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
/// Devuelve la ruta absoluta a un directorio existente.
|
||||
#[deprecated(since = "0.3.0", note = "Use [`resolve_absolute_dir`] instead")]
|
||||
pub fn absolute_dir<P, Q>(root_path: P, relative_path: Q) -> io::Result<PathBuf>
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
Q: AsRef<Path>,
|
||||
{
|
||||
resolve_absolute_dir(root_path.as_ref().join(relative_path.as_ref()))
|
||||
}
|
||||
|
||||
// MACROS ÚTILES ***********************************************************************************
|
||||
|
||||
#[doc(hidden)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue