Skip to content

Commit

Permalink
Add envtpl as shorthand for -f j2
Browse files Browse the repository at this point in the history
  • Loading branch information
m-o-e committed Jan 7, 2023
1 parent 00750c8 commit 488e238
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 29 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/release_notes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ wget https://github.com/busyloop/envcat/releases/download/v${VERSION}/envcat-${V
chmod +x envcat-${VERSION}.darwin-x86_64
sudo mv envcat-${VERSION}.darwin-x86_64 /usr/local/bin
sudo ln -sf /usr/local/bin/envcat-${VERSION}.darwin-x86_64 /usr/local/bin/envcat
sudo ln -sf /usr/local/bin/envcat-${VERSION}.darwin-x86_64 /usr/local/bin/envtpl
\`\`\`
## Linux
Expand All @@ -23,6 +24,7 @@ wget https://github.com/busyloop/envcat/releases/download/v${VERSION}/envcat-${V
chmod +x envcat-${VERSION}.linux-x86_64
sudo mv envcat-${VERSION}.linux-x86_64 /usr/bin
sudo ln -sf /usr/bin/envcat-${VERSION}.linux-x86_64 /usr/bin/envcat
sudo ln -sf /usr/bin/envcat-${VERSION}.linux-x86_64 /usr/bin/envtpl
\`\`\`
Expand Down
60 changes: 45 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ Do not edit this file. Edit 'docs/templates/README.md.j2' instead and run 'make

| OS | Arch | Version | |
| ------------ | ------- | --------------------- | ---- |
| OSX (Darwin) | x86_64 | 1.0.0 (latest) | [Download](https://github.com/busyloop/envcat/releases/tag/v1.0.0) |
| Linux | x86_64 | 1.0.0 (latest) | [Download](https://github.com/busyloop/envcat/releases/tag/v1.0.0) |
| Linux | aarch64 | 1.0.0 (latest) | [Download](https://github.com/busyloop/envcat/releases/tag/v1.0.0) |
| OSX (Darwin) | x86_64 | 1.0.1 (latest) | [Download](https://github.com/busyloop/envcat/releases/tag/v1.0.1) |
| Linux | x86_64 | 1.0.1 (latest) | [Download](https://github.com/busyloop/envcat/releases/tag/v1.0.1) |
| Linux | aarch64 | 1.0.1 (latest) | [Download](https://github.com/busyloop/envcat/releases/tag/v1.0.1) |

#### Dockerfile

See the [download page](https://github.com/busyloop/envcat/releases/tag/v1.0.0) for an example Dockerfile. :whale:
See the [download page](https://github.com/busyloop/envcat/releases/tag/v1.0.1) for an example Dockerfile. :whale:


## Usage
Expand Down Expand Up @@ -60,7 +60,7 @@ echo "{{BIND}}:{{PORT | default('443')}} {{NAME}}" | envcat -f j2 -c PORT:?port

## Templating

With `-f j2` envcat renders a jinja2 template from _stdin_ to _stdout_.
With `-f j2`, or when called by the name `envtpl`, envcat renders a jinja2 template from _stdin_ to _stdout_.
Environment variables are available as `{{VAR}}`.

envcat will abort with code 5 if your template references an undefined variable,
Expand All @@ -78,15 +78,49 @@ unset NOPE
echo "{{FOO}}" | envcat -f j2 FOO # => a,b,c
echo "{{NOPE | default('empty')}}" | envcat -f j2 NOPE # => empty
echo "{% for x in FOO | split(',') %}{{x}}{% endfor %}" | envcat -f j2 FOO # => abc
echo "{% if FOO == 'd,e,f' %}A{% else %}B{% endif %}" | envcat -f j2 FOO # => B
echo "{% if BAR | int + 1 == 42 %}yes{% endif %}" | envcat -f j2 BAR # => yes
echo "{% if FOO == 'd,e,f' %}A{% else %}B{% endif %}" | envtpl FOO # => B
echo "{% if BAR | int + 1 == 42 %}yes{% endif %}" | envtpl BAR # => yes
```

