Skip to content

Implement feedback from #1 #4

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

Merged
merged 6 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
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
92 changes: 92 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# =============
# This file is copied (and adopted) from the templates in stackabletech/operator-templating
# =============
---
name: Stackable Build Pipeline

on:
push:
branches:
- main
- staging
- trying
- "renovate/**"
pull_request:
merge_group:

env:
CARGO_TERM_COLOR: always
CARGO_INCREMENTAL: '0'
CARGO_PROFILE_DEV_DEBUG: '0'
RUST_TOOLCHAIN_VERSION: "1.78.0"
RUSTFLAGS: "-D warnings"
RUSTDOCFLAGS: "-D warnings"
RUST_LOG: "info"
DEV_REPO_HELM_URL: https://repo.stackable.tech/repository/helm-dev
TEST_REPO_HELM_URL: https://repo.stackable.tech/repository/helm-test
STABLE_REPO_HELM_URL: https://repo.stackable.tech/repository/helm-stable

jobs:
run_rustfmt:
name: Run Rustfmt
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@44c2b7a8a4ea60a981eaca3cf939b5f4305c123b # v4.1.5
with:
submodules: recursive
- uses: dtolnay/rust-toolchain@d8352f6b1d2e870bc5716e7a6d9b65c4cc244a1a
with:
toolchain: ${{ env.RUST_TOOLCHAIN_VERSION }}
components: rustfmt
- run: cargo fmt --all -- --check

run_clippy:
name: Run Clippy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@44c2b7a8a4ea60a981eaca3cf939b5f4305c123b # v4.1.5
with:
submodules: recursive
- uses: dtolnay/rust-toolchain@d8352f6b1d2e870bc5716e7a6d9b65c4cc244a1a
with:
toolchain: ${{ env.RUST_TOOLCHAIN_VERSION }}
components: clippy
- name: Run clippy action to produce annotations
uses: giraffate/clippy-action@13b9d32482f25d29ead141b79e7e04e7900281e0 # v1.0.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
if: env.GITHUB_TOKEN != null
with:
clippy_flags: --all-targets -- -D warnings
reporter: 'github-pr-review'
github_token: ${{ secrets.GITHUB_TOKEN }}
- name: Run clippy manually without annotations
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
if: env.GITHUB_TOKEN == null
run: cargo clippy --color never -q --all-targets -- -D warnings

run_rustdoc:
name: Run RustDoc
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@44c2b7a8a4ea60a981eaca3cf939b5f4305c123b # v4.1.5
with:
submodules: recursive
- uses: dtolnay/rust-toolchain@d8352f6b1d2e870bc5716e7a6d9b65c4cc244a1a
with:
toolchain: ${{ env.RUST_TOOLCHAIN_VERSION }}
components: rustfmt
- run: cargo doc --document-private-items

run_tests:
name: Run Cargo Tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@44c2b7a8a4ea60a981eaca3cf939b5f4305c123b # v4.1.5
with:
submodules: recursive
- uses: dtolnay/rust-toolchain@d8352f6b1d2e870bc5716e7a6d9b65c4cc244a1a
with:
toolchain: ${{ env.RUST_TOOLCHAIN_VERSION }}
- run: cargo test
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
# The following files are mentioned as an example in the README, so let's exclude them
example.xml
example-password

