Skip to content

Commit

Permalink
Bug fixes and QOL improvements
Browse files Browse the repository at this point in the history
- Fixed concurrency and infinite recursion bugs
- Resolved issue with having set -e set
- Optimized output to be useful
  • Loading branch information
l50 committed Sep 7, 2023
1 parent e519eea commit cb6e002
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
21 changes: 11 additions & 10 deletions .hooks/validate-yaml.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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=$?
Expand Down
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,4 @@ repos:
language: script
entry: .hooks/validate-yaml.sh
files: '\.yaml$'
require_serial: true
6 changes: 6 additions & 0 deletions magefiles/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ func init() {
os.Setenv("GO111MODULE", "on")
}

const maxDepth = 10

// InstallDeps Installs go dependencies
func InstallDeps() error {
fmt.Println("Installing dependencies.")
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit cb6e002

Please sign in to comment.