If you need more, please consult the [jinja2 documentation](https://jinja.palletsprojects.com/en/2.11.x/templates/).

**Note:**
There are some [subtle differences](https://straight-shoota.github.io/crinja/#:~:text=Differences%20from%20Jinja2) between [the jinja2 library used in envcat](https://straight-shoota.github.io/crinja/) and the original Python jinja2.
But likely none that you will encounter in normal usage.
## Template syntax

Envcat supports most jinja2 syntax and [builtin filters](https://jinja.palletsprojects.com/en/2.11.x/templates/#list-of-builtin-filters).

On top it provides the following additional filters:

#### b64encode, b64encode_urlsafe

```bash
export FOO="hello? world?"

# b64encode, b64encode_urlsafe
echo "{{FOO | b64encode}}" | envtpl FOO # => aGVsbG8/IHdvcmxkPw==
echo "{{FOO | b64encode_urlsafe}}" | envtpl FOO # => aGVsbG8_IHdvcmxkPw==
```

#### b64decode

```bash
export B64_REGULAR="aGVsbG8/IHdvcmxkPw=="
export B64_URLSAFE="aGVsbG8_IHdvcmxkPw=="

echo "{{B64_REGULAR | b64decode}}" | envtpl 'B*' # => hello? world?
echo "{{B64_URLSAFE | b64decode}}" | envtpl 'B*' # => hello? world?
```

#### split


```bash
export FOO=a,b,c

echo "{% for x in FOO | split(',') %}{{x}}..{% endfor %}" | envtpl FOO # => a..b..c..
```

**Note:**
Envcat uses a [Crystal implementation of the jinja2 template engine](https://straight-shoota.github.io/crinja/).
Python expressions are **not** supported.


## Checks
Expand Down Expand Up @@ -210,7 +244,3 @@ $ envcat -f json VARS_ETF:etf A B C
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request

## Contributors

- [moe](https://github.com/m-o-e) - creator and maintainer
52 changes: 41 additions & 11 deletions docs/templates/README.md.j2
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ echo "{{BIND}}:{{PORT | default('443')}} {{NAME}}" | envcat -f j2 -c PORT:?port

## Templating

With `-f j2` envcat renders a jinja2 template from _stdin_ to _stdout_.
With `-f j2`, or when called by the name `envtpl`, envcat renders a jinja2 template from _stdin_ to _stdout_.
Environment variables are available as `{{VAR}}`.

envcat will abort with code 5 if your template references an undefined variable,
Expand All @@ -81,15 +81,49 @@ unset NOPE
echo "{{FOO}}" | envcat -f j2 FOO # => a,b,c
echo "{{NOPE | default('empty')}}" | envcat -f j2 NOPE # => empty
echo "{% for x in FOO | split(',') %}{{x}}{% endfor %}" | envcat -f j2 FOO # => abc
echo "{% if FOO == 'd,e,f' %}A{% else %}B{% endif %}" | envcat -f j2 FOO # => B
echo "{% if BAR | int + 1 == 42 %}yes{% endif %}" | envcat -f j2 BAR # => yes
echo "{% if FOO == 'd,e,f' %}A{% else %}B{% endif %}" | envtpl FOO # => B
echo "{% if BAR | int + 1 == 42 %}yes{% endif %}" | envtpl BAR # => yes
```

If you need more, please consult the [jinja2 documentation](https://jinja.palletsprojects.com/en/2.11.x/templates/).

**Note:**
There are some [subtle differences](https://straight-shoota.github.io/crinja/#:~:text=Differences%20from%20Jinja2) between [the jinja2 library used in envcat](https://straight-shoota.github.io/crinja/) and the original Python jinja2.
But likely none that you will encounter in normal usage.
## Template syntax

Envcat supports most jinja2 syntax and [builtin filters](https://jinja.palletsprojects.com/en/2.11.x/templates/#list-of-builtin-filters).

On top it provides the following additional filters:

#### b64encode, b64encode_urlsafe

```bash
export FOO="hello? world?"

# b64encode, b64encode_urlsafe
echo "{{FOO | b64encode}}" | envtpl FOO # => aGVsbG8/IHdvcmxkPw==
echo "{{FOO | b64encode_urlsafe}}" | envtpl FOO # => aGVsbG8_IHdvcmxkPw==
```

#### b64decode

```bash
export B64_REGULAR="aGVsbG8/IHdvcmxkPw=="
export B64_URLSAFE="aGVsbG8_IHdvcmxkPw=="

echo "{{B64_REGULAR | b64decode}}" | envtpl 'B*' # => hello? world?
echo "{{B64_URLSAFE | b64decode}}" | envtpl 'B*' # => hello? world?
```

#### split


```bash
export FOO=a,b,c

echo "{% for x in FOO | split(',') %}{{x}}..{% endfor %}" | envtpl FOO # => a..b..c..
```

**Note:**
Envcat uses a [Crystal implementation of the jinja2 template engine](https://straight-shoota.github.io/crinja/).
Python expressions are **not** supported.


## Checks
Expand Down Expand Up @@ -161,8 +195,4 @@ $ envcat -f json VARS_ETF:etf A B C
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request

## Contributors

- [moe](https://github.com/m-o-e) - creator and maintainer
{% endraw %}
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: envcat
version: 1.0.0
version: 1.0.1

authors:
- moe <[email protected]>
Expand Down
8 changes: 6 additions & 2 deletions src/envcat/cli.cr
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ class Envcat::Cli
Toka.mapping({
format: {
type: String,
default: Format::DEFAULT,
default: Envcat::Cli.default_format,
value_name: "FORMAT",
description: Format.keys.sort!.join("|") + " (default: #{Format::DEFAULT})",
description: Format.keys.sort!.join("|") + " (default: #{Envcat::Cli.default_format})",
},
check: {
type: Array(String),
Expand All @@ -53,6 +53,10 @@ class Envcat::Cli
help: false,
})

def self.default_format
PROGRAM_NAME.try &.includes?("envtpl") ? "j2" : Format::DEFAULT
end

def self.help
String.build(4096) do |s|
s.puts "FORMAT"
Expand Down

0 comments on commit 488e238

Please sign in to comment.