Skip to content

Commit

Permalink
+ option trim for template
Browse files Browse the repository at this point in the history
This commit adds option to control if all secret contents inserting to
the template needs trim.

Will remove leading and trailing whitespace of the content to insert
while enable.

Default enabled.
  • Loading branch information
oluceps committed Nov 10, 2024
1 parent 4878f6d commit 28aa9fe
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
4 changes: 4 additions & 0 deletions module/template.nix
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ let
Content of the template
'';
};
trim = (mkEnableOption { }) // {
default = true;
description = "remove trailing and leading whitespace of the secret content to insert";
};
name = mkOption {
type = types.str;
default = submod.config._module.args.name;
Expand Down
15 changes: 13 additions & 2 deletions src/cmd/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,26 @@ impl Profile {
let mut template = t.content.clone();
let hashstrs_of_it = t.parse_hash_str_list().expect("parse template");

let trim_the_insertial = t.trim;

hashstr_ctx_map
.iter()
.filter(|(k, _)| hashstrs_of_it.contains(k))
.for_each(|(k, v)| {
// render
// render and insert
trace!("template before process: {}", template);

let raw_composed_insertial = String::from_utf8_lossy(v).to_string();

let insertial = if trim_the_insertial {
raw_composed_insertial.trim()
} else {
raw_composed_insertial.as_str()
};

template = template.replace(
format!("{{{{ {} }}}}", hex::encode(k.as_slice())).as_str(),
String::from_utf8_lossy(v).to_string().as_str(),
insertial,
);
});

Expand Down
1 change: 1 addition & 0 deletions src/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub struct Secret {
pub struct Template {
pub name: String,
pub content: String,
pub trim: bool,
pub group: String,
pub mode: String,
pub owner: String,
Expand Down

0 comments on commit 28aa9fe

Please sign in to comment.