-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created by RobertDeRose Co-authored-by: GitHub Actions <github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
64f1649
commit f39d170
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
- id: esh-template-check | ||
name: ESH Template Check | ||
description: Pre-processes ESH templates, renders them with test cases, and runs optional shellcheck. | ||
language: docker_image | ||
entry: docker.io/robertderose/esh-template-check:v1.0.0 | ||
files: \.esh$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# ESH Template Check Pre-Commit Hook | ||
|
||
This pre-commit hook processes [`esh`](https://github.com/jirutka/esh) templates, renders them with test cases specified | ||
in `YAML` front matter, and optionally runs [`shellcheck`](https://github.com/koalaman/shellcheck) on the generated | ||
files. | ||
|
||
## Features | ||
|
||
- Parses YAML front matter to extract test cases and `shellcheck` arguments. | ||
- Preprocesses ESH templates for each test case. | ||
- Runs `shellcheck` with specified arguments, if provided. | ||
|
||
## Setup | ||
|
||
To use this hook in your repository: | ||
|
||
1. Add this repo to your `.pre-commit-config.yaml`: | ||
|
||
```yaml | ||
- repo: https://github.com/RobertDeRose/esh-template-check | ||
rev: v1.0.0 | ||
hooks: | ||
- id: esh-template-check | ||
``` | ||
### Supported Arguments | ||
- **-k** | ||
- Keep the generated output after running. Stores generated output in `esh_template_output` | ||
- If you enable keeping the output, it is recommended add the above directory to your `.gitignore` file | ||
- **-v** | ||
- Enable verbose output when an error occurs | ||
- **-s=SHELL** | ||
- Override the Shell that ESH will use, by default uses Bash | ||
|
||
### Supported Front Matter | ||
|
||
- **check_args** | ||
- This should be a string that will be passed in as the arguments to shellcheck | ||
- **test_cases** | ||
- This is an array of key/value pairs to be set as variables for the **template** | ||
|
||
## Example Template | ||
|
||
```bash | ||
#!/bin/bash | ||
<%# --- -%> | ||
<%# check_args: -s bash -%> | ||
<%# test_cases: -%> | ||
<%# - VAR1: one -%> | ||
<%# VAR2: two -%> | ||
<%# - VAR1: a -%> | ||
<%# VAR2: b -%> | ||
<%# - VAR1: 1 -%> | ||
<%# VAR2: 2 -%> | ||
<%# --- -%> | ||
<% if [[ "${VAR1}" =~ ^(one|a)$ ]]; then -%> | ||
<%= "# Generating script for ${VAR2}" %> | ||
hello_world() { | ||
local name | ||
<% if [[ "${VAR1}" == "one" ]]; then %> | ||
name="Rob" | ||
<% elif [[ "${VAR1}" == "a" ]]; then %> | ||
name="Bob" | ||
<% fi %> | ||
echo "Hello ${name} you got ${VAR2}" | ||
} | ||
``` |