Executing Templates
With the sketch exec
command, you can render a template and then execute it as a shell command.
To do this, you can use a regular file, a template with an id (either defined in the config file or inside the templates_dir
), or even one defined within the command itself.
From Template
Let's say that we have a file named cmd_template.j2
inside our templates_dir
, and it looks like this:
echo "{{ category }} engine... {{ category }}... argh!" > output_from_templates_dir.txt
âšī¸ You do not have to use
.j2
as an extension for the template files. Any extension can be used.
We can refer to it by its id:
sketch --set category="gp2" exec -t cmd_template.j2
This will create a file containing
gp2 engine... gp2... argh!
From File
We can also use a file that is not inside templates_dir
by using the -f
flag and providing the path to said file. Relative paths will be resolved starting from the cwd.
So let's say that we have this template file:
echo "all the time you have to leave the {{ something }}!" > output_from_file.txt
We launch the command like this:
sketch --set something="space" exec -f tests/commands_tests/cmd_from_file.j2
To create a file containing:
all the time you have to leave the space!
From Literal Template
Templates can also be defined directly as part of the command.
sketch --set general="kenobi" exec 'echo "hello there!\ngeneral {{ general }}." > command_output.txt'
This creates a file containing:
hello there!
general kenobi.