Skip to content

Commit d6eb7d1

Browse files
author
Nydragon
committed
feat(config): adding the option to explicitly state the source file
This modification allows to specify the source file in a "file" key for complex targets. Using this key overrides the path deduced from the TOML section header or the TOML key. Examples: ```toml [sway.files.fish] file = "fish/conf.d/sway.fish" target = "~/.config/fish/conf.d/sway.fish" type = "symbolic" ``` ```toml [sway.files] fish = { target = "~/.config/fish/conf.d/sway.fish", type = "symbolic", file = "fish/conf.d/sway.fish" } ```
1 parent a46ed61 commit d6eb7d1

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

src/config.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ impl fmt::Display for UnixUser {
2828
#[serde(deny_unknown_fields)]
2929
pub struct SymbolicTarget {
3030
pub target: PathBuf,
31+
/// If supplied it replaces the source file deduced from the TOML section header/given as key.
32+
/// ```toml
33+
/// [sway.files.fish]
34+
/// file = "fish/conf.d/sway.fish"
35+
/// target = "~/.config/fish/conf.d/sway.fish"
36+
/// type = "symbolic"
37+
/// ````
38+
pub file: Option<PathBuf>,
3139
pub owner: Option<UnixUser>,
3240
pub recurse: Option<bool>,
3341
#[serde(rename = "if")]
@@ -38,6 +46,14 @@ pub struct SymbolicTarget {
3846
#[serde(deny_unknown_fields)]
3947
pub struct TemplateTarget {
4048
pub target: PathBuf,
49+
/// If supplied it replaces the source file deduced from the TOML section header/given as key.
50+
/// ```toml
51+
/// [sway.files.fish]
52+
/// file = "fish/conf.d/sway.fish"
53+
/// target = "~/.config/fish/conf.d/sway.fish"
54+
/// type = "symbolic"
55+
/// ````
56+
pub file: Option<PathBuf>,
4157
pub owner: Option<UnixUser>,
4258
pub append: Option<String>,
4359
pub prepend: Option<String>,
@@ -159,6 +175,10 @@ pub fn load_configuration(
159175
merge_configuration_files(global, local, patch).context("merge configuration files")?;
160176
trace!("Merged config: {:#?}", merged_config);
161177

178+
debug!("Replacing source by the file key indicated in complex targets");
179+
merged_config.files =
180+
replace_source(&merged_config).context("replace source for complex targets")?;
181+
162182
debug!("Expanding files which are directories...");
163183
merged_config.files =
164184
expand_directories(&merged_config).context("expand files that are directories")?;
@@ -451,6 +471,7 @@ impl<T: Into<PathBuf>> From<T> for SymbolicTarget {
451471
fn from(input: T) -> Self {
452472
SymbolicTarget {
453473
target: input.into(),
474+
file: None,
454475
owner: None,
455476
condition: None,
456477
recurse: None,
@@ -462,6 +483,7 @@ impl<T: Into<PathBuf>> From<T> for TemplateTarget {
462483
fn from(input: T) -> Self {
463484
TemplateTarget {
464485
target: input.into(),
486+
file: None,
465487
owner: None,
466488
append: None,
467489
prepend: None,
@@ -476,6 +498,7 @@ impl SymbolicTarget {
476498
target: self.target,
477499
owner: self.owner,
478500
condition: self.condition,
501+
file: self.file,
479502
prepend: None,
480503
append: None,
481504
}
@@ -517,6 +540,7 @@ fn expand_directory(source: &Path, target: &FileTarget, config: &Configuration)
517540
let recurse = match target {
518541
FileTarget::Symbolic(SymbolicTarget {
519542
target: _,
543+
file: _,
520544
owner: _,
521545
condition: _,
522546
recurse: Some(rec),
@@ -546,6 +570,36 @@ fn expand_directory(source: &Path, target: &FileTarget, config: &Configuration)
546570
}
547571
}
548572

573+
fn replace_source(config: &Configuration) -> Result<Files> {
574+
let expanded = config
575+
.files
576+
.iter()
577+
.map(|(source, target)| {
578+
let mut map = Files::new();
579+
580+
match target {
581+
FileTarget::Symbolic(t) => {
582+
map.insert(
583+
t.file.clone().unwrap_or(source.clone()).into(),
584+
target.clone(),
585+
);
586+
}
587+
FileTarget::ComplexTemplate(t) => {
588+
map.insert(
589+
t.file.clone().unwrap_or(source.clone()).into(),
590+
target.clone(),
591+
);
592+
}
593+
FileTarget::Automatic(_) => {
594+
map.insert(source.clone(), target.clone());
595+
}
596+
};
597+
Ok(map)
598+
})
599+
.collect::<Result<Vec<Files>>>()?;
600+
Ok(expanded.into_iter().flatten().collect::<Files>())
601+
}
602+
549603
#[cfg(unix)]
550604
impl UnixUser {
551605
pub fn as_sudo_arg(&self) -> String {

0 commit comments

Comments
 (0)