[enh] sh: add shell formatter

Related https://github.com/searxng/searxng/issues/4803
This commit is contained in:
Ivan Gabaldon 2025-06-15 11:06:52 +02:00
parent 4b9644eb27
commit 2311d16497
No known key found for this signature in database
GPG key ID: 075587C93FA67582
8 changed files with 67 additions and 10 deletions

View file

@ -1,4 +1,4 @@
FROM mcr.microsoft.com/devcontainers/base:debian FROM mcr.microsoft.com/devcontainers/base:debian
RUN apt-get update && \ RUN apt-get update && \
apt-get -y install python3 python3-venv valkey firefox-esr graphviz imagemagick librsvg2-bin fonts-dejavu shellcheck apt-get -y install python3 python3-venv valkey firefox-esr graphviz imagemagick librsvg2-bin fonts-dejavu shellcheck shfmt

View file

@ -50,3 +50,6 @@ max_line_length = 79
[*.yml] [*.yml]
indent_size = 2 indent_size = 2
[[shell]]
switch_case_indent = true

View file

@ -34,6 +34,9 @@ jobs:
- "3.13" - "3.13"
steps: steps:
- name: Install shfmt
run: sudo apt-get install -y shfmt
- name: Setup Python - name: Setup Python
uses: actions/setup-python@v5 uses: actions/setup-python@v5
with: with:

View file

@ -50,8 +50,8 @@ search.checker.%: install
$(Q)./manage pyenv.cmd searxng-checker -v "$(subst _, ,$(patsubst search.checker.%,%,$@))" $(Q)./manage pyenv.cmd searxng-checker -v "$(subst _, ,$(patsubst search.checker.%,%,$@))"
PHONY += test ci.test test.shell PHONY += test ci.test test.shell
ci.test: test.yamllint test.black test.types.ci test.pylint test.unit test.robot test.rst test.shell test.pybabel ci.test: test.yamllint test.shfmt test.black test.types.ci test.pylint test.unit test.robot test.rst test.shell test.pybabel
test: test.yamllint test.black test.types.dev test.pylint test.unit test.robot test.rst test.shell test: test.yamllint test.shfmt test.black test.types.dev test.pylint test.unit test.robot test.rst test.shell
test.shell: test.shell:
$(Q)shellcheck -x -s dash \ $(Q)shellcheck -x -s dash \
container/entrypoint.sh container/entrypoint.sh
@ -82,8 +82,8 @@ MANAGE += gecko.driver
MANAGE += node.env node.env.dev node.clean MANAGE += node.env node.env.dev node.clean
MANAGE += py.build py.clean MANAGE += py.build py.clean
MANAGE += pyenv pyenv.install pyenv.uninstall MANAGE += pyenv pyenv.install pyenv.uninstall
MANAGE += format.python MANAGE += format.python format.shell
MANAGE += test.yamllint test.pylint test.black test.pybabel test.unit test.coverage test.robot test.rst test.clean test.themes test.types.dev test.types.ci MANAGE += test.yamllint test.pylint test.shfmt test.black test.pybabel test.unit test.coverage test.robot test.rst test.clean test.themes test.types.dev test.types.ci
MANAGE += themes.all themes.simple themes.fix themes.lint themes.test MANAGE += themes.all themes.simple themes.fix themes.lint themes.test
MANAGE += static.build.commit static.build.drop static.build.restore MANAGE += static.build.commit static.build.drop static.build.restore
MANAGE += nvm.install nvm.clean nvm.status nvm.nodejs MANAGE += nvm.install nvm.clean nvm.status nvm.nodejs

View file

@ -161,3 +161,33 @@ To lint shell scripts we use ShellCheck_ - a shell script static analysis tool
$ sudo dnf install ShellCheck $ sudo dnf install ShellCheck
.. SNAP sh lint requirements .. SNAP sh lint requirements
.. _sh format:
Format shell scripts
====================
.. _shfmt: https://github.com/mvdan/sh
To format shell scripts we use shfmt_ - a shell script parser & formatter
(:ref:`install buildhost <searxng.sh install buildhost>`).
.. tabs::
.. group-tab:: Ubuntu / debian
.. code-block:: sh
$ sudo apt install shfmt
.. group-tab:: Arch Linux
.. code-block:: sh
$ sudo pacman -S shfmt
.. group-tab:: Fedora / RHEL
.. code-block:: sh
$ sudo dnf install shfmt

16
manage
View file

