Skip to content

Commit

Permalink
Merge pull request #5 from msherif1234/shell_lint
Browse files Browse the repository at this point in the history
NETOBSERV-1498 Add shellcheck linter for bash and make help target
  • Loading branch information
msherif1234 authored Mar 14, 2024
2 parents 43fcbf0 + 2571728 commit fa4dc73
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
16 changes: 13 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,34 @@ 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 ./...
ifeq (, $(shell which shellcheck))
@echo "### shellcheck could not be found, skipping shell lint"
else
@echo "### Run shellcheck against bash scripts"
find . -name '*.sh' | xargs shellcheck
endif

.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)
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ To run this CLI, you will need:

To build the project locally:

### Install `shellcheck` package

```bash
sudo dnf install -y shellcheck
```

### Build the project

```bash
make build
```
Expand Down
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 fa4dc73

Please sign in to comment.