Support relative imports in config file

Co-authored-by: Christian Duerr <contact@christianduerr.com>
This commit is contained in:
Joshua Cao 2024-07-02 12:14:25 -07:00 committed by GitHub
parent 138ac426bf
commit 5e6b92db85
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 26 additions and 7 deletions

View file

@ -10,6 +10,10 @@ Notable changes to the `alacritty_terminal` crate are documented in its
## 0.14.0-dev
### Added
- Support relative path imports from config files
### Changed
- Pressing `Alt` with unicode input will now add `ESC` like for ASCII input

View file

@ -205,7 +205,7 @@ fn parse_config(
let config = deserialize_config(path, false)?;
// Merge config with imports.
let imports = load_imports(&config, config_paths, recursion_limit);
let imports = load_imports(&config, path, config_paths, recursion_limit);
Ok(serde_utils::merge(imports, config))
}
@ -237,9 +237,14 @@ pub fn deserialize_config(path: &Path, warn_pruned: bool) -> Result<Value> {
}
/// Load all referenced configuration files.
fn load_imports(config: &Value, config_paths: &mut Vec<PathBuf>, recursion_limit: usize) -> Value {
fn load_imports(
config: &Value,
base_path: &Path,
config_paths: &mut Vec<PathBuf>,
recursion_limit: usize,
) -> Value {
// Get paths for all imports.
let import_paths = match imports(config, recursion_limit) {
let import_paths = match imports(config, base_path, recursion_limit) {
Ok(import_paths) => import_paths,
Err(err) => {
error!(target: LOG_TARGET_CONFIG, "{err}");
@ -278,6 +283,7 @@ fn load_imports(config: &Value, config_paths: &mut Vec<PathBuf>, recursion_limit
/// Get all import paths for a configuration.
pub fn imports(
config: &Value,
base_path: &Path,
recursion_limit: usize,
) -> StdResult<Vec<StdResult<PathBuf, String>>, String> {
let imports = match config.get("import") {
@ -307,6 +313,12 @@ pub fn imports(
path = home_dir.join(stripped);
}
if path.is_relative() {
if let Some(base_path) = base_path.parent() {
path = base_path.join(path)
}
}
import_paths.push(Ok(path));
}

View file

@ -81,7 +81,7 @@ fn migrate_config(
// Migrate config imports.
if !options.skip_imports {
migrate_imports(options, &mut config, recursion_limit)?;
migrate_imports(options, &mut config, path, recursion_limit)?;
}
// Migrate deprecated field names to their new location.
@ -110,9 +110,10 @@ fn migrate_config(
fn migrate_imports(
options: &MigrateOptions,
config: &mut Value,
base_path: &Path,
recursion_limit: usize,
) -> Result<(), String> {
let imports = match config::imports(config, recursion_limit) {
let imports = match config::imports(config, base_path, recursion_limit) {
Ok(imports) => imports,
Err(err) => return Err(format!("import error: {err}")),
};

View file

@ -35,13 +35,15 @@ This section documents the root level of the configuration file.
file being loaded last. If a field is already present in a previous import,
it will be replaced.
All imports must either be absolute paths starting with _/_, or paths
relative to the user's home directory starting with _~/_.
All imports must either be absolute paths starting with _/_, paths relative
to the user's home directory starting with _~/_, or paths relative from the
current config file.
Example:
import = [++
_"~/.config/alacritty/base16-dark.toml"_,++
_"~/.config/alacritty/keybindings.toml"_,++
_"alacritty-theme/themes/gruvbox_dark.toml"_,++
]
*shell* = _"<string>"_ | { program = _"<string>"_, args = [_"<string>"_,] }