From cb6e0022daac16f43335f6192b421f58d84983eb Mon Sep 17 00:00:00 2001 From: Jayson Grace Date: Thu, 7 Sep 2023 16:54:37 -0600 Subject: [PATCH] Bug fixes and QOL improvements - Fixed concurrency and infinite recursion bugs - Resolved issue with having set -e set - Optimized output to be useful --- .hooks/validate-yaml.sh | 21 +++++++++++---------- .pre-commit-config.yaml | 1 + magefiles/magefile.go | 6 ++++++ 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/.hooks/validate-yaml.sh b/.hooks/validate-yaml.sh index 83696c2..9a73bf8 100755 --- a/.hooks/validate-yaml.sh +++ b/.hooks/validate-yaml.sh @@ -6,36 +6,37 @@ # validates all committed YAML files against a predefined schema. If any # validation fails, the commit is stopped and an error message is shown. -set -e - # Change to the repository root -cd "$(git rev-parse --show-toplevel)" +cd "$(git rev-parse --show-toplevel)" || exit 1 # Determine the location of the mage binary mage_bin=$(go env GOPATH)/bin/mage # Check if mage is installed -if [[ -x "${mage_bin}" ]]; then - echo "mage is installed" -else +if [[ ! -x "${mage_bin}" ]]; then echo -e "mage is not installed\n" echo -e "Please install mage by running the following command:\n" echo -e "go install github.com/magefile/mage@latest\n" exit 1 fi -# Get the list of staged files ending with .yaml -staged_files=$(git diff --cached --name-only --diff-filter=AM | grep '\.yaml$') +# Get the list of staged files under ttps directory and ending with .yaml +staged_files=$(git diff --cached --name-only --diff-filter=AM | grep '^ttps/.*\.yaml$') if [[ -z "$staged_files" ]]; then - echo "No YAML files to validate." + echo "This commit has no TTPs that need to be validated." exit 0 fi +echo "Files to validate: $staged_files" + # Iterate over each staged file and validate it for file in $staged_files; do # Run the mage validateyaml command for the staged YAML file - "${mage_bin}" validateyaml docs/ttpforge-spec.yaml "$file" + "${mage_bin}" validateyaml docs/ttpforge-spec.yaml "$file" || { + echo "Failed validation for: $file" + exit 1 + } # Catch the exit code of the last command exit_status=$? diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 956ca28..fff5a71 100755 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -67,3 +67,4 @@ repos: language: script entry: .hooks/validate-yaml.sh files: '\.yaml$' + require_serial: true diff --git a/magefiles/magefile.go b/magefiles/magefile.go index 421c0c5..0dcca39 100755 --- a/magefiles/magefile.go +++ b/magefiles/magefile.go @@ -42,6 +42,8 @@ func init() { os.Setenv("GO111MODULE", "on") } +const maxDepth = 10 + // InstallDeps Installs go dependencies func InstallDeps() error { fmt.Println("Installing dependencies.") @@ -221,6 +223,10 @@ func inspectAndValidate(filePath string, schema map[string]interface{}, validate fmt.Println("YAML is valid according to the schema.") } + if depth >= maxDepth { + return fmt.Errorf("maximum recursion depth reached") + } + return nil }