Skip to content

Commit

Permalink
refactor: refine the generate-client.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
zyy17 committed Dec 4, 2024
1 parent 2927e87 commit f67ca42
Showing 1 changed file with 33 additions and 15 deletions.
48 changes: 33 additions & 15 deletions hack/client/generate-client.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env bash

set -euo pipefail

# The Go module path.
GO_MODULE=github.com/GreptimeTeam/greptimedb-operator

Expand All @@ -12,36 +14,52 @@ BOILERPLATE_FILE=hack/boilerplate.go.txt
# The Go bin directory which to download the *-gen binaries.
GOBIN=$(pwd)/bin

# The code generator version.
CODE_GENERATOR_VERSION=v0.30.0

# If the kube_codegen.sh is already in ${GOBIN}, skip the download.
if [ ! -f ${GOBIN}/kube_codegen.sh ]; then
# Download kube_codegen.sh to ${GOBIN}
curl -sSL https://raw.githubusercontent.com/kubernetes/code-generator/refs/tags/${CODE_GENERATOR_VERSION}/kube_codegen.sh -o ${GOBIN}/kube_codegen.sh
# The local path of the kube_codegen.sh.
KUBE_CODEGEN_SCRIPT="${GOBIN}/kube_codegen.sh"

# Create the Go bin directory if not exists.
if [ ! -d "${GOBIN}" ]; then
mkdir -p "${GOBIN}"
fi

source ${GOBIN}/kube_codegen.sh
# Download kube_codegen.sh if not present
if [ ! -f "${KUBE_CODEGEN_SCRIPT}" ]; then
echo "Downloading kube_codegen.sh..."
curl -sSL "https://raw.githubusercontent.com/kubernetes/code-generator/refs/tags/${CODE_GENERATOR_VERSION}/kube_codegen.sh" -o "${KUBE_CODEGEN_SCRIPT}"
chmod +x "${KUBE_CODEGEN_SCRIPT}"
fi

BACKUP_DIR=$(mktemp -d)
# Source the kube_codegen.sh for using the `kube::codegen::*` functions.
source "${KUBE_CODEGEN_SCRIPT}"

echo "Backup the original client directory to ${BACKUP_DIR}"
# Create backup of existing client directory
BACKUP_DIR=$(mktemp -d)
echo "Backing up original client directory to ${BACKUP_DIR}"

# Backup the original client directory.
mv $(pwd)/${OUTPUT_DIR} ${BACKUP_DIR}
if [ -d "$(pwd)/${OUTPUT_DIR}" ]; then
mv "$(pwd)/${OUTPUT_DIR}" "${BACKUP_DIR}"
fi

echo "Generating client code..."
GOBIN=${GOBIN} kube::codegen::gen_client \
$(pwd) \
--output-pkg ${GO_MODULE}/${OUTPUT_DIR} \
--output-dir $(pwd)/${OUTPUT_DIR} \
"$(pwd)" \
--output-pkg "${GO_MODULE}/${OUTPUT_DIR}" \
--output-dir "$(pwd)/${OUTPUT_DIR}" \
--clientset-name clientset \
--versioned-name versioned \
--listers-name listers \
--applyconfig-name applyconfiguration \
--boilerplate ${BOILERPLATE_FILE} \
--boilerplate "${BOILERPLATE_FILE}" \
--with-watch

# Recover the original client directory if the generation failed.
if [ $? -ne 0 ]; then
echo "Client generation failed, recovering the original client directory."
mv ${BACKUP_DIR}/client $(pwd)/${OUTPUT_DIR}
echo "Client generation failed, recovering the original client directory..."
mv "${BACKUP_DIR}/client" "$(pwd)/${OUTPUT_DIR}"
exit 1
fi

echo "Client generation completed successfully"

0 comments on commit f67ca42

Please sign in to comment.