@ -51,6 +51,14 @@ GECKODRIVER_VERSION="v0.35.0"
# SPHINXOPTS= # SPHINXOPTS=
BLACK_OPTIONS=("--target-version" "py311" "--line-length" "120" "--skip-string-normalization") BLACK_OPTIONS=("--target-version" "py311" "--line-length" "120" "--skip-string-normalization")
BLACK_TARGETS=("--exclude" "(searx/static|searx/languages.py)" "--include" 'searxng.msg|\.pyi?$' "searx" "searxng_extra" "tests") BLACK_TARGETS=("--exclude" "(searx/static|searx/languages.py)" "--include" 'searxng.msg|\.pyi?$' "searx" "searxng_extra" "tests")
SHFMT_TARGETS=(
"./container/"
"./docs/"
"./searx/"
"./searxng_extra/"
"./tests/"
"./utils/"
"./manage")
YAMLLINT_FILES=() YAMLLINT_FILES=()
while IFS= read -r line; do while IFS= read -r line; do
@ -218,11 +226,17 @@ pyenv.uninstall() {
} }
format.python() { format.python() {
build_msg TEST "[format.python] black \$BLACK_TARGETS" build_msg TEST "[format.python] black ${BLACK_TARGETS[*]}"
pyenv.cmd black "${BLACK_OPTIONS[@]}" "${BLACK_TARGETS[@]}" pyenv.cmd black "${BLACK_OPTIONS[@]}" "${BLACK_TARGETS[@]}"
dump_return $? dump_return $?
} }
format.shell() {
build_msg TEST "[format.shell] shfmt ${SHFMT_TARGETS[*]}"
shfmt --list --write --apply-ignore --simplify "${SHFMT_TARGETS[@]}"
dump_return $?
}
docs.prebuild() { docs.prebuild() {
build_msg DOCS "build ${DOCS_BUILD}/includes" build_msg DOCS "build ${DOCS_BUILD}/includes"
( (

View file

@ -8,6 +8,7 @@ test.:
pylint : lint ./searx, ./searxng_extra and ./tests pylint : lint ./searx, ./searxng_extra and ./tests
pyright : static type check of python sources (.dev or .ci) pyright : static type check of python sources (.dev or .ci)
black : check black code format black : check black code format
shfmt : check shfmt code format
unit : run unit tests unit : run unit tests
coverage : run unit tests with coverage coverage : run unit tests with coverage
robot : run robot test robot : run robot test
@ -100,11 +101,17 @@ test.types.ci() {
} }
test.black() { test.black() {
build_msg TEST "[black] \$BLACK_TARGETS" build_msg TEST "[black] ${BLACK_TARGETS[*]}"
pyenv.cmd black --check --diff "${BLACK_OPTIONS[@]}" "${BLACK_TARGETS[@]}" pyenv.cmd black --check --diff "${BLACK_OPTIONS[@]}" "${BLACK_TARGETS[@]}"
dump_return $? dump_return $?
} }
test.shfmt() {
build_msg TEST "[shfmt] ${SHFMT_TARGETS[*]}"
shfmt --list --diff --apply-ignore --simplify "${SHFMT_TARGETS[@]}"
dump_return $?
}
test.unit() { test.unit() {
build_msg TEST 'tests/unit' build_msg TEST 'tests/unit'
# shellcheck disable=SC2086 # shellcheck disable=SC2086

View file

@ -66,7 +66,7 @@ git build-essential libxslt-dev zlib1g-dev libffi-dev libssl-dev"
SEARXNG_BUILD_PACKAGES_debian="\ SEARXNG_BUILD_PACKAGES_debian="\
graphviz imagemagick texlive-xetex librsvg2-bin graphviz imagemagick texlive-xetex librsvg2-bin
texlive-latex-recommended texlive-extra-utils fonts-dejavu texlive-latex-recommended texlive-extra-utils fonts-dejavu
latexmk shellcheck" latexmk shellcheck shfmt"
# pacman packages # pacman packages
@ -77,7 +77,7 @@ git base-devel libxml2"
SEARXNG_BUILD_PACKAGES_arch="\ SEARXNG_BUILD_PACKAGES_arch="\
graphviz imagemagick texlive-bin extra/librsvg graphviz imagemagick texlive-bin extra/librsvg
texlive-core texlive-latexextra ttf-dejavu shellcheck" texlive-core texlive-latexextra ttf-dejavu shellcheck shfmt"
# dnf packages # dnf packages
@ -90,7 +90,7 @@ SEARXNG_BUILD_PACKAGES_fedora="\
graphviz graphviz-gd ImageMagick librsvg2-tools graphviz graphviz-gd ImageMagick librsvg2-tools
texlive-xetex-bin texlive-collection-fontsrecommended texlive-xetex-bin texlive-collection-fontsrecommended
texlive-collection-latex dejavu-sans-fonts dejavu-serif-fonts texlive-collection-latex dejavu-sans-fonts dejavu-serif-fonts
dejavu-sans-mono-fonts ShellCheck" dejavu-sans-mono-fonts ShellCheck shfmt"
case $DIST_ID-$DIST_VERS in case $DIST_ID-$DIST_VERS in
ubuntu-18.04) ubuntu-18.04)