tests/resources/properties/*.properties
tests/resources/xml/*.xml
21 changes: 2 additions & 19 deletions src/cli_args.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::path::PathBuf;

use clap::{Parser, Subcommand};

use config_utils::file_types::FileType;
use config_utils::template::cli_args::TemplateCommand;

/// Utility that helps you handling config files.
#[derive(Debug, Parser)]
Expand All @@ -14,20 +12,5 @@ pub struct Args {

#[derive(Debug, Subcommand)]
pub enum Command {
/// Fill out variables in config files from either env variables or files directly.
Template {
/// The path to the file that should be templated
file: PathBuf,

/// The optional file type of the file to be templated. If this is not specified this utility will try to infer
/// the type based on the file name.
#[arg(value_enum)]
file_type: Option<FileType>,

/// By default inserted values are automatically escaped according to the deteced file format. You can disable
/// this, e.g. when you need to insert XML tags (as they otherwise would be escaped).
/// NOTE: Please make sure to correctly escape the inserted text on your own!
#[clap(long)]
dont_escape: bool,
},
Template(TemplateCommand),
}
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clap::Parser;
use config_utils::template::{self, template};
use config_utils::template::{self, cli_args::TemplateCommand, template};
use snafu::{ResultExt, Snafu};

use cli_args::{Args, Command};
Expand All @@ -19,11 +19,11 @@ fn main() -> Result<()> {
let args = Args::parse();

match args.command {
Command::Template {
Command::Template(TemplateCommand {
file,
file_type,
dont_escape,
} => {
}) => {
template(&file, file_type.as_ref(), !dont_escape).context(TemplateFileSnafu)?;
}
}
Expand Down
23 changes: 23 additions & 0 deletions src/template/cli_args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use std::path::PathBuf;

use clap::Parser;

use crate::file_types::FileType;

/// Fill out variables in config files from either env variables or files directly.
#[derive(Debug, Parser)]
pub struct TemplateCommand {
/// The path to the file that should be templated
pub file: PathBuf,

/// The optional file type of the file to be templated. If this is not specified this utility will try to infer
/// the type based on the file name.
#[arg(value_enum)]
pub file_type: Option<FileType>,

/// By default inserted values are automatically escaped according to the deteced file format. You can disable
/// this, e.g. when you need to insert XML tags (as they otherwise would be escaped).
/// NOTE: Please make sure to correctly escape the inserted text on your own!
#[clap(long)]
pub dont_escape: bool,
}
4 changes: 3 additions & 1 deletion src/template.rs → src/template/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use crate::{
ENV_VAR_PATTERN_END, ENV_VAR_PATTERN_START, FILE_PATTERN_END, FILE_PATTERN_START,
};

pub mod cli_args;

#[derive(Debug, Snafu)]
pub enum Error {
#[snafu(display("Could not read file {file_name:?}"))]
Expand Down Expand Up @@ -125,7 +127,7 @@ pub fn template(file_name: &PathBuf, file_type: Option<&FileType>, escape: bool)
})?;
}

fs::rename(&tmp_file_name, &file_name).context(RenameTemporaryFileSnafu {
fs::rename(&tmp_file_name, file_name).context(RenameTemporaryFileSnafu {
tmp_file_name,
destination_file_name: file_name,
})?;
Expand Down
4 changes: 0 additions & 4 deletions tests/resources/properties/security_from_env.properties

This file was deleted.

3 changes: 0 additions & 3 deletions tests/resources/properties/security_from_file.properties

This file was deleted.

This file was deleted.

3 changes: 0 additions & 3 deletions tests/resources/properties/security_from_file.properties.in

This file was deleted.

4 changes: 0 additions & 4 deletions tests/resources/properties/security_from_nested.properties

This file was deleted.

3 changes: 0 additions & 3 deletions tests/resources/properties/security_untouched.properties

This file was deleted.

32 changes: 0 additions & 32 deletions tests/resources/xml/nifi_ldap.xml

This file was deleted.

8 changes: 4 additions & 4 deletions tests/templating.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{
env,
fs::{self, File},
io::Write,
path::PathBuf,
path::{Path, PathBuf},
};

use rstest::rstest;
Expand All @@ -21,13 +21,13 @@ fn test_file_templating(#[files("tests/resources/**/*.in")] test_file_in: PathBu
fs::copy(&test_file_in, &test_file).unwrap();
template(&test_file, None, true).unwrap();

let actual = fs::read_to_string(&test_file).unwrap();
let expected = fs::read_to_string(&test_file_expected).unwrap();
let actual = fs::read_to_string(test_file).unwrap();
let expected = fs::read_to_string(test_file_expected).unwrap();

similar_asserts::assert_eq!(actual, expected);
}

fn set_example_envs(example_dir: &PathBuf) {
fn set_example_envs(example_dir: &Path) {
// SAFETY: We only use a single thread to set this env vars
env::set_var("ENV_TEST", "foo");
env::set_var("ENV_TEST_USERNAME", "example user");
Expand Down
Loading