Skip to content

Commit

Permalink
Merge pull request #202 from kukacz/custom-crd-script
Browse files Browse the repository at this point in the history
Fixed crd_to_kcl.sh to work with latest kcl import structure
  • Loading branch information
Peefy authored Aug 2, 2024
2 parents 4d98e20 + da6e534 commit 9c5c551
Showing 1 changed file with 31 additions and 37 deletions.
68 changes: 31 additions & 37 deletions scripts/crd_to_kcl.sh
Original file line number Diff line number Diff line change
@@ -1,65 +1,59 @@
#!/bin/bash

if [ "$#" -ne 1 ]; then
echo "Usage: $0 <github-repo-url>"
if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then
echo "Usage: $0 <github-repo-url> <version-tag>"
exit 1
fi

REPO_URL="$1"
VER="$2"

REPO_URL_SUFFIX=$(echo $REPO_URL | sed -n 's|.*github.com/||p')
OWNER=$(echo $REPO_URL_SUFFIX | cut -d '/' -f 1)
REPO=$(echo $REPO_URL_SUFFIX | cut -d '/' -f 2)
[ -z "$VER" ] && REPOVER="$REPO" || REPOVER="$REPO@$VER"

export KCL_FAST_EVAL=1

if [ -z "$OWNER" ] || [ -z "$REPO" ]; then
echo "Invalid GitHub repository URL."
exit 1
fi

# Init the kcl module
kcl mod init $REPO
if [ -z "$VER" ]; then
kcl mod init $REPO
else
kcl mod init $REPO --version $VER
fi

cd $REPO

# Get the Kubernetes CRD files
wget "https://doc.crds.dev/raw/github.com/$OWNER/$REPO"
wget "https://doc.crds.dev/raw/github.com/$OWNER/$REPOVER"
mkdir -p crds
mv $REPO crds/$REPO.yaml
mv $REPOVER crds/$REPO.yaml

# Import Kubernetes CRD to KCL files
kcl import -m crd -s ./crds/**
rm -rf main.k

# Add the k8s dependency
kcl mod add k8s
rm -rf models/k8s

models_dir="models"
regex="^(.*)_(v[0-9]+(alpha[0-9]+|beta[0-9]+)?)(.*)$"
rm -rf main.k models/{k8s,kcl.mod}

if [ ! -d "$models_dir" ]; then
echo "The directory '$models_dir' does not exist."
exit 1
fi

cd "$models_dir"

for file in *; do
if [ -f "$file" ]; then
if [[ $file =~ $regex ]]; then
version="${BASH_REMATCH[2]}"
mkdir -p "../$version"
mv "$file" "../$version/"
else
echo "File '$file' does not contain a version number."
fi
fi
done

cd -
echo "Files have been grouped by version."
rm -rf models
# Check KCL runs
for version_dir in *; do
if [ -d "$version_dir" ]; then
echo "Contents of '$version_dir':"
kcl run ./$version_dir
for version_dir in models/*/; do
if [ $(basename $version_dir) == "unknown" ]; then
rm -rf $version_dir
continue
fi
echo "Contents of '$version_dir':"
kcl run $version_dir
mv $version_dir .
done
echo "Files have been grouped and listed by version."
echo "Files have been listed by version."

rmdir models || exit 1

kcl doc generate
mv ./docs/*.md README.md

0 comments on commit 9c5c551

Please sign in to comment.