Last active
July 24, 2023 09:39
-
-
Save gregalia/6af525347021b17d1b937a5be76987bd to your computer and use it in GitHub Desktop.
Lint and format GitHub Actions run blocks
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
#!/bin/bash | |
WORK_DIR="${PWD}/.ignore/run_blocks" | |
function export_run_blocks() { | |
mkdir -p "${WORK_DIR}" | |
yaml_file="${1}" | |
num_runs="$( | |
yq '[ | |
.jobs | .. | | |
select(has("run")) | |
] | length' \ | |
"${yaml_file}" | |
)" | |
# echo $num_runs | |
for ((i = 0; i < num_runs; i++)); do | |
export i | |
name="$( | |
yq '[ | |
.jobs | .. | | |
select(has("run")) | |
][env(i)].name' \ | |
"${yaml_file}" | |
)" | |
# echo "$name" | |
yq '[ | |
.jobs | .. | | |
select(has("run"))][env(i) | |
].run' \ | |
"${yaml_file}" \ | |
>"${WORK_DIR}/$(basename "$yaml_file"):${name// /_}.sh" | |
done | |
} | |
function strip_trailing_newlines() { | |
tempfile="$(mktemp)" | |
for file in "${WORK_DIR}"/*.sh; do | |
printf "%s" "$(<"${file}")" >"${tempfile}" | |
mv "${tempfile}" "${file}" | |
done | |
} | |
function format_scripts() { | |
shfmt \ | |
--diff \ | |
--list \ | |
--write \ | |
--indent 2 \ | |
--language-dialect bash \ | |
"${WORK_DIR}" 2>&1 | | |
grep \ | |
--invert-match \ | |
--extended-regexp 'invalid parameter name$' | |
} | |
function lint_scripts() { | |
for file in "${WORK_DIR}"/*.sh; do | |
shellcheck \ | |
--shell=bash \ | |
--exclude=SC2296 \ | |
--format=tty \ | |
"${file}" | |
done | |
} | |
function main() { | |
export_run_blocks "$@" | |
strip_trailing_newlines | |
lint_scripts | |
format_scripts | |
} | |
main "$@" | |
# ❯ shfmt --help | |
# usage: shfmt [flags] [path ...] | |
# | |
# shfmt formats shell programs. If the only argument is a dash ('-') or no | |
# arguments are given, standard input will be used. If a given path is a | |
# directory, all shell scripts found under that directory will be used. | |
# | |
# --version show version and exit | |
# | |
# -l, --list list files whose formatting differs from shfmt's | |
# -w, --write write result to file instead of stdout | |
# -d, --diff error with a diff when the formatting differs | |
# -s, --simplify simplify the code | |
# -mn, --minify minify the code to reduce its size (implies -s) | |
# | |
# Parser options: | |
# | |
# -ln, --language-dialect str bash/posix/mksh/bats, default "auto" | |
# -p, --posix shorthand for -ln=posix | |
# --filename str provide a name for the standard input file | |
# | |
# Printer options: | |
# | |
# -i, --indent uint 0 for tabs (default), >0 for number of spaces | |
# -bn, --binary-next-line binary ops like && and | may start a line | |
# -ci, --case-indent switch cases will be indented | |
# -sr, --space-redirects redirect operators will be followed by a space | |
# -kp, --keep-padding keep column alignment paddings | |
# -fn, --func-next-line function opening braces are placed on a separate line | |
# | |
# Utilities: | |
# | |
# -f, --find recursively find all shell files and print the paths | |
# --to-json print syntax tree to stdout as a typed JSON | |
# --from-json read syntax tree from stdin as a typed JSON | |
# | |
# For more information, see 'man shfmt' and https://github.com/mvdan/sh. | |
############## | |
# ❯ shellcheck --help | |
# Usage: shellcheck [OPTIONS...] FILES... | |
# -a --check-sourced Include warnings from sourced files | |
# -C[WHEN] --color[=WHEN] Use color (auto, always, never) | |
# -i CODE1,CODE2.. --include=CODE1,CODE2.. Consider only given types of warnings | |
# -e CODE1,CODE2.. --exclude=CODE1,CODE2.. Exclude types of warnings | |
# -f FORMAT --format=FORMAT Output format (checkstyle, diff, gcc, json, json1, quiet, tty) | |
# --list-optional List checks disabled by default | |
# --norc Don't look for .shellcheckrc files | |
# -o check1,check2.. --enable=check1,check2.. List of optional checks to enable (or 'all') | |
# -P SOURCEPATHS --source-path=SOURCEPATHS Specify path when looking for sourced files ("SCRIPTDIR" for script's dir) | |
# -s SHELLNAME --shell=SHELLNAME Specify dialect (sh, bash, dash, ksh) | |
# -S SEVERITY --severity=SEVERITY Minimum severity of errors to consider (error, warning, info, style) | |
# -V --version Print version information | |
# -W NUM --wiki-link-count=NUM The number of wiki links to show, when applicable | |
# -x --external-sources Allow 'source' outside of FILES | |
# --help Show this usage summary and exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment