Skip to content

Commit

Permalink
feat(config): adding the option to explicitly state the source file
Browse files Browse the repository at this point in the history
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" }
```
  • Loading branch information
nydragon committed Dec 24, 2023
1 parent a46ed61 commit d6eb7d1
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ impl fmt::Display for UnixUser {
#[serde(deny_unknown_fields)]
pub struct SymbolicTarget {
pub target: PathBuf,
/// If supplied it replaces the source file deduced from the TOML section header/given as key.
/// ```toml
/// [sway.files.fish]
/// file = "fish/conf.d/sway.fish"
/// target = "~/.config/fish/conf.d/sway.fish"
/// type = "symbolic"
/// ````
pub file: Option<PathBuf>,
pub owner: Option<UnixUser>,
pub recurse: Option<bool>,
#[serde(rename = "if")]
Expand All @@ -38,6 +46,14 @@ pub struct SymbolicTarget {
#[serde(deny_unknown_fields)]
pub struct TemplateTarget {
pub target: PathBuf,
/// If supplied it replaces the source file deduced from the TOML section header/given as key.
/// ```toml
/// [sway.files.fish]
/// file = "fish/conf.d/sway.fish"
/// target = "~/.config/fish/conf.d/sway.fish"
/// type = "symbolic"
/// ````
pub file: Option<PathBuf>,
pub owner: Option<UnixUser>,
pub append: Option<String>,
pub prepend: Option<String>,
Expand Down Expand Up @@ -159,6 +175,10 @@ pub fn load_configuration(
merge_configuration_files(global, local, patch).context("merge configuration files")?;
trace!("Merged config: {:#?}", merged_config);

debug!("Replacing source by the file key indicated in complex targets");
merged_config.files =
replace_source(&merged_config).context("replace source for complex targets")?;

debug!("Expanding files which are directories...");
merged_config.files =
expand_directories(&merged_config).context("expand files that are directories")?;
Expand Down Expand Up @@ -451,6 +471,7 @@ impl<T: Into<PathBuf>> From<T> for SymbolicTarget {
fn from(input: T) -> Self {
SymbolicTarget {
target: input.into(),
file: None,
owner: None,
condition: None,
recurse: None,
Expand All @@ -462,6 +483,7 @@ impl<T: Into<PathBuf>> From<T> for TemplateTarget {
fn from(input: T) -> Self {
TemplateTarget {
target: input.into(),
file: None,
owner: None,
append: None,
prepend: None,
Expand All @@ -476,6 +498,7 @@ impl SymbolicTarget {
target: self.target,
owner: self.owner,
condition: self.condition,
file: self.file,
prepend: None,
append: None,
}
Expand Down Expand Up @@ -517,6 +540,7 @@ fn expand_directory(source: &Path, target: &FileTarget, config: &Configuration)
let recurse = match target {
FileTarget::Symbolic(SymbolicTarget {
target: _,
file: _,
owner: _,
condition: _,
recurse: Some(rec),
Expand Down Expand Up @@ -546,6 +570,36 @@ fn expand_directory(source: &Path, target: &FileTarget, config: &Configuration)
}
}

fn replace_source(config: &Configuration) -> Result<Files> {
let expanded = config
.files
.iter()
.map(|(source, target)| {
let mut map = Files::new();

match target {
FileTarget::Symbolic(t) => {
map.insert(
t.file.clone().unwrap_or(source.clone()).into(),
target.clone(),
);
}
FileTarget::ComplexTemplate(t) => {
map.insert(
t.file.clone().unwrap_or(source.clone()).into(),
target.clone(),
);
}
FileTarget::Automatic(_) => {
map.insert(source.clone(), target.clone());
}
};
Ok(map)
})
.collect::<Result<Vec<Files>>>()?;
Ok(expanded.into_iter().flatten().collect::<Files>())
}

#[cfg(unix)]
impl UnixUser {
pub fn as_sudo_arg(&self) -> String {
Expand Down

0 comments on commit d6eb7d1

Please sign in to comment.