├── .github ├── ISSUE_TEMPLATE │ └── release-checklist.md ├── dependabot.yml └── workflows │ ├── require-release-note.yml │ └── rust.yml ├── .gitignore ├── COPYRIGHT ├── Cargo.toml ├── LICENSE-APACHE-2.0 ├── LICENSE-MIT ├── README.md ├── docs ├── development.md └── release-notes.md └── src └── lib.rs /.github/ISSUE_TEMPLATE/release-checklist.md: -------------------------------------------------------------------------------- 1 | --- 2 | # Template generated by https://github.com/coreos/repo-templates; do not edit downstream 3 | --- 4 | 5 | # Release process 6 | 7 | This project uses [cargo-release][cargo-release] in order to prepare new releases, tag and sign the relevant git commit, and publish the resulting artifacts to [crates.io][crates-io]. 8 | The release process follows the usual PR-and-review flow, allowing an external reviewer to have a final check before publishing. 9 | 10 | ## Requirements 11 | 12 | This guide requires: 13 | 14 | * A web browser (and network connectivity) 15 | * `git` 16 | * [GPG setup][GPG setup] and personal key for signing 17 | * `cargo` (suggested: latest stable toolchain from [rustup][rustup]) 18 | * `cargo-release` (suggested: `cargo install -f cargo-release`) 19 | * Write access to this GitHub project 20 | * A verified account on crates.io 21 | * Membership in the [Fedora CoreOS Crates Owners group](https://github.com/orgs/coreos/teams/fedora-coreos-crates-owners/members), which will give you upload access to crates.io 22 | 23 | ## Release checklist 24 | 25 | These steps show how to release version `x.y.z` on the `origin` remote (this can be checked via `git remote -av`). 26 | Push access to the upstream repository is required in order to publish the new tag and the PR branch. 27 | 28 | :warning:: if `origin` is not the name of the locally configured remote that points to the upstream git repository (i.e. `git@github.com:coreos/envsubst-rs.git`), be sure to assign the correct remote name to the `UPSTREAM_REMOTE` variable. 29 | 30 | - prepare environment: 31 | - [ ] `RELEASE_VER=x.y.z` 32 | - [ ] `UPSTREAM_REMOTE=origin` 33 | - [ ] `git checkout -b pre-release-${RELEASE_VER}` 34 | 35 | - check `Cargo.toml` for unintended increases of lower version bounds: 36 | - [ ] `git diff $(git describe --abbrev=0) Cargo.toml` 37 | 38 | - write release notes: 39 | - [ ] write release notes in `docs/release-notes.md` 40 | - [ ] `git add docs/release-notes.md && git commit -m "docs/release-notes: update for release ${RELEASE_VER}"` 41 | 42 | - land the changes: 43 | - [ ] PR the changes, get them reviewed, approved and merged 44 | - [ ] if doing a branched release, also include a PR to merge the `docs/release-notes.md` changes into main 45 | 46 | - make sure the project is clean: 47 | - [ ] Make sure `cargo-release` is up to date: `cargo install cargo-release` 48 | - [ ] `git checkout main && git pull ${UPSTREAM_REMOTE} main` 49 | - [ ] `cargo test --all-features` 50 | - [ ] `cargo clean` 51 | - [ ] `git clean -fd` 52 | 53 | - create release commit on a dedicated branch and tag it (the commit and tag will be signed with the GPG signing key you configured): 54 | - [ ] `git checkout -b release-${RELEASE_VER}` 55 | - [ ] `cargo release --execute ${RELEASE_VER}` (and confirm the version when prompted) 56 | 57 | - open and merge a PR for this release: 58 | - [ ] `git push ${UPSTREAM_REMOTE} release-${RELEASE_VER}` 59 | - [ ] open a web browser and create a PR for the branch above 60 | - [ ] make sure the resulting PR contains exactly one commit 61 | - [ ] get the PR reviewed, approved and merged 62 | 63 | - publish the artifacts (tag and crate): 64 | - [ ] `git checkout v${RELEASE_VER}` 65 | - [ ] verify that `grep "^version = \"${RELEASE_VER}\"$" Cargo.toml` produces output 66 | - [ ] `git push ${UPSTREAM_REMOTE} v${RELEASE_VER}` 67 | - [ ] `cargo publish` 68 | 69 | - publish this release on GitHub: 70 | - [ ] find the new tag in the [GitHub tag list](https://github.com/coreos/envsubst-rs/tags), click the triple dots menu, and create a release for it 71 | - [ ] copy in the changelog from the release notes doc 72 | - [ ] publish release 73 | 74 | - clean up the local environment (optional, but recommended): 75 | - [ ] `cargo clean` 76 | - [ ] `git checkout main` 77 | - [ ] `git pull ${UPSTREAM_REMOTE} main` 78 | - [ ] `git push ${UPSTREAM_REMOTE} :pre-release-${RELEASE_VER} :release-${RELEASE_VER}` 79 | - [ ] `git branch -d pre-release-${RELEASE_VER} release-${RELEASE_VER}` 80 | 81 | - Fedora packaging: 82 | - [ ] update the `rust-envsubst` spec file in [Fedora](https://src.fedoraproject.org/rpms/rust-envsubst) 83 | - bump the `Version` 84 | - switch the `Release` back to `1%{?dist}` 85 | - remove any patches obsoleted by the new release 86 | - update changelog 87 | - [ ] run `spectool -g -S rust-envsubst.spec` 88 | - [ ] run `kinit your_fas_account@FEDORAPROJECT.ORG` 89 | - [ ] run `fedpkg new-sources $(spectool -S rust-envsubst.spec | sed 's:.*/::')` 90 | - [ ] PR the changes in [Fedora](https://src.fedoraproject.org/rpms/rust-envsubst) 91 | - [ ] once the PR merges to rawhide, merge rawhide into the other relevant branches (e.g. f39) then push those, for example: 92 | ```bash 93 | git checkout rawhide 94 | git pull --ff-only 95 | git checkout f39 96 | git merge --ff-only rawhide 97 | git push origin f39 98 | ``` 99 | - [ ] on each of those branches run `fedpkg build` 100 | - [ ] once the builds have finished, submit them to [bodhi](https://bodhi.fedoraproject.org/updates/new), filling in: 101 | - `rust-envsubst` for `Packages` 102 | - selecting the build(s) that just completed, except for the rawhide one (which gets submitted automatically) 103 | - writing brief release notes like "New upstream release; see release notes at `link to GitHub release`" 104 | - leave `Update name` blank 105 | - `Type`, `Severity` and `Suggestion` can be left as `unspecified` unless it is a security release. In that case select `security` with the appropriate severity. 106 | - `Stable karma` and `Unstable` karma can be set to `2` and `-1`, respectively. 107 | 108 | [cargo-release]: https://github.com/sunng87/cargo-release 109 | [rustup]: https://rustup.rs/ 110 | [crates-io]: https://crates.io/ 111 | [GPG setup]: https://docs.github.com/en/github/authenticating-to-github/managing-commit-signature-verification 112 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # Maintained in https://github.com/coreos/repo-templates 2 | # Do not edit downstream. 3 | 4 | version: 2 5 | updates: 6 | - package-ecosystem: cargo 7 | directory: / 8 | schedule: 9 | interval: monthly 10 | open-pull-requests-limit: 10 11 | labels: 12 | - dependency 13 | - skip-notes 14 | -------------------------------------------------------------------------------- /.github/workflows/require-release-note.yml: -------------------------------------------------------------------------------- 1 | # Maintained in https://github.com/coreos/repo-templates 2 | # Do not edit downstream. 3 | 4 | name: Release notes 5 | 6 | on: 7 | pull_request: 8 | branches: [main] 9 | types: [opened, synchronize, reopened, labeled, unlabeled] 10 | 11 | permissions: 12 | contents: read 13 | 14 | concurrency: 15 | group: release-note-${{ github.ref }} 16 | cancel-in-progress: true 17 | 18 | jobs: 19 | require-notes: 20 | name: Require release note 21 | runs-on: ubuntu-latest 22 | steps: 23 | - name: Require release-notes.md update 24 | uses: coreos/actions-lib/require-file-change@main 25 | with: 26 | path: docs/release-notes.md 27 | override-label: skip-notes 28 | -------------------------------------------------------------------------------- /.github/workflows/rust.yml: -------------------------------------------------------------------------------- 1 | # Maintained in https://github.com/coreos/repo-templates 2 | # Do not edit downstream. 3 | 4 | name: Rust 5 | on: 6 | push: 7 | branches: [main] 8 | pull_request: 9 | branches: [main] 10 | permissions: 11 | contents: read 12 | 13 | # don't waste job slots on superseded code 14 | concurrency: 15 | group: ${{ github.workflow }}-${{ github.ref }} 16 | cancel-in-progress: true 17 | 18 | env: 19 | CARGO_TERM_COLOR: always 20 | # Pinned toolchain for linting 21 | ACTIONS_LINTS_TOOLCHAIN: 1.71.0 22 | 23 | jobs: 24 | tests-stable: 25 | name: Tests, stable toolchain 26 | runs-on: ubuntu-latest 27 | steps: 28 | - name: Check out repository 29 | uses: actions/checkout@v3 30 | - name: Install toolchain 31 | uses: dtolnay/rust-toolchain@v1 32 | with: 33 | toolchain: stable 34 | - name: Cache build artifacts 35 | uses: Swatinem/rust-cache@v2 36 | - name: cargo build 37 | run: cargo build --all-targets 38 | - name: cargo test 39 | run: cargo test --all-targets 40 | tests-release-stable: 41 | name: Tests (release), stable toolchain 42 | runs-on: ubuntu-latest 43 | steps: 44 | - name: Check out repository 45 | uses: actions/checkout@v3 46 | - name: Install toolchain 47 | uses: dtolnay/rust-toolchain@v1 48 | with: 49 | toolchain: stable 50 | - name: Cache build artifacts 51 | uses: Swatinem/rust-cache@v2 52 | - name: cargo build (release) 53 | run: cargo build --all-targets --release 54 | - name: cargo test (release) 55 | run: cargo test --all-targets --release 56 | tests-release-msrv: 57 | name: Tests (release), minimum supported toolchain 58 | runs-on: ubuntu-latest 59 | steps: 60 | - name: Check out repository 61 | uses: actions/checkout@v3 62 | - name: Detect crate MSRV 63 | run: | 64 | msrv=$(cargo metadata --format-version 1 --no-deps | \ 65 | jq -r '.packages[0].rust_version') 66 | echo "Crate MSRV: $msrv" 67 | echo "MSRV=$msrv" >> $GITHUB_ENV 68 | - name: Install toolchain 69 | uses: dtolnay/rust-toolchain@v1 70 | with: 71 | toolchain: ${{ env.MSRV }} 72 | - name: Cache build artifacts 73 | uses: Swatinem/rust-cache@v2 74 | - name: cargo build (release) 75 | run: cargo build --all-targets --release 76 | - name: cargo test (release) 77 | run: cargo test --all-targets --release 78 | linting: 79 | name: Lints, pinned toolchain 80 | runs-on: ubuntu-latest 81 | steps: 82 | - name: Check out repository 83 | uses: actions/checkout@v3 84 | - name: Install toolchain 85 | uses: dtolnay/rust-toolchain@v1 86 | with: 87 | toolchain: ${{ env.ACTIONS_LINTS_TOOLCHAIN }} 88 | components: rustfmt, clippy 89 | - name: Cache build artifacts 90 | uses: Swatinem/rust-cache@v2 91 | - name: cargo fmt (check) 92 | run: cargo fmt -- --check -l 93 | - name: cargo clippy (warnings) 94 | run: cargo clippy --all-targets -- -D warnings 95 | tests-other-channels: 96 | name: Tests, unstable toolchain 97 | runs-on: ubuntu-latest 98 | continue-on-error: true 99 | strategy: 100 | matrix: 101 | channel: [beta, nightly] 102 | steps: 103 | - name: Check out repository 104 | uses: actions/checkout@v3 105 | - name: Install toolchain 106 | uses: dtolnay/rust-toolchain@v1 107 | with: 108 | toolchain: ${{ matrix.channel }} 109 | - name: Cache build artifacts 110 | uses: Swatinem/rust-cache@v2 111 | - name: cargo build 112 | run: cargo build --all-targets 113 | - name: cargo test 114 | run: cargo test --all-targets 115 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | Cargo.lock -------------------------------------------------------------------------------- /COPYRIGHT: -------------------------------------------------------------------------------- 1 | Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ 2 | Upstream-Name: envsubst 3 | Source: https://github.com/coreos/envsubst-rs 4 | 5 | Files: * 6 | Copyright: 2019-2022, Project contributors 7 | License: MIT or Apache-2.0 8 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "envsubst" 3 | version = "0.2.1" 4 | description = "Variables substitution" 5 | license = "MIT OR Apache-2.0" 6 | keywords = ["envsubst", "varsubst"] 7 | authors = ["Luca Bruno "] 8 | repository = "https://github.com/coreos/envsubst-rs" 9 | edition = "2021" 10 | rust-version = "1.56.0" 11 | exclude = [".gitignore", ".github"] 12 | 13 | [dependencies] 14 | thiserror = "^1.0" 15 | 16 | [package.metadata.release] 17 | sign-commit = true 18 | sign-tag = true 19 | publish = false 20 | push = false 21 | pre-release-commit-message = "cargo: envsubst release {{version}}" 22 | tag-message = "envsubst {{version}}" 23 | -------------------------------------------------------------------------------- /LICENSE-APACHE-2.0: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Permission is hereby granted, free of charge, to any person obtaining 2 | a copy of this software and associated documentation files (the 3 | "Software"), to deal in the Software without restriction, including 4 | without limitation the rights to use, copy, modify, merge, publish, 5 | distribute, sublicense, and/or sell copies of the Software, and to 6 | permit persons to whom the Software is furnished to do so, subject to 7 | the following conditions: 8 | 9 | The above copyright notice and this permission notice shall be 10 | included in all copies or substantial portions of the Software. 11 | 12 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 13 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 14 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 15 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 16 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 17 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 18 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # envsubst 2 | 3 | [![crates.io](https://img.shields.io/crates/v/envsubst.svg)](https://crates.io/crates/envsubst) 4 | [![Documentation](https://docs.rs/envsubst/badge.svg)](https://docs.rs/envsubst) 5 | 6 | A simple Rust library for variables substitution. 7 | 8 | This library provide helper functions for string manipulation, 9 | taking values from a context **env**ironment map and **subst**ituting 10 | all matching placeholders. 11 | 12 | Its name and logic is similar to the [`envsubst`] GNU utility, but 13 | this only supports braces-delimited variables (i.e. `${foo}`) and 14 | takes replacement values from an explicit map of variables. 15 | 16 | [`envsubst`]: https://www.gnu.org/software/gettext/manual/html_node/envsubst-Invocation.html 17 | 18 | ## License 19 | 20 | Licensed under either of 21 | 22 | * MIT license - 23 | * Apache License, Version 2.0 - 24 | 25 | at your option. 26 | -------------------------------------------------------------------------------- /docs/development.md: -------------------------------------------------------------------------------- 1 | # Development 2 | 3 | ## Release process 4 | 5 | Releases can be performed by [creating a new release ticket][new-release-ticket] and following the steps in the checklist there. 6 | 7 | [new-release-ticket]: https://github.com/coreos/envsubst-rs/issues/new?template=release-checklist.md 8 | -------------------------------------------------------------------------------- /docs/release-notes.md: -------------------------------------------------------------------------------- 1 | # Release notes 2 | 3 | ## Upcoming envsubst 0.2.2 (unreleased) 4 | 5 | Changes: 6 | 7 | - Require Rust ≥ 1.56.0 8 | - Add release notes doc 9 | 10 | 11 | ## envsubst 0.2.1 (2022-11-29) 12 | 13 | Changes: 14 | 15 | - cargo: update all URLs to new location 16 | - cargo: update cargo-release metadata 17 | - ci: drop unused TravisCI config 18 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //! Variables substitution in string templates. 2 | //! 3 | //! This library provide helper functions for string manipulation, 4 | //! taking values from a context **env**ironment map and **subst**ituting 5 | //! all matching placeholders. 6 | //! 7 | //! Its name and logic is similar to the [`envsubst`] GNU utility, but 8 | //! this only supports braces-delimited variables (i.e. `${foo}`) and 9 | //! takes replacement values from an explicit map of variables. 10 | //! 11 | //! [`envsubst`]: https://www.gnu.org/software/gettext/manual/html_node/envsubst-Invocation.html 12 | //! 13 | //! ## Example 14 | //! 15 | //! ```rust 16 | //! let base_url = "${protocol}://${hostname}/${endpoint}"; 17 | //! assert!(envsubst::is_templated(base_url)); 18 | //! 19 | //! let mut context = std::collections::HashMap::new(); 20 | //! context.insert("protocol".to_string(), "https".to_string()); 21 | //! context.insert("hostname".to_string(), "example.com".to_string()); 22 | //! context.insert("endpoint".to_string(), "login".to_string()); 23 | //! assert!(envsubst::validate_vars(&context).is_ok()); 24 | //! 25 | //! let final_url = envsubst::substitute(base_url, &context).unwrap(); 26 | //! assert!(!envsubst::is_templated(&final_url)); 27 | //! assert_eq!(final_url, "https://example.com/login"); 28 | //! ``` 29 | 30 | #![allow(clippy::implicit_hasher)] 31 | 32 | use std::collections::HashMap; 33 | 34 | /// Library errors. 35 | #[derive(thiserror::Error, Debug)] 36 | #[error("envsubst error: {0}")] 37 | pub struct Error(String); 38 | 39 | /// Substitute variables in a template string. 40 | /// 41 | /// Given an input string `template`, replace tokens of the form `${foo}` with 42 | /// values provided in `variables`. 43 | pub fn substitute(template: T, variables: &HashMap) -> Result 44 | where 45 | T: Into, 46 | { 47 | let mut output = template.into(); 48 | if variables.is_empty() { 49 | return Ok(output); 50 | } 51 | 52 | for (k, v) in variables { 53 | validate(k, "key")?; 54 | validate(v, "value")?; 55 | 56 | let from = format!("${{{}}}", k); 57 | output = output.replace(&from, v) 58 | } 59 | 60 | Ok(output) 61 | } 62 | 63 | /// Check whether input string contains templated variables. 64 | pub fn is_templated(input: S) -> bool 65 | where 66 | S: AsRef, 67 | { 68 | let start = input.as_ref().find("${"); 69 | let end = input.as_ref().find('}'); 70 | 71 | match (start, end) { 72 | (Some(s), Some(e)) => s < e, 73 | _ => false, 74 | } 75 | } 76 | 77 | /// Validate variables for substitution. 78 | /// 79 | /// This check whether substitution variables are valid. In order to make 80 | /// substitution deterministic, the following characters are not allowed 81 | /// within variables names nor values: `$`, `{`, `}`. 82 | pub fn validate_vars(variables: &HashMap) -> Result<(), Error> { 83 | for (k, v) in variables { 84 | validate(k, "key")?; 85 | validate(v, "value")?; 86 | } 87 | Ok(()) 88 | } 89 | 90 | /// Check whether `value` contains invalid characters. 91 | fn validate(value: S, kind: &str) -> Result<(), Error> 92 | where 93 | S: AsRef, 94 | { 95 | let forbidden = &["$", "{", "}"]; 96 | for c in forbidden { 97 | if value.as_ref().contains(c) { 98 | let err_msg = format!( 99 | "variable {} '{}' contains forbidden character '{}'", 100 | kind, 101 | value.as_ref(), 102 | c 103 | ); 104 | return Err(Error(err_msg)); 105 | }; 106 | } 107 | Ok(()) 108 | } 109 | 110 | #[cfg(test)] 111 | mod tests { 112 | use super::*; 113 | use std::collections::HashMap; 114 | 115 | #[test] 116 | fn basic_subst() { 117 | let template = "foo ${VAR} bar"; 118 | let mut env = HashMap::new(); 119 | env.insert("VAR".to_string(), "var".to_string()); 120 | 121 | let out = substitute(template, &env).unwrap(); 122 | let expected = "foo var bar"; 123 | assert_eq!(out, expected); 124 | } 125 | 126 | #[test] 127 | fn template_check() { 128 | let plain = "foo"; 129 | assert!(!is_templated(plain)); 130 | 131 | let template = "foo ${VAR} bar"; 132 | assert!(is_templated(template)); 133 | 134 | let starting = "foo${"; 135 | assert!(!is_templated(starting)); 136 | 137 | let ending = "foo}"; 138 | assert!(!is_templated(ending)); 139 | } 140 | 141 | #[test] 142 | fn basic_empty_vars() { 143 | let template = "foo ${VAR} bar"; 144 | let env = HashMap::new(); 145 | 146 | let out = substitute(template, &env).unwrap(); 147 | assert_eq!(out, template); 148 | } 149 | 150 | #[test] 151 | fn dollar_bracket() { 152 | let template = "foo ${ bar"; 153 | let mut env = HashMap::new(); 154 | env.insert("VAR".to_string(), "var".to_string()); 155 | 156 | let out = substitute(template, &env).unwrap(); 157 | assert_eq!(out, template); 158 | } 159 | 160 | #[test] 161 | fn invalid_vars() { 162 | let template = "foo ${VAR} bar"; 163 | let mut env = HashMap::new(); 164 | env.insert("${VAR}".to_string(), "var".to_string()); 165 | 166 | substitute(template, &env).unwrap_err(); 167 | 168 | let mut env = HashMap::new(); 169 | env.insert("VAR".to_string(), "${VAR}".to_string()); 170 | } 171 | } 172 | --------------------------------------------------------------------------------