Skip to content

Commit b4d6082

Browse files
author
Alexander Weber
committed
+ helper functions
1 parent 4dad653 commit b4d6082

7 files changed

Lines changed: 165 additions & 86 deletions

File tree

.complate/config.yaml

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
version: 0.10
1+
version: 0.11
22
templates:
33
zero:
44
content:
55
inline: |-
66
{{ a.alpha }}
77
values:
88
a.alpha:
9-
static: '"test"'
9+
static: ALPHA
1010
one:
1111
content:
1212
file: ./.complate/templates/arbitraty-template-file.tpl
1313
values:
14-
a.summary:
15-
static: "random summary"
14+
a.pwd:
15+
env: "PWD"
1616
two:
1717
content:
1818
inline: |-
@@ -53,3 +53,14 @@ templates:
5353
display: bravo
5454
value:
5555
shell: printf bravo
56+
f.foxtrot:
57+
env: "FOXTROT"
58+
three:
59+
content:
60+
inline: |-
61+
{{ _decode "dGVzdA==" }}
62+
helpers:
63+
"_decode":
64+
shell: |-
65+
printf "$(printf $VALUE | base64 -D)"
66+
values: {}

docs/wiki/content/docs/command reference/_index.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,15 @@ Either one of the `backend+` features (or both) MUST be enabled for `complate` t
5656
|Template|-t|--template|Skip the template selection by defining the used template from the configuration via this argument||stable
5757
|Backend|-b|--backend|Defines the backend for the user interaction.||`CLI` is stable. `UI` is experimental (feature = "backend+ui").
5858
|Value|-v|--value|Overrides a certain variable with a given string value. Specify the variable and value with an equals sign as separator|Multiple allowed. Example: <br/> `-v"variable=some arbitrary value"`|experimental
59+
|Helpers||--helpers|Enables helper functions.||experimental
5960
|===
6061

6162
=== Examples:
6263

6364
* `complate render` +
6465
* `complate render -c .complate/alternative.yml` +
6566
* `complate -e render -c <(cat .complate/alternative.yaml) -bui -t0.default` +
67+
* `complate -e render -t three --helpers` +
6668

6769
== Configuration file
6870

docs/wiki/content/docs/configuration/_index.adoc

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@ The `complate` configuration schema written in YAML. It includes a version numbe
1515
== Structure by example
1616

1717
```
18-
version: 0.10
18+
version: 0.11
1919
templates:
2020
zero:
2121
content:
2222
inline: |-
2323
{{ a.alpha }}
2424
values:
2525
a.alpha:
26-
env: ALPHA
26+
static: ALPHA
2727
one:
2828
content:
2929
file: ./.complate/templates/arbitraty-template-file.tpl
3030
values:
31-
a.summary:
32-
static: "random summary"
31+
a.pwd:
32+
env: "PWD"
3333
two:
3434
content:
3535
inline: |-
@@ -70,6 +70,18 @@ templates:
7070
display: bravo
7171
value:
7272
shell: printf bravo
73+
f.foxtrot:
74+
env: "FOXTROT"
75+
three:
76+
content:
77+
inline: |-
78+
{{ _decode "dGVzdA==" }}
79+
helpers:
80+
"_decode":
81+
shell: |-
82+
printf "$(printf $VALUE | base64 -D)"
83+
values: {}
84+
7385
```
7486

7587
The first tag has the current version of the configuration schema. It needs to be equal to the major version number of the cli program. Since `complate` is still sub-one, the versioning is not yet stable and therefore, the major *and* minor version need to be equal to the ones of the cli program. +
@@ -94,3 +106,7 @@ You can resolve the value for each variable individual. Following options are av
94106
#### Shell security
95107

96108
Since the `shell` value provider is able to run arbitrary shell commands, it is only allowed if and only if the `SHELL_TRUST` argument is explicitly set. See the `render` command reference for possible values for this setting. If *not* set, the provider will throw an unrecoverable error and the program will abort.
109+
110+
### Helpers
111+
112+
Helper functions can be used to modify a value that is encoded into the template itself. These functions can be defined freely. This functionality is experimental and requires the `--helpers` flag to be enabled. The value that is to be modified is given to the shell command via the environment variable "$VALUE".

