Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config): adding the option to explicitly state the source file #160

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ 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]
/// source = "fish/conf.d/sway.fish"
/// target = "~/.config/fish/conf.d/sway.fish"
/// type = "symbolic"
/// ````
#[serde(rename = "source")]
pub file: Option<PathBuf>,
pub owner: Option<UnixUser>,
pub recurse: Option<bool>,
#[serde(rename = "if")]
Expand All @@ -38,6 +47,15 @@ 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]
/// source = "fish/conf.d/sway.fish"
/// target = "~/.config/fish/conf.d/sway.fish"
/// type = "symbolic"
/// ````
#[serde(rename = "source")]
pub file: Option<PathBuf>,
pub owner: Option<UnixUser>,
pub append: Option<String>,
pub prepend: Option<String>,
Expand Down Expand Up @@ -159,6 +177,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 +473,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 +485,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 +500,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 +542,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 +572,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