Skip to content

Commit eb77d74

Browse files
committed
install- and run-shfmt.sh, and format own sources include in cleanup
so far we formatted casually via IDE but of course it would make sense to do this automatically
1 parent 402d7ae commit eb77d74

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+691
-349
lines changed

.github/ISSUE_TEMPLATE/bug_report.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ body:
2222
options:
2323
- ci/install-shellcheck.sh
2424
- ci/install-shellspec.sh
25+
- ci/install-shfmt.sh
2526
- ci/jelastic/deploy.sh
2627
- ci/jelastic/utils.sh
2728
- qa/run-shellcheck.sh
2829
- qa/run-shellcheck-pull-hooks.sh
2930
- qa/run-shellspec-if-installed.sh
31+
- qa/run-shfmt.sh
3032
- releasing/common-constants.source.sh
3133
- releasing/deduce-next-version.source.sh
3234
- releasing/pre-release-checks-git.sh

README.md

Lines changed: 75 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,15 @@ The scripts are ordered by topic:
104104
- [CI](#continuous-integration)
105105
- [install shellcheck](#install-shellcheck)
106106
- [install shellspec](#install-shellspec)
107+
- [install shfmt](#install-shfmt)
107108
- [Jelastic](#jelastic)
108109
- [deploy](#deploy-to-jelastic)
109110
- [utils](#jelastic-utils)
110111
- [Quality Assurance](#quality-assurance)
111112
- [runShellcheck](#runshellcheck)
112113
- [runShellcheckPullHooks](#runshellcheck-on-pull-hookssh)
113114
- [runShellspecIfInstalled](#runshellspecifinstalled)
115+
- [runShfmt](#runshfmt)
114116
- [Releasing](#releasing)
115117
- [Releasing Files](#release-files)
116118
- [Prepare Files Next Dev Cycle](#prepare-files-next-dev-cycle)
@@ -194,6 +196,28 @@ jobs:
194196

195197
</ci-install-shellspec>
196198

199+
200+
## Install shfmt
201+
202+
<ci-install-shfmt>
203+
204+
<!-- auto-generated, do not modify here but in src/ci/install-shfmt.sh.doc -->
205+
```bash
206+
# run the install-shfmt.sh in your github/gitlab workflow
207+
# for instance, assuming you fetched this file via gt and remote name is tegonal-scripts
208+
# then in a github workflow you would have
209+
210+
jobs:
211+
steps:
212+
- name: install shfmt
213+
run: ./lib/tegonal-scripts/src/ci/install-shfmt.sh
214+
# and most likely as well
215+
- name: run shfmt
216+
run: ./scripts/run-shfmt.sh
217+
```
218+
219+
</ci-install-shfmt>
220+
197221
## Jelastic
198222

199223
The scripts under this topic (in directory `ci/jelastic`) help out when dealing with jelastic cli
@@ -306,9 +330,9 @@ The scripts under this topic (in directory `qa`) perform checks or execute qa to
306330

307331
## runShellcheck
308332

309-
A function which expects the name of an array of dirs as first argument and a source path as second argument (which is
310-
passed to shellcheck via -P parameter). It then executes shellcheck for each *.sh in these directories with predefined
311-
settings for shellcheck.
333+
A function which expects the name of an array of dirs as first argument, a source path as second argument (which is
334+
passed to `shellcheck` via `-P` parameter) and optionally additional arguments which are passed to the `find` command.
335+
It then executes shellcheck for each *.sh in these directories with predefined settings for shellcheck.
312336

313337
<qa-run-shellcheck>
314338

@@ -370,6 +394,39 @@ runShellspecIfInstalled --jobs 2
370394

371395
</qa-run-shellspec-if-installed>
372396

397+
## runShfmt
398+
399+
A function which expects the name of an array of dirs as first argument and optionally additional arguments which
400+
are passed to the `find` command. It then executes shfmt for each *.sh in these directories.
401+
402+
<qa-run-shfmt>
403+
404+
<!-- auto-generated, do not modify here but in src/qa/run-shfmt.sh.doc -->
405+
```bash
406+
#!/usr/bin/env bash
407+
set -euo pipefail
408+
shopt -s inherit_errexit || { echo >&2 "please update to bash 5, see errors above" && exit 1; }
409+
# Assumes tegonal's scripts were fetched with gt - adjust location accordingly
410+
dir_of_tegonal_scripts="$(cd -- "$(dirname -- "${BASH_SOURCE[0]:-$0}")" >/dev/null && pwd 2>/dev/null)/../lib/tegonal-scripts/src"
411+
source "$dir_of_tegonal_scripts/setup.sh" "$dir_of_tegonal_scripts"
412+
413+
source "$dir_of_tegonal_scripts/qa/run-shfmt.sh"
414+
415+
# shellcheck disable=SC2034 # is passed by name to runShfmt
416+
declare -a dirs=(
417+
"$dir_of_tegonal_scripts"
418+
"$dir_of_tegonal_scripts/../scripts"
419+
"$dir_of_tegonal_scripts/../spec"
420+
)
421+
runShfmt dirs -not -name sh-to-exclude.sh
422+
423+
# pass the working directory of gt which usually is .gt in the root of your repository
424+
# this will run shfmt on all pull-hook.sh files
425+
runShfmtPullHooks ".gt"
426+
```
427+
428+
</qa-run-shfmt>
429+
373430
# Releasing
374431

375432
The scripts under this topic (in directory `releasing`) perform some steps of your release process.
@@ -523,7 +580,7 @@ sourceOnce "$scriptsDir/before-pr.sh"
523580
# prepare dev cycle for version v0.2.0, assumes a function beforePr is in scope which we sourced above
524581
"$dir_of_tegonal_scripts/releasing/prepare-files-next-dev-cycle.sh" -v v0.2.0
525582

526-
function specialBeforePr(){
583+
function specialBeforePr() {
527584
beforePr && echo "imagine some additional work"
528585
}
529586
# make the function visible to release-files.sh / not necessary if you source prepare-files-next-dev-cycle.sh, see further below
@@ -701,7 +758,6 @@ shopt -s inherit_errexit || { echo >&2 "please update to bash 5, see errors abov
701758
dir_of_tegonal_scripts="$(cd -- "$(dirname -- "${BASH_SOURCE[0]:-$0}")" >/dev/null && pwd 2>/dev/null)/../lib/tegonal-scripts/src"
702759
source "$dir_of_tegonal_scripts/setup.sh" "$dir_of_tegonal_scripts"
703760

704-
705761
function prepareNextAfterVersionUpdateHook() {
706762
# some additional version bumps e.g. using perl
707763
perl -0777 -i #...
@@ -1362,10 +1418,10 @@ function foo() {
13621418
local -r version=$4
13631419

13641420
# resolves arr recursively via recursiveDeclareP and check that is a non-associative array
1365-
checkArgIsArray arr 1 # same as exitIfArgIsNotArray if set -e has an effect on this line
1366-
checkArgIsFunction "$fn" 2 # same as exitIfArgIsNotFunction if set -e has an effect on this line
1367-
checkArgIsBoolean "$bool" 3 # same as exitIfArgIsNotBoolean if set -e has an effect on this line
1368-
checkArgIsVersion "$version" 4 # same as exitIfArgIsNotVersion if set -e has an effect on this line
1421+
checkArgIsArray arr 1 # same as exitIfArgIsNotArray if set -e has an effect on this line
1422+
checkArgIsFunction "$fn" 2 # same as exitIfArgIsNotFunction if set -e has an effect on this line
1423+
checkArgIsBoolean "$bool" 3 # same as exitIfArgIsNotBoolean if set -e has an effect on this line
1424+
checkArgIsVersion "$version" 4 # same as exitIfArgIsNotVersion if set -e has an effect on this line
13691425

13701426
# shellcheck disable=SC2329 # is passed by name to checkArgIsArrayWithTuples
13711427
function describeTriple() {
@@ -1447,15 +1503,15 @@ timestampToDate 1662981524 # outputs 2022-09-12
14471503
# (usually as defined by the user in the system settings)
14481504
timestampToDateInUserFormat 1662981524 # outputs 12.09.2022 for ch_DE
14491505

1450-
dateToTimestamp "2024-03-01" # outputs 1709247600
1506+
dateToTimestamp "2024-03-01" # outputs 1709247600
14511507
dateToTimestamp "2022-09-12T13:18:44" # outputs 1662981524
14521508

14531509
# outputs a timestamp in ms
14541510
startTimestampInMs="$(timestampInMs)"
14551511

1456-
formatMsToSeconds 12 # outputs 0.012
1457-
formatMsToSeconds 1234 # outputs 1.234
1458-
formatMsToSeconds -123 # outputs -0.123
1512+
formatMsToSeconds 12 # outputs 0.012
1513+
formatMsToSeconds 1234 # outputs 1.234
1514+
formatMsToSeconds -123 # outputs -0.123
14591515
# note that formatMsToSeconds does not check if you pass a number
14601516

14611517
# outputs the time passed since the given timestamp in ms formatted as seconds
@@ -1648,7 +1704,6 @@ function readFile() {
16481704
# executes readFile and closes the file descriptors again
16491705
withCustomOutputInput 3 4 readFile "my-file.txt"
16501706

1651-
16521707
# First tries to set chmod 777 to the directory and all files within it and then deletes the directory
16531708
deleteDirChmod777 ".git"
16541709
```
@@ -1832,7 +1887,7 @@ function myFunctionWithVarargs() {
18321887
parseFnArgs params "$@" || return $?
18331888

18341889
# use varargs in another script
1835-
echo "command: $command, dir: $dir, varargs: ${varargs*}"
1890+
echo "command: $command, dir: $dir, varargs: ${varargs[*]}"
18361891
}
18371892
```
18381893

@@ -1986,7 +2041,7 @@ source "$dir_of_tegonal_scripts/utility/replace-snippet.sh"
19862041

19872042
declare file
19882043
file=$(mktemp)
1989-
echo "<my-script></my-script>" > "$file"
2044+
echo "<my-script></my-script>" >"$file"
19902045

19912046
declare dir fileName output
19922047
dir=$(dirname "$file")
@@ -2067,10 +2122,10 @@ source "$dir_of_tegonal_scripts/setup.sh" "$dir_of_tegonal_scripts"
20672122

20682123
source "$dir_of_tegonal_scripts/utility/source-once.sh"
20692124

2070-
sourceOnce "foo.sh" # creates a variable named sourceOnceGuard_foo__sh which acts as guard and sources foo.sh
2071-
sourceOnce "foo.sh" # will source nothing as sourceOnceGuard_foo__sh is already defined
2072-
unset sourceOnceGuard_foo__sh # unsets the guard
2073-
sourceOnce "foo.sh" # is sourced again and the guard established
2125+
sourceOnce "foo.sh" # creates a variable named sourceOnceGuard_foo__sh which acts as guard and sources foo.sh
2126+
sourceOnce "foo.sh" # will source nothing as sourceOnceGuard_foo__sh is already defined
2127+
unset sourceOnceGuard_foo__sh # unsets the guard
2128+
sourceOnce "foo.sh" # is sourced again and the guard established
20742129
# you can also use sourceAlways instead of unsetting and using sourceOnce.
20752130
sourceAlways "foo.sh"
20762131

scripts/cleanup-on-push-to-main.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@ fi
3030
sourceOnce "$dir_of_tegonal_scripts/utility/cleanups.sh"
3131
sourceOnce "$dir_of_tegonal_scripts/utility/replace-help-snippet.sh"
3232
sourceOnce "$dir_of_tegonal_scripts/utility/update-bash-docu.sh"
33+
sourceOnce "$scriptsDir/run-shfmt.sh"
3334

3435
function cleanupOnPushToMain() {
35-
removeUnusedSignatures "$projectDir"
36+
removeUnusedSignatures "$projectDir" || return $?
37+
38+
customRunShfmt || die "was not able to format"
3639

3740
local script
3841
find "$dir_of_tegonal_scripts" -name "*.sh" \

scripts/run-shellcheck.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ function customRunShellcheck() {
3131
"$dir_of_tegonal_scripts/../spec"
3232
)
3333
local sourcePath="$dir_of_tegonal_scripts:$scriptsDir"
34-
runShellcheck dirs "$sourcePath" -not -name "install-*.doc.sh"
34+
runShellcheck dirs "$sourcePath" \
35+
-not -name "install-*.doc.sh" # those are github workflow excerpts and not bash
3536

3637
runShellcheckPullHooks "$scriptsDir/../.gt"
3738
}

scripts/run-shfmt.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env bash
2+
#
3+
# __ __
4+
# / /____ ___ ____ ___ ___ _/ / This script is provided to you by https://github.com/tegonal/scripts
5+
# / __/ -_) _ `/ _ \/ _ \/ _ `/ / Copyright 2022 Tegonal Genossenschaft <info@tegonal.com>
6+
# \__/\__/\_, /\___/_//_/\_,_/_/ It is licensed under Apache License 2.0
7+
# /___/ Please report bugs and contribute back your improvements
8+
#
9+
# Version: v4.10.0-SNAPSHOT
10+
###################################
11+
set -euo pipefail
12+
shopt -s inherit_errexit || { echo >&2 "please update to bash 5, see errors above" && exit 1; }
13+
unset CDPATH
14+
15+
if ! [[ -v scriptsDir ]]; then
16+
scriptsDir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]:-$0}")" >/dev/null && pwd 2>/dev/null)"
17+
readonly scriptsDir
18+
fi
19+
20+
if ! [[ -v dir_of_tegonal_scripts ]]; then
21+
dir_of_tegonal_scripts="$scriptsDir/../src"
22+
source "$dir_of_tegonal_scripts/setup.sh" "$dir_of_tegonal_scripts"
23+
fi
24+
sourceOnce "$dir_of_tegonal_scripts/qa/run-shfmt.sh"
25+
26+
function customRunShfmt() {
27+
# shellcheck disable=SC2034 # is passed by name to runShfmt
28+
local -ra dirs=(
29+
"$dir_of_tegonal_scripts"
30+
"$scriptsDir"
31+
"$dir_of_tegonal_scripts/../spec"
32+
)
33+
runShfmt dirs \
34+
-not -name "install-*.doc.sh" # those are github workflow excerpts and not bash
35+
36+
runShfmtPullHooks "$scriptsDir/../.gt"
37+
}
38+
39+
${__SOURCED__:+return}
40+
customRunShfmt "$@"

0 commit comments

Comments
 (0)