Skip to content

Commit

Permalink
Add shellcheck linter for bash and make help target
Browse files Browse the repository at this point in the history
Signed-off-by: Mohamed Mahmoud <[email protected]>
  • Loading branch information
msherif1234 committed Mar 12, 2024
1 parent 43fcbf0 commit 142f313
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,30 @@ prereqs: ## Test if prerequisites are met, and installing missing dependencies
GOFLAGS="" go install github.com/golangci/golangci-lint/cmd/golangci-lint@${GOLANGCI_LINT_VERSION}

.PHONY: build
build: prepare lint
build: prepare lint ## Build the binary and run Lint
@go build -o $(OUTPUT)
cp -a ./oc/. ./$(DIST_DIR)
cp -a ./res/. ./$(DIST_DIR)/network-observability-cli-resources

.PHONY: image
image:
image: ## Build the docker image
$(OCI_BIN) build -t network-observability-cli .

.PHONY: lint
lint: prereqs ## Lint code
@echo "### Linting code"
golangci-lint run ./...
@echo "### Run shellcheck against bash scripts"
find . -name '*.sh' | xargs shellcheck

.PHONY: clean
clean:
clean: ## Clean up build directory
@rm -rf $(DIST_DIR)

.PHONY: oc-commands
oc-commands: build
sudo cp -a ./build/. /usr/bin/

.PHONY: help
help: ## Display this help.
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
24 changes: 14 additions & 10 deletions res/functions.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#!/usr/bin/env bash
set -eux

function setup {
echo "Setting up... "

Expand All @@ -11,27 +14,27 @@ function setup {
fi

filename=""
mkdir -p ${BASH_SOURCE%/*}/current
mkdir -p "${BASH_SOURCE%/*}"/current

if [ $1 = "flows" ]; then
if [ "$1" = "flows" ]; then
filename="flow-capture"
sed "s/{{FLOW_FILTER_VALUE}}/$2/gi" ${BASH_SOURCE%/*}/flow-capture.yml > ${BASH_SOURCE%/*}/current/agent.yml
elif [ $1 = "packets" ]; then
sed "s/{{FLOW_FILTER_VALUE}}/$2/gi" "${BASH_SOURCE%/*}"/flow-capture.yml > "${BASH_SOURCE%/*}"/current/agent.yml
elif [ "$1" = "packets" ]; then
filename="packet-capture"
sed "s/{{PCA_FILTER_VALUE}}/$2/gi" ${BASH_SOURCE%/*}/packet-capture.yml > ${BASH_SOURCE%/*}/current/agent.yml
sed "s/{{PCA_FILTER_VALUE}}/$2/gi" "${BASH_SOURCE%/*}"/packet-capture.yml > "${BASH_SOURCE%/*}"/current/agent.yml
else
echo "invalid setup argument"
return
fi

echo "creating netobserv-cli namespace"
oc apply -f ${BASH_SOURCE%/*}/namespace.yml
oc apply -f "${BASH_SOURCE%/*}"/namespace.yml

echo "creating service account"
oc apply -f ${BASH_SOURCE%/*}/service-account.yml
oc apply -f "${BASH_SOURCE%/*}"/service-account.yml

echo "creating $filename agents"
oc apply -f ${BASH_SOURCE%/*}/current/agent.yml
oc apply -f "${BASH_SOURCE%/*}"/current/agent.yml
oc rollout status daemonset netobserv-cli -n netobserv-cli --timeout 60s

echo "forwarding agents ports"
Expand All @@ -43,8 +46,8 @@ function setup {
do
echo "forwarding $pod:9999 to local port $port"
pkill --oldest --full "$port:9999"
oc port-forward $pod $port:9999 -n netobserv-cli & # run in background
node=$(oc get $pod -n netobserv-cli -o jsonpath='{.spec.nodeName}')
oc port-forward "$pod" $port:9999 -n netobserv-cli & # run in background
node=$(oc get "$pod" -n netobserv-cli -o jsonpath='{.spec.nodeName}')
if [ -z "$ports" ]
then
nodes="$node"
Expand All @@ -64,6 +67,7 @@ function cleanup {
stty -F /dev/tty echo
setterm -linewrap on

# shellcheck disable=SC2034
if output=$(oc whoami 2>&1); then
printf "\nCleaning up... "
oc delete namespace netobserv-cli
Expand Down

0 comments on commit 142f313

Please sign in to comment.