Skip to content

Commit

Permalink
Fix add_descriptions_to_readme.sh script (kuskoman#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
satk0 authored Feb 14, 2024
1 parent 433021b commit d0eca4c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 184 deletions.
1 change: 0 additions & 1 deletion .github/workflows/go-application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ jobs:
uses: ludeeus/[email protected]
with:
ignore_names: >-
add_descriptions_to_readme.sh
add_metrics_to_readme.sh
create_release_notes.sh
verify_metrics.sh
Expand Down
94 changes: 4 additions & 90 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ See more in the [Migration](#migration) section.
- `make build-linux`: Builds a binary executable for Linux.
- `make build-darwin`: Builds a binary executable for Darwin.
- `make build-windows`: Builds a binary executable for Windows.
- `make build-linux-arm`: Builds a binary executable for Linux ARM.
- `make build-docker`: Builds a Docker image for the Go Exporter application.
- `make build-docker-multi`: Builds a multi-arch Docker image (`amd64` and `arm64`).
- `make clean`: Deletes all binary executables in the out directory.
Expand All @@ -168,6 +169,9 @@ See more in the [Migration](#migration) section.
- `make install-helm-readme`: Installs readme-generator-for-helm tool.
- `make helm-readme`: Generates Helm chart README.md file.
- `make clean-elasticsearch`: Cleans Elasticsearch data, works only with default ES port. The command may take a very long time to complete.
- `make upgrade-dependencies`: Upgrades all dependencies.
- `make migrate-v1-to-v2`: Migrates configuration from v1 to v2.
- `make update-readme-descriptions`: Update Makefile descriptions in main README.md.
- `make help`: Shows info about available commands.

<!--- **************************************************** --->
Expand All @@ -177,96 +181,6 @@ See more in the [Migration](#migration) section.
The main Go Exporter application is located in the cmd/exporter/main.go file.
The binary executables are saved in the out directory.

#### Example Usage

<!--- GENERATED by ./scripts/add_descriptions_to_readme.sh --->

Builds binary executables for all OS (Win, Darwin, Linux):

make all

Runs the Go Exporter application:

make run

Builds a binary executable for Linux:

make build-linux

Builds a binary executable for Darwin:

make build-darwin

Builds a binary executable for Windows:

make build-windows

Builds a Docker image for the Go Exporter application:

make build-docker

Builds a multi-arch Docker image (`amd64` and `arm64`):

make build-docker-multi

Deletes all binary executables in the out directory:

make clean

Runs all tests:

make test

Displays test coverage report:

make test-coverage

Starts a Docker-compose configuration:

make compose

Starts a Docker-compose configuration until it's ready:

make wait-for-compose

Stops a Docker-compose configuration:

make compose-down

Verifies the metrics from the Go Exporter application:

make verify-metrics

Pulls the Docker image from the registry:

make pull

Shows logs from the Docker-compose configuration:

make logs

Minifies the binary executables:

make minify

Installs readme-generator-for-helm tool:

make install-helm-readme

Generates Helm chart README.md file:

make helm-readme

Cleans Elasticsearch data, works only with default ES port. The command may take a very long time to complete:

make clean-elasticsearch

Shows info about available commands:

make help

<!--- **************************************************** --->

## Helper Scripts

Application repository contains some helper scripts, which can be used to improve process
Expand Down
158 changes: 65 additions & 93 deletions scripts/add_descriptions_to_readme.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,128 +2,100 @@

# execute: ./scripts/add_descriptions_to_readme.sh

# define new line character to be used in `sed`
nl='
'

# enable regex (see: https://stackoverflow.com/a/42740385)
shopt -s extglob

scriptName="$(dirname "$0")/$(basename "$0")"

function getHelp() { # get descriptions and commands from Makefile
i=0
commands=()
descriptions=()

while read -r line; do
if (( i % 2 == 0 ));
then
descriptions+=( "$(echo $line | sed 's/#:[ ]*//')" )
else
commands+=( $(echo "$line" | cut -d : -f 1) )
fi

((i++))
done < <(
# https://stackoverflow.com/a/59087509
grep -B1 -E "^[a-zA-Z0-9_-]+:([^\=]|$)" ./Makefile \
| grep -v -- --
)
i=0
commands=()
descriptions=()

while read -r line; do
if (( i % 2 == 0 ));
then
descriptions+=( "${line//#:*( )}" )
else
commands+=( "$(echo "$line" | cut -d : -f 1)" )
fi
((i++))
done < <(
# https://stackoverflow.com/a/59087509
grep -B1 -E "^[a-zA-Z0-9_-]+:([^\=]|$)" ./Makefile \
| grep -v -- --
)
}

FILE=README.md

# returns `commands` and `descriptions` arrays
getHelp

let startLine=$(grep -n "^#### Available Commands" $FILE | cut -d : -f 1)+2
let endLine=$(grep -n "^#### File Structure" $FILE | cut -d : -f 1)-2

# Updates "Available Commands" section:
startLine=$(( $( grep -n "^#### Available Commands" $FILE | cut -d : -f 1 ) + 2 ))
endLine=$(( $( grep -n "^#### File Structure" $FILE | cut -d : -f 1 ) - 2 ))

# Updates "Available Commands" section
if (( startLine <= endLine));
then
$(sed -i "$startLine,${endLine}d" $FILE) # deletion of previous descriptions
# Deletes previous descriptions
sed -i "$startLine,${endLine}d" "$FILE"
fi

function printAvailableCommands() {
curLine=$startLine
stringToWrite="<!--- GENERATED by $scriptName --->"
let commentLen=${#stringToWrite}-11
i=0

$(sed -i "${curLine}i\\${stringToWrite}" $FILE)
let curLine++

$(sed -i "${curLine}i\\ " $FILE) # empty line
let curLine++

while (( $i < ${#commands[@]} ))
do

stringToWrite="- \`make ${commands[$i]}\`: ${descriptions[$i]}."
$(sed -i "${curLine}i\\${stringToWrite}" $FILE)
let curLine++

let i++
done

$(sed -i "${curLine}i\\ " $FILE) # empty line
let curLine++

stringToWrite="<!--- $( eval $( echo printf '"\*%.0s"' {1..$commentLen} ) ) --->" # multiple '*'
$(sed -i "${curLine}i\\${stringToWrite}" $FILE)
let curLine++

function createMultipleAsterisks() {
numOfAsterisks=$1
asterisks=$( eval printf "\*%.0s" "{1..${numOfAsterisks}}" )
echo "$asterisks"
}

echo 'Updating "Available Commands" section...'

printAvailableCommands

# Updates "Example Usage" section:

let startLine=$(grep -n "^#### Example Usage" $FILE | cut -d : -f 1)+2
let endLine=$(grep -n "^## Helper Scripts" $FILE | cut -d : -f 1)-2

if (( startLine <= endLine));
then
$(sed -i "$startLine,${endLine}d" $FILE) # deletion of previous descriptions
fi
function printAvailableCommands() {
curLine=$startLine
stringToWrite="<!--- GENERATED by $scriptName --->"
commentLen=$(( ${#stringToWrite} - 11 ))
i=0

function printExampleUsage() {
curLine=$startLine
stringToWrite="<!--- GENERATED by $scriptName --->"
let commentLen=${#stringToWrite}-11
i=0
sed -i "${curLine}i\\${stringToWrite}" "$FILE"

$(sed -i "${curLine}i\\${stringToWrite}" $FILE)
let curLine++
# https://www.shellcheck.net/wiki/SC2219
(( curLine++ )) || true

$(sed -i "${curLine}i\\ " $FILE) # empty line
let curLine++
# empty line
sed -i "${curLine}i${nl}" "$FILE"

while (( $i < ${#commands[@]} ))
do
stringToWrite="${descriptions[$i]}:"
$(sed -i "${curLine}i\\${stringToWrite}" $FILE)
let curLine++
(( curLine++ )) || true

$(sed -i "${curLine}i\\ " $FILE)
let curLine++
echo 'Writing the following commands with their descriptions:'
while (( i < ${#commands[@]} ))
do
stringToWrite="- \`make ${commands[$i]}\`: ${descriptions[$i]}."
stringToWrite=${stringToWrite//\'/\\\'}
echo "$stringToWrite"

stringToWrite=" make ${commands[$i]}" # 4 spaces for tab (DON'T CHANGE IT)
$(sed -i "${curLine}i\\${stringToWrite}" $FILE)
let curLine++
sed -i "${curLine}i\\${stringToWrite}" "$FILE"
(( curLine++ )) || true

$(sed -i "${curLine}i\\ " $FILE)
let curLine++
(( i++ )) || true
done

let i++
done
# empty line
sed -i "${curLine}i${nl}" "$FILE"
(( curLine++ )) || true

stringToWrite="<!--- $( eval $( echo printf '"\*%.0s"' {1..$commentLen} ) ) --->" # multiple '*'
$(sed -i "${curLine}i\\${stringToWrite}" $FILE)
let curLine++
# multiple '*'
asterisks=$(createMultipleAsterisks $commentLen)
stringToWrite="<!--- ${asterisks} --->"
sed -i "${curLine}i\\${stringToWrite}" "$FILE"

(( curLine++ )) || true
}

echo 'Updating "Example Usage" section...'
echo 'Updating "Available Commands" section...'

printExampleUsage
printAvailableCommands

echo 'Done.'

0 comments on commit d0eca4c

Please sign in to comment.