src/args/mod.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,22 @@ impl CallArgs {
1414
match args.backend {
1515
#[cfg(feature = "backend+ui")]
1616
Backend::UI => {
17-
return Err(Box::new(crate::error::NeedExperimentalFlag::default()))
17+
return Err(Box::new(crate::error::NeedExperimentalFlag::new(
18+
"to enable UI backend",
19+
)))
1820
}
1921
#[cfg(feature = "backend+cli")]
2022
Backend::CLI => {}
2123
};
2224
if args.value_overrides.len() > 0 {
23-
return Err(Box::new(crate::error::NeedExperimentalFlag::default()));
25+
return Err(Box::new(crate::error::NeedExperimentalFlag::new(
26+
"to enable value overrides",
27+
)));
28+
}
29+
if args.helpers {
30+
return Err(Box::new(crate::error::NeedExperimentalFlag::new(
31+
"to enable helpers",
32+
)));
2433
}
2534
#[allow(unreachable_code)]
2635
Ok(())
@@ -46,6 +55,7 @@ pub struct RenderArguments {
4655
pub configuration: String,
4756
pub template: Option<String>,
4857
pub value_overrides: std::collections::HashMap<String, String>,
58+
pub helpers: bool,
4959
pub shell_trust: ShellTrust,
5060
pub strict: bool,
5161
pub backend: Backend,
@@ -127,6 +137,13 @@ impl ClapArgumentLoader {
127137
.multiple(false)
128138
.required(false)
129139
.takes_value(true))
140+
.arg(clap::Arg::with_name("helpers")
141+
.long("helpers")
142+
.value_name("HELPERS")
143+
.help("Enables handlebar helpers.")
144+
.multiple(false)
145+
.required(false)
146+
.takes_value(false))
130147
.arg(clap::Arg::with_name("backend")
131148
.short("b")
132149
.long("backend")
@@ -218,6 +235,8 @@ impl ClapArgumentLoader {
218235
None => Backend::UI,
219236
};
220237

238+
let helpers = x.is_present("helpers");
239+
221240
Ok(CallArgs {
222241
privileges,
223242
command: Command::Render(RenderArguments {
@@ -227,6 +246,7 @@ impl ClapArgumentLoader {
227246
shell_trust,
228247
strict,
229248
backend,
249+
helpers,
230250
}),
231251
})
232252
}

src/config/mod.rs

Lines changed: 74 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ pub struct Template {
1111
pub content: Content,
1212
#[serde(default)]
1313
pub values: std::collections::BTreeMap<String, VariableDefinition>,
14+
#[serde(default)]
15+
pub helpers: std::collections::BTreeMap<String, Helper>,
16+
}
17+
18+
#[derive(Debug, serde::Serialize, serde::Deserialize)]
19+
#[serde(rename_all = "snake_case")]
20+
pub struct Helper {
21+
pub shell: String,
1422
}
1523

1624
#[derive(Debug, serde::Serialize, serde::Deserialize)]
@@ -52,82 +60,74 @@ pub enum VariableDefinition {
5260
},
5361
}
5462

55-
impl Config {
56-
pub fn new(version: String) -> Self {
57-
Self {
58-
version,
59-
templates: std::collections::BTreeMap::new(),
60-
}
61-
}
62-
}
63-
64-
impl Template {
65-
pub fn new(content: Content) -> Self {
66-
Self {
67-
content,
68-
values: std::collections::BTreeMap::new(),
69-
}
70-
}
71-
}
72-
7363
pub async fn default_config() -> String {
74-
r###"version: 0.10
64+
r###"version: 0.11
7565
templates:
76-
zero:
77-
content:
78-
inline: |-
79-
{{ a.alpha }}
80-
values:
81-
a.alpha:
82-
env: ALPHA
83-
one:
84-
content:
85-
file: ./.complate/templates/arbitraty-template-file.tpl
86-
values:
87-
a.summary:
88-
env: "random summary"
89-
two:
90-
content:
91-
inline: |-
92-
{{ a.alpha }}
93-
{{ b.bravo }}
94-
{{ c.charlie }}
95-
{{ d.delta }}
96-
{{ e.echo }}
97-
values:
98-
a.alpha:
99-
prompt: "alpha"
100-
b.bravo:
101-
shell: "printf bravo"
102-
c.charlie:
103-
static: "charlie"
104-
d.delta:
105-
select:
106-
text: Select the version level that shall be incremented
107-
options:
108-
alpha:
109-
display: alpha
110-
value:
111-
static: alpha
112-
bravo:
113-
display: bravo
114-
value:
115-
shell: printf bravo
116-
e.echo:
117-
check:
118-
text: Select the components that are affected
119-
separator: ", "
120-
options:
121-
alpha:
122-
display: alpha
123-
value:
124-
static: alpha
125-
bravo:
126-
display: bravo
127-
value:
128-
shell: printf bravo
129-
f.foxtrot:
130-
env: "FOXTROT"
66+
zero:
67+
content:
68+
inline: |-
69+
{{ a.alpha }}
70+
values:
71+
a.alpha:
72+
static: ALPHA
73+
one:
74+
content:
75+
file: ./.complate/templates/arbitraty-template-file.tpl
76+
values:
77+
a.pwd:
78+
env: "PWD"
79+
two:
80+
content:
81+
inline: |-
82+
{{ a.alpha }}
83+
{{ b.bravo }}
84+
{{ c.charlie }}
85+
{{ d.delta }}
86+
{{ e.echo }}
87+
values:
88+
a.alpha:
89+
prompt: "alpha"
90+
b.bravo:
91+
shell: "printf bravo"
92+
c.charlie:
93+
static: "charlie"
94+
d.delta:
95+
select:
96+
text: Select the version level that shall be incremented
97+
options:
98+
alpha:
99+
display: alpha
100+
value:
101+
static: alpha
102+
bravo:
103+
display: bravo
104+
value:
105+
shell: printf bravo
106+
e.echo:
107+
check:
108+
text: Select the components that are affected
109+
separator: ", "
110+
options:
111+
alpha:
112+
display: alpha
113+
value:
114+
static: alpha
115+
bravo:
116+
display: bravo
117+
value:
118+
shell: printf bravo
119+
f.foxtrot:
120+
env: "FOXTROT"
121+
three:
122+
content:
123+
inline: |-
124+
{{ _decode "dGVzdA==" }}
125+
helpers:
126+
"_decode":
127+
shell: |-
128+
printf "$(printf $VALUE | base64 -D)"
129+
values: {}
130+
131131
"###
132132
.to_owned()
133133
}

src/render/mod.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub async fn render(
1414
args: &crate::args::RenderArguments,
1515
template: &str,
1616
values: &BTreeMap<String, String>,
17+
helpers: &BTreeMap<String, crate::config::Helper>,
1718
) -> Result<String, Box<dyn std::error::Error>> {
1819
fn recursive_add(
1920
namespace: &mut std::collections::VecDeque<String>,
@@ -52,6 +53,35 @@ pub async fn render(
5253
if args.strict {
5354
hb.set_strict_mode(true);
5455
}
56+
57+
if args.helpers {
58+
for helper in helpers {
59+
let h_func = move |h: &handlebars::Helper,
60+
_: &handlebars::Handlebars,
61+
_: &handlebars::Context,
62+
_: &mut handlebars::RenderContext,
63+
out: &mut dyn handlebars::Output|
64+
-> handlebars::HelperResult {
65+
let param = h.param(0).unwrap();
66+
// dbg!(param);
67+
let cmd = helper.1.shell.to_owned();
68+
69+
let output = std::process::Command::new("sh")
70+
.arg("-c")
71+
.arg(cmd)
72+
.env("VALUE", param.value().as_str().unwrap())
73+
.output()?;
74+
if output.status.code().unwrap() != 0 {
75+
return Err(handlebars::RenderError::new("failed to get command status"));
76+
}
77+
78+
out.write(String::from_utf8(output.stdout).unwrap().as_str())?;
79+
Ok(())
80+
};
81+
hb.register_helper(helper.0, Box::new(h_func))
82+
}
83+
}
84+
5585
let rendered_template = hb.render_template(template, &values_json).unwrap();
5686
Ok(rendered_template)
5787
}
@@ -120,7 +150,7 @@ pub async fn select_and_render(
120150
&invoke_options.backend,
121151
)
122152
.await?;
123-
render(&invoke_options, &template_str, &values).await
153+
render(&invoke_options, &template_str, &values, &template.helpers).await
124154
}
125155

126156
#[async_trait]

test/config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: 0.10
1+
version: 0.11
22
templates:
33
test:
44
content:

0 commit comments

Comments
 (0)