Skip to content

Commit 80248c3

Browse files
author
Max Dymond
committed
Add floki render to print out the rendered configuration template.
Signed-off-by: Max Dymond <max.dymond@microsoft.com>
1 parent 40389df commit 80248c3

4 files changed

Lines changed: 22 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Status: Available for use
2020
can use `jsonload(file="<filepath>")` in order to load values.
2121
- Add `tomlload` function to tera templating engine so that floki templates
2222
can use `tomlload(file="<filepath>")` in order to load values.
23+
- Add `floki render` to print out the rendered configuration template.
2324

2425
### Fixed
2526

src/cli.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ pub(crate) enum Subcommand {
2020
#[structopt(name = "SHELL", parse(try_from_str))]
2121
shell: structopt::clap::Shell,
2222
},
23+
24+
/// Render the configuration file to stdout, performing any templating
25+
/// operations.
26+
#[structopt(name = "render")]
27+
Render {},
2328
}
2429

2530
/// Main CLI interface

src/config.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,7 @@ pub fn render_template(template: &str, source_filename: &Path) -> Result<String,
156156
}
157157

158158
impl FlokiConfig {
159-
pub fn from_file(file: &Path) -> Result<Self, Error> {
160-
debug!("Reading configuration file: {:?}", file);
161-
162-
// Read the content from the path
159+
pub fn render(file: &Path) -> Result<String, Error> {
163160
let content = std::fs::read_to_string(file).map_err(|e| {
164161
errors::FlokiError::ProblemOpeningConfigYaml {
165162
name: file.display().to_string(),
@@ -168,7 +165,14 @@ impl FlokiConfig {
168165
})?;
169166

170167
// Render the template first before parsing it.
171-
let output = render_template(&content, file)?;
168+
render_template(&content, file)
169+
}
170+
171+
pub fn from_file(file: &Path) -> Result<Self, Error> {
172+
debug!("Reading configuration file: {:?}", file);
173+
174+
// Render the output from the configuration file before parsing.
175+
let output = Self::render(file)?;
172176

173177
// Parse the rendered floki file from the string.
174178
let mut config: FlokiConfig = serde_yaml::from_str(&output).map_err(|e| {

src/main.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ fn run_floki_from_args(args: &Cli) -> Result<(), Error> {
6363
Ok(())
6464
}
6565

66+
Some(Subcommand::Render {}) => {
67+
let env = Environment::gather(&args.config_file)?;
68+
let contents = FlokiConfig::render(&env.config_file)?;
69+
println!("{contents}");
70+
Ok(())
71+
}
72+
6673
// Launch an interactive floki shell (the default)
6774
None => {
6875
let env = Environment::gather(&args.config_file)?;

0 commit comments

Comments
 (0)