Skip to content

Commit 488e238

Browse files
committed
Add envtpl as shorthand for -f j2
1 parent 00750c8 commit 488e238

File tree

5 files changed

+95
-29
lines changed

5 files changed

+95
-29
lines changed

.github/workflows/release_notes.sh

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ wget https://github.com/busyloop/envcat/releases/download/v${VERSION}/envcat-${V
1414
chmod +x envcat-${VERSION}.darwin-x86_64
1515
sudo mv envcat-${VERSION}.darwin-x86_64 /usr/local/bin
1616
sudo ln -sf /usr/local/bin/envcat-${VERSION}.darwin-x86_64 /usr/local/bin/envcat
17+
sudo ln -sf /usr/local/bin/envcat-${VERSION}.darwin-x86_64 /usr/local/bin/envtpl
1718
\`\`\`
1819
1920
## Linux
@@ -23,6 +24,7 @@ wget https://github.com/busyloop/envcat/releases/download/v${VERSION}/envcat-${V
2324
chmod +x envcat-${VERSION}.linux-x86_64
2425
sudo mv envcat-${VERSION}.linux-x86_64 /usr/bin
2526
sudo ln -sf /usr/bin/envcat-${VERSION}.linux-x86_64 /usr/bin/envcat
27+
sudo ln -sf /usr/bin/envcat-${VERSION}.linux-x86_64 /usr/bin/envtpl
2628
\`\`\`
2729
2830

README.md

+45-15
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ Do not edit this file. Edit 'docs/templates/README.md.j2' instead and run 'make
2626

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

3333
#### Dockerfile
3434

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

3737

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

6161
## Templating
6262

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

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

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

87-
**Note:**
88-
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.
89-
But likely none that you will encounter in normal usage.
86+
## Template syntax
87+
88+
Envcat supports most jinja2 syntax and [builtin filters](https://jinja.palletsprojects.com/en/2.11.x/templates/#list-of-builtin-filters).
89+
90+
On top it provides the following additional filters:
91+
92+
#### b64encode, b64encode_urlsafe
93+
94+
```bash
95+
export FOO="hello? world?"
96+
97+
# b64encode, b64encode_urlsafe
98+
echo "{{FOO | b64encode}}" | envtpl FOO # => aGVsbG8/IHdvcmxkPw==
99+
echo "{{FOO | b64encode_urlsafe}}" | envtpl FOO # => aGVsbG8_IHdvcmxkPw==
100+
```
101+
102+
#### b64decode
103+
104+
```bash
105+
export B64_REGULAR="aGVsbG8/IHdvcmxkPw=="
106+
export B64_URLSAFE="aGVsbG8_IHdvcmxkPw=="
107+
108+
echo "{{B64_REGULAR | b64decode}}" | envtpl 'B*' # => hello? world?
109+
echo "{{B64_URLSAFE | b64decode}}" | envtpl 'B*' # => hello? world?
110+
```
111+
112+
#### split
113+
114+
115+
```bash
116+
export FOO=a,b,c
117+
118+
echo "{% for x in FOO | split(',') %}{{x}}..{% endfor %}" | envtpl FOO # => a..b..c..
119+
```
120+
121+
**Note:**
122+
Envcat uses a [Crystal implementation of the jinja2 template engine](https://straight-shoota.github.io/crinja/).
123+
Python expressions are **not** supported.
90124

91125

92126
## Checks
@@ -210,7 +244,3 @@ $ envcat -f json VARS_ETF:etf A B C
210244
3. Commit your changes (`git commit -am 'Add some feature'`)
211245
4. Push to the branch (`git push origin my-new-feature`)
212246
5. Create a new Pull Request
213-
214-
## Contributors
215-
216-
- [moe](https://github.com/m-o-e) - creator and maintainer

docs/templates/README.md.j2

+41-11
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ echo "{{BIND}}:{{PORT | default('443')}} {{NAME}}" | envcat -f j2 -c PORT:?port
6363

6464
## Templating
6565

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

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

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

90-
**Note:**
91-
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.
92-
But likely none that you will encounter in normal usage.
89+
## Template syntax
90+
91+
Envcat supports most jinja2 syntax and [builtin filters](https://jinja.palletsprojects.com/en/2.11.x/templates/#list-of-builtin-filters).
92+
93+
On top it provides the following additional filters:
94+
95+
#### b64encode, b64encode_urlsafe
96+
97+
```bash
98+
export FOO="hello? world?"
99+
100+
# b64encode, b64encode_urlsafe
101+
echo "{{FOO | b64encode}}" | envtpl FOO # => aGVsbG8/IHdvcmxkPw==
102+
echo "{{FOO | b64encode_urlsafe}}" | envtpl FOO # => aGVsbG8_IHdvcmxkPw==
103+
```
104+
105+
#### b64decode
106+
107+
```bash
108+
export B64_REGULAR="aGVsbG8/IHdvcmxkPw=="
109+
export B64_URLSAFE="aGVsbG8_IHdvcmxkPw=="
110+
111+
echo "{{B64_REGULAR | b64decode}}" | envtpl 'B*' # => hello? world?
112+
echo "{{B64_URLSAFE | b64decode}}" | envtpl 'B*' # => hello? world?
113+
```
114+
115+
#### split
116+
117+
118+
```bash
119+
export FOO=a,b,c
120+
121+
echo "{% for x in FOO | split(',') %}{{x}}..{% endfor %}" | envtpl FOO # => a..b..c..
122+
```
123+
124+
**Note:**
125+
Envcat uses a [Crystal implementation of the jinja2 template engine](https://straight-shoota.github.io/crinja/).
126+
Python expressions are **not** supported.
93127

94128

95129
## Checks
@@ -161,8 +195,4 @@ $ envcat -f json VARS_ETF:etf A B C
161195
3. Commit your changes (`git commit -am 'Add some feature'`)
162196
4. Push to the branch (`git push origin my-new-feature`)
163197
5. Create a new Pull Request
164-
165-
## Contributors
166-
167-
- [moe](https://github.com/m-o-e) - creator and maintainer
168198
{% endraw %}

shard.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: envcat
2-
version: 1.0.0
2+
version: 1.0.1
33

44
authors:
55

src/envcat/cli.cr

+6-2
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ class Envcat::Cli
2929
Toka.mapping({
3030
format: {
3131
type: String,
32-
default: Format::DEFAULT,
32+
default: Envcat::Cli.default_format,
3333
value_name: "FORMAT",
34-
description: Format.keys.sort!.join("|") + " (default: #{Format::DEFAULT})",
34+
description: Format.keys.sort!.join("|") + " (default: #{Envcat::Cli.default_format})",
3535
},
3636
check: {
3737
type: Array(String),
@@ -53,6 +53,10 @@ class Envcat::Cli
5353
help: false,
5454
})
5555

56+
def self.default_format
57+
PROGRAM_NAME.try &.includes?("envtpl") ? "j2" : Format::DEFAULT
58+
end
59+
5660
def self.help
5761
String.build(4096) do |s|
5862
s.puts "FORMAT"

0 commit comments

Comments
 (0)