diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..19fa74c --- /dev/null +++ b/.github/workflows/build.yml @@ -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 diff --git a/.gitignore b/.gitignore index 272009d..f31e01c 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/src/cli_args.rs b/src/cli_args.rs index f7ab346..e78aabd 100644 --- a/src/cli_args.rs +++ b/src/cli_args.rs @@ -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)] @@ -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, - - /// 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), } diff --git a/src/main.rs b/src/main.rs index 7f36f89..52c9471 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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}; @@ -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)?; } } diff --git a/src/template/cli_args.rs b/src/template/cli_args.rs new file mode 100644 index 0000000..7075018 --- /dev/null +++ b/src/template/cli_args.rs @@ -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, + + /// 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, +} diff --git a/src/template.rs b/src/template/mod.rs similarity index 98% rename from src/template.rs rename to src/template/mod.rs index cd0d694..9f89fa9 100644 --- a/src/template.rs +++ b/src/template/mod.rs @@ -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:?}"))] @@ -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, })?; diff --git a/tests/resources/properties/security_from_env.properties b/tests/resources/properties/security_from_env.properties deleted file mode 100644 index 9633d58..0000000 --- a/tests/resources/properties/security_from_env.properties +++ /dev/null @@ -1,4 +0,0 @@ -networkaddress.cache.negative.ttl=0 -networkaddress.cache.ttl=foo -meine.suß.Pröpertie=42 -example-password=admin-pw\=\ withSpace$%"'\ &&}\ § diff --git a/tests/resources/properties/security_from_file.properties b/tests/resources/properties/security_from_file.properties deleted file mode 100644 index 508c1b2..0000000 --- a/tests/resources/properties/security_from_file.properties +++ /dev/null @@ -1,3 +0,0 @@ -networkaddress.cache.negative.ttl=0 -networkaddress.cache.ttl=nixos -meine.suß.Pröpertie=42,3 diff --git a/tests/resources/properties/security_from_file.properties.expected b/tests/resources/properties/security_from_file.properties.expected deleted file mode 100644 index 508c1b2..0000000 --- a/tests/resources/properties/security_from_file.properties.expected +++ /dev/null @@ -1,3 +0,0 @@ -networkaddress.cache.negative.ttl=0 -networkaddress.cache.ttl=nixos -meine.suß.Pröpertie=42,3 diff --git a/tests/resources/properties/security_from_file.properties.in b/tests/resources/properties/security_from_file.properties.in deleted file mode 100644 index 6290272..0000000 --- a/tests/resources/properties/security_from_file.properties.in +++ /dev/null @@ -1,3 +0,0 @@ -networkaddress.cache.negative.ttl=0 -networkaddress.cache.ttl=${file:UTF-8:/etc/hostname} -meine.suß.Pröpertie=42,3 diff --git a/tests/resources/properties/security_from_nested.properties b/tests/resources/properties/security_from_nested.properties deleted file mode 100644 index acf8cbf..0000000 --- a/tests/resources/properties/security_from_nested.properties +++ /dev/null @@ -1,4 +0,0 @@ -networkaddress.cache.negative.ttl=0 -networkaddress.cache.ttl=42 -meine.suß.Pröpertie=42 -example-password=admin-pw\=\ withSpace$%"'\ &&}\ § diff --git a/tests/resources/properties/security_untouched.properties b/tests/resources/properties/security_untouched.properties deleted file mode 100644 index be2a474..0000000 --- a/tests/resources/properties/security_untouched.properties +++ /dev/null @@ -1,3 +0,0 @@ -networkaddress.cache.negative.ttl=0 -networkaddress.cache.ttl=${FOO},${dont:replace} -meine.suß.Pröpertie=42,3 diff --git a/tests/resources/xml/nifi_ldap.xml b/tests/resources/xml/nifi_ldap.xml deleted file mode 100644 index 5970509..0000000 --- a/tests/resources/xml/nifi_ldap.xml +++ /dev/null @@ -1,32 +0,0 @@ - - - - login-identity-provider - org.apache.nifi.ldap.LdapProvider - LDAPS - - example user - admin-pw= withSpace$%"' &&} § - - THROW - 10 secs - 10 secs - - ldaps://openldap.kuttl-test-tidy-asp.svc.cluster.local:1636 - ou=my users,dc=example,dc=org - uid={0} - - NONE - /stackable/server_tls/keystore.p12 - secret - PKCS12 - /stackable/server_tls/truststore.p12 - secret - PKCS12 - TLSv1.2 - true - - USE_DN - 7 days - - diff --git a/tests/templating.rs b/tests/templating.rs index b9c9822..0d0f648 100644 --- a/tests/templating.rs +++ b/tests/templating.rs @@ -2,7 +2,7 @@ use std::{ env, fs::{self, File}, io::Write, - path::PathBuf, + path::{Path, PathBuf}, }; use rstest::rstest; @@ -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");