Skip to content

Commit

Permalink
Add script to automate commands description in README (kuskoman#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
satk0 authored Jul 17, 2023
1 parent 637e83d commit 021b814
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 67 deletions.
16 changes: 8 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ GIT_COMMIT := $(shell git rev-parse HEAD)
DOCKER_IMG ?= "logstash-exporter"

# ****************************** NOTE ****************************** #
# Commands description was made using the following syntax: #
# https://stackoverflow.com/a/59087509 #
# #
# To write command description use "#:" before command definition #
# Commands description was made using the following syntax: #
# https://stackoverflow.com/a/59087509 #
# #
# To write command description use "#:" before command definition #
# ****************************************************************** #

#: Builds binary executables for all OS (Win, Darwin, Linux)
Expand Down Expand Up @@ -83,19 +83,19 @@ pull:
logs:
docker-compose logs -f

#: Minifies the binary executables.
#: Minifies the binary executables
minify:
upx -9 $(GOOS_EXES)

#: Installs readme-generator-for-helm tool.
#: Installs readme-generator-for-helm tool
install-helm-readme:
./scripts/install_helm_readme_generator.sh

#: Generates Helm chart README.md file.
#: Generates Helm chart README.md file
helm-readme:
./scripts/generate_helm_readme.sh

#: Show this help about available commands
#: Shows info about available commands
help:
@grep -B1 -E "^[a-zA-Z0-9_-]+\:([^\=]|$$)" Makefile \
| grep -v -- -- \
Expand Down
136 changes: 77 additions & 59 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,24 +99,30 @@ All configuration variables can be checked in the [config directory](./config/).

#### Available Commands

- `make all`: Builds binary executables for Linux, macOS, and Windows and saves them in the out directory.
<!--- GENERATED by ./scripts/add_descriptions_to_readme.sh --->

- `make all`: Builds binary executables for all OS (Win, Darwin, Linux).
- `make run`: Runs the Go Exporter application.
- `make build-<OS>`: Builds a binary executable for the specified OS (`<OS>` can be linux, darwin, or windows).
- `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-docker`: Builds a Docker image for the Go Exporter application.
- `make build-docker-multi`: Builds a multi-arch Docker image (`amd64` and `arm64`) 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.
- `make test`: Runs all tests.
- `make test-coverage`: Displays test coverage report.
- `make compose`: Starts a Docker-compose configuration.
- `make wait-for-compose`: Starts a Docker-compose configuration and waits for it to be ready.
- `make wait-for-compose`: Starts a Docker-compose configuration until it's ready.
- `make compose-down`: Stops a Docker-compose configuration.
- `make verify-metrics`: Verifies the metrics from the Go Exporter application.
- `make pull`: Pulls the Docker image from the registry.
- `make logs`: Shows logs from the Docker-compose configuration.
- `make minify`: Minifies the binary executables.
- `make install-helm-readme`: Installs readme-generator-for-helm tool.
- `make helm-readme`: Generates Helm chart README.md file.
- `make help`: Shows the available commands.
- `make help`: Shows info about available commands.

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

#### File Structure

Expand All @@ -125,77 +131,89 @@ The binary executables are saved in the out directory.

#### Example Usage

Build binary executables for all supported operating systems:

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

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

make all

Run the Go Exporter application:

Runs the Go Exporter application:
make run

Build a binary executable for macOS:


Builds a binary executable for Linux:

make build-linux

Builds a binary executable for Darwin:

make build-darwin

Build the Docker image:


Builds a binary executable for Windows:

make build-windows

Builds a Docker image for the Go Exporter application:

make build-docker

Build the multi-arch Docker image:

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

Delete all binary executables:

Deletes all binary executables in the out directory:
make clean

Run all tests:

Runs all tests:
make test

Display test coverage report:

make test-coverage

Start the Docker-compose configuration:

Displays test coverage report:
make test-coverage
Starts a Docker-compose configuration:
make compose

Start the Docker-compose configuration and wait for it to be ready:

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

Stop the Docker-compose configuration:

Stops a Docker-compose configuration:
make compose-down

Verify the metrics from the Exporter application:

Verifies the metrics from the Go Exporter application:
make verify-metrics

Pull the Docker image from the registry:

Pulls the Docker image from the registry:
make pull

Show logs from the Docker-compose configuration:

Shows logs from the Docker-compose configuration:
make logs

Minify the binary executables:

Minifies the binary executables:
make minify

Install readme-generator-for-helm tool:

Installs readme-generator-for-helm tool:
make install-helm-readme

Generate Helm chart README.md file:

Generates Helm chart README.md file:
make helm-readme

Show the available commands:

Shows info about available commands:
make help

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

## Helper Scripts

Expand Down
129 changes: 129 additions & 0 deletions scripts/add_descriptions_to_readme.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#!/usr/bin/env bash

# execute: ./scripts/add_descriptions_to_readme.sh

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 -- --
)
}

FILE=README.md

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:

if (( startLine <= endLine));
then
$(sed -i "$startLine,${endLine}d" $FILE) # deletion of previous descriptions
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++

}

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 printExampleUsage() {
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="${descriptions[$i]}:"
$(sed -i "${curLine}i\\${stringToWrite}" $FILE)
let curLine++

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

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

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

let i++
done

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

}

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

printExampleUsage

echo 'Done.'

0 comments on commit 021b814

Please sign in to comment.