Skip to content

Commit

Permalink
chore: lint (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
aslafy-z authored Apr 11, 2024
1 parent b2f9417 commit 9583dfd
Showing 1 changed file with 58 additions and 60 deletions.
118 changes: 58 additions & 60 deletions helm-git-plugin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,54 +71,54 @@ git_try() {

#git_fetch_ref(git_repo_path, git_ref)
git_fetch_ref() {
_git_repo_path="${1?Missing git_repo_path as first parameter}"
_git_ref="${2?Mising git_ref as second parameter}"
_git_repo_path="${1?Missing git_repo_path as first parameter}"
_git_ref="${2?Mising git_ref as second parameter}"

# Fetches any kind of ref to its right place, tags, annotated tags, branches and commit refs
GIT_DIR="${_git_repo_path}" git fetch -u --depth=1 origin "refs/*/${_git_ref}:refs/*/${_git_ref}" "${_git_ref}" >"${git_output}" 2>&1
# Fetches any kind of ref to its right place, tags, annotated tags, branches and commit refs
GIT_DIR="${_git_repo_path}" git fetch -u --depth=1 origin "refs/*/${_git_ref}:refs/*/${_git_ref}" "${_git_ref}" >"${git_output}" 2>&1
}

#git_cache_intercept(git_repo, git_ref)
git_cache_intercept() {
_git_repo="${1?Missing git_repo as first parameter}"
_git_ref="${2?Missing git_ref as second parameter}"
debug "Trying to intercept for ${_git_repo}#${_git_ref}"
repo_tokens=$(echo "${_git_repo}" | sed -E -e 's/[^/]+\/\/([^@]*@)?([^/]+)\/(.+)$/\2 \3/' -e 's/\.git$//g' )
repo_host=$(echo "${repo_tokens}" | cut -d " " -f1)
repo_repo=$(echo "${repo_tokens}" | cut -d " " -f2)
if [ ! -d "${HELM_GIT_REPO_CACHE}" ]; then
debug "HELM_GIT_REPO_CACHE:${HELM_GIT_REPO_CACHE} is not a directory, cannot cache"
return 1
fi
_git_repo="${1?Missing git_repo as first parameter}"
_git_ref="${2?Missing git_ref as second parameter}"
debug "Trying to intercept for ${_git_repo}#${_git_ref}"
repo_tokens=$(echo "${_git_repo}" | sed -E -e 's/[^/]+\/\/([^@]*@)?([^/]+)\/(.+)$/\2 \3/' -e 's/\.git$//g')
repo_host=$(echo "${repo_tokens}" | cut -d " " -f1)
repo_repo=$(echo "${repo_tokens}" | cut -d " " -f2)
if [ ! -d "${HELM_GIT_REPO_CACHE}" ]; then
debug "HELM_GIT_REPO_CACHE:${HELM_GIT_REPO_CACHE} is not a directory, cannot cache"
return 1
fi

repo_path="${HELM_GIT_REPO_CACHE}/${repo_host}/${repo_repo}"
debug "Calculated cache path for repo ${_git_repo} is ${repo_path}"

if [ ! -d "${repo_path}" ]; then
debug "First time I see ${_git_repo}, setting it up at into ${repo_path}"
{
mkdir -p "${repo_path}" &&
cd "${repo_path}" &&
git init --bare ${git_quiet} >"${git_output}" 2>&1 &&
git remote add origin "${_git_repo}" >"${git_output}" 2>&1
} >&2 || debug "Could not setup ${_git_repo}" && return 1
else
debug "${_git_repo} exists in cache"
fi
debug "Making sure we have the requested ref #${_git_ref}"
if [ -z "$(GIT_DIR="${repo_path}" git tag -l "${_git_ref}" 2>"${git_output}")" ]; then
debug "Did not find ${_git_ref} in our cache for ${_git_repo}, fetching...."
# This fetches properly tags, annotated tags, branches and commits that match the name and leave them at the right place
git_fetch_ref "${repo_path}" "${git_ref}" ||
debug "Could not fetch ${_git_ref}" && return 1
else
debug "Ref ${_git_ref} was already cached for ${_git_repo}"
fi
debug "Tags in the repo: $(GIT_DIR="${repo_path}" git tag -l 2>"${git_output}")"
repo_path="${HELM_GIT_REPO_CACHE}/${repo_host}/${repo_repo}"
debug "Calculated cache path for repo ${_git_repo} is ${repo_path}"

if [ ! -d "${repo_path}" ]; then
debug "First time I see ${_git_repo}, setting it up at into ${repo_path}"
{
mkdir -p "${repo_path}" &&
cd "${repo_path}" &&
git init --bare ${git_quiet} >"${git_output}" 2>&1 &&
git remote add origin "${_git_repo}" >"${git_output}" 2>&1
} >&2 || debug "Could not setup ${_git_repo}" && return 1
else
debug "${_git_repo} exists in cache"
fi
debug "Making sure we have the requested ref #${_git_ref}"
if [ -z "$(GIT_DIR="${repo_path}" git tag -l "${_git_ref}" 2>"${git_output}")" ]; then
debug "Did not find ${_git_ref} in our cache for ${_git_repo}, fetching...."
# This fetches properly tags, annotated tags, branches and commits that match the name and leave them at the right place
git_fetch_ref "${repo_path}" "${git_ref}" ||
debug "Could not fetch ${_git_ref}" && return 1
else
debug "Ref ${_git_ref} was already cached for ${_git_repo}"
fi
debug "Tags in the repo: $(GIT_DIR="${repo_path}" git tag -l 2>"${git_output}")"

new_git_repo="file://${repo_path}"
debug "Returning cached repo at ${new_git_repo}"
echo "${new_git_repo}"
new_git_repo="file://${repo_path}"
debug "Returning cached repo at ${new_git_repo}"
echo "${new_git_repo}"
}

# git_checkout(sparse, target_path, git_repo, git_ref, git_path)
Expand All @@ -142,11 +142,11 @@ git_checkout() {
if [ "$_sparse" = "1" ] && [ -n "$_git_path" ]; then
git config core.sparseCheckout true >"${git_output}" 2>&1
mkdir -p .git/info
echo "$_git_path/*" > .git/info/sparse-checkout
echo "$_git_path/*" >.git/info/sparse-checkout
fi
git_fetch_ref "${PWD}/.git" "${_git_ref}" || \
git_fetch_ref "${PWD}/.git" "${_git_ref}" ||
error "Unable to fetch remote. Check your Git url."
git checkout ${git_quiet} "${_git_ref}" >"${git_output}" 2>&1 || \
git checkout ${git_quiet} "${_git_ref}" >"${git_output}" 2>&1 ||
error "Unable to checkout ref. Check your Git ref ($_git_ref) and path ($_git_path)."
# shellcheck disable=SC2010,SC2012
if [ "$(ls -A | grep -v '^.git$' -c)" = "0" ]; then
Expand Down Expand Up @@ -202,7 +202,7 @@ helm_dependency_update() {
helm_args=${helm_args:-}

# Prevent infinity loop when calling helm-git plugin
if ${HELM_GIT_DEPENDENCY_CIRCUITBREAKER:-false}; then
if ${HELM_GIT_DEPENDENCY_CIRCUITBREAKER:-false}; then
# shellcheck disable=SC2086
"$HELM_BIN" dependency update $helm_args --skip-refresh "$_target_path" >/dev/null
ret=$?
Expand Down Expand Up @@ -249,18 +249,16 @@ main() {
helm_args="" # "$1 $2 $3"
_raw_uri=$4 # eg: git+https://git.com/user/repo@path/to/charts/index.yaml?ref=master


# If defined, use $HELM_GIT_HELM_BIN as $HELM_BIN.
if [ -n "${HELM_GIT_HELM_BIN:-}" ]
then
if [ -n "${HELM_GIT_HELM_BIN:-}" ]; then
export HELM_BIN="${HELM_GIT_HELM_BIN}"
# If not, use $HELM_BIN after sanitizing it or default to 'helm'.
elif
[ -z "$HELM_BIN" ] ||
# terraform-provider-helm: https://github.com/aslafy-z/helm-git/issues/101
echo "$HELM_BIN" | grep -q "terraform-provider-helm" ||
# helm-diff plugin: https://github.com/aslafy-z/helm-git/issues/107
echo "$HELM_BIN" | grep -q "diff"
# terraform-provider-helm: https://github.com/aslafy-z/helm-git/issues/101
echo "$HELM_BIN" | grep -q "terraform-provider-helm" ||
# helm-diff plugin: https://github.com/aslafy-z/helm-git/issues/107
echo "$HELM_BIN" | grep -q "diff"
then
export HELM_BIN="helm"
fi
Expand Down Expand Up @@ -349,19 +347,19 @@ main() {

_cached_file="${_cache_folder}/${helm_file}"
if [ -f "${_cached_file}" ]; then
debug "Returning cached helm request for ${_raw_uri}: ${_cached_file}"
cat "${_cached_file}"
return 0
debug "Returning cached helm request for ${_raw_uri}: ${_cached_file}"
cat "${_cached_file}"
return 0
else
debug "Helm request not found in cache ${_cached_file}"
mkdir -p "${_cache_folder}"
debug "Helm request not found in cache ${_cached_file}"
mkdir -p "${_cache_folder}"
fi
fi

# Setup cleanup trap
# shellcheck disable=SC2317
cleanup() {
rm -rf "$git_root_path" "${helm_home_target_path:-}"
rm -rf "$git_root_path" "${helm_home_target_path:-}"
${CACHE_CHARTS} || rm -rf "${helm_target_path:-}"
}
trap cleanup EXIT
Expand Down Expand Up @@ -423,8 +421,8 @@ main() {
error "Error while helm_dependency_update"
fi
if [ "$helm_package" = "1" ]; then
helm_package "$helm_target_path" "$chart_path" "$chart_name" ||
error "Error while helm_package"
helm_package "$helm_target_path" "$chart_path" "$chart_name" ||
error "Error while helm_package"
fi
done
}
Expand Down

0 comments on commit 9583dfd

Please sign in to comment.