forked from aslafy-z/helm-git
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelm-git-plugin.sh
executable file
·227 lines (176 loc) · 6.17 KB
/
helm-git-plugin.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env sh
# See Helm plugins documentation: https://docs.helm.sh/using_helm/#downloader-plugins
set -e
readonly bin_name="helm-git"
readonly allowed_protocols="https http file ssh"
readonly url_prefix="git+"
readonly error_invalid_prefix="Git url should start with '$url_prefix'. Please check helm-git usage."
readonly error_invalid_protocol="Protocol not allowed, it should match one of theses: $allowed_protocols."
debug=0
if [ "$HELM_GIT_DEBUG" = "1" ]; then
debug=1
fi
export TMPDIR=${TMPDIR:-/tmp}
## Tooling
string_starts() { [ "$(echo "$1" | cut -c 1-${#2})" = "$2" ]; }
string_ends() { [ "$(echo "$1" | cut -c $((${#1} - ${#2} + 1))-${#1})" = "$2" ]; }
string_contains() { echo "$1" | grep -q "$2"; }
path_join() { echo "${1:+$1/}$2" | sed 's#//#/#g'; }
## Logging
debug() {
[ $debug = 1 ] && echo "Debug in plugin '$bin_name': $*" >&2
return 0
}
error() {
echo "Error in plugin '$bin_name': $*" >&2
exit 1
}
warning() {
echo "Warning in plugin '$bin_name': $*" >&2
}
## Functions
# git_try(git_repo)
git_try() {
_git_repo=$1
GIT_TERMINAL_PROMPT=0 git ls-remote "$_git_repo" --refs >&2 || return 1
}
# git_checkout(sparse, target_path, git_repo, git_ref, git_path)
git_checkout() {
_sparse=$1
_target_path=$2
_git_repo=$3
_git_ref=$4
_git_path=$5
cd "$_target_path" >&2
git init --quiet
git remote add origin "$_git_repo" >&2
# git fetch --tags >&2
if [ "$_sparse" = "1" ]; then
git config core.sparseCheckout true
[ -n "$_git_path" ] && echo "$_git_path/*" >.git/info/sparse-checkout
git pull --quiet --depth 1 origin "$_git_ref" >&2 || error \
error "Unable to sparse-checkout. Check your Git ref ($git_ref) and path ($git_path)."
else
git fetch --quiet origin >&2 || error \
error "Unable to fetch remote. Check your Git url."
git checkout --quiet "$git_ref" >&2 || error \
error "Unable to checkout ref. Check your Git ref ($git_ref)."
fi
}
# helm_init(helm_home)
helm_init() {
_helm_home=$1
helm init --client-only --home "$_helm_home" >/dev/null
HELM_HOME=$_helm_home
export HELM_HOME
}
# helm_package(target_path, source_path, chart_name)
helm_package() {
_target_path=$1
_source_path=$2
_chart_name=$3
tmp_target="$(mktemp -d "$TMPDIR/helm-git.XXXXXX")"
cp -r "$_source_path" "$tmp_target/$_chart_name"
_source_path="$tmp_target/$_chart_name"
cd "$_target_path" >&2
# shellcheck disable=SC2086
helm package $helm_args --save=false "$_source_path" >/dev/null
ret=$?
rm -rf "$tmp_target"
# forward return code
return $ret
}
# helm_dependency_update(target_path)
helm_dependency_update() {
_target_path=$1
# shellcheck disable=SC2086
helm dependency update $helm_args --skip-refresh "$_target_path" >/dev/null
}
# helm_index(target_path, base_url)
helm_index() {
_target_path=$1
_base_url=$2
# shellcheck disable=SC2086
helm repo index $helm_args --url="$_base_url" "$_target_path" >/dev/null
}
# helm_inspect_name(source_path)
helm_inspect_name() {
_source_path=$1
# shellcheck disable=SC2086
output=$(helm inspect chart $helm_args "$_source_path")
name=$(echo "$output" | grep -e '^name: ' | cut -d' ' -f2)
echo "$name"
[ -n "$name" ]
}
# main(raw_uri)
main() {
helm_args="" # "$1 $2 $3"
_raw_uri=$4 # eg: git+https://git.com/user/repo@path/to/charts/index.yaml?ref=master
# Parse URI
string_starts "$_raw_uri" "$url_prefix" ||
error "Invalid format, got '$_raw_uri'. $error_invalid_prefix"
_raw_uri=$(echo "$_raw_uri" | sed 's/^git+//')
readonly git_proto=$(echo "$_raw_uri" | cut -d':' -f1)
string_contains "$allowed_protocols" "$git_proto" ||
error "$error_invalid_protocol"
readonly git_repo=$(echo "$_raw_uri" | sed -E 's#^([^@\?]+)@?[^@\?]+\??.*$#\1#')
# TODO: Validate git_repo
readonly git_path=$(echo "$_raw_uri" | sed -E 's#.*@(.*)\/.*#\1#')
# TODO: Validate git_path
readonly helm_file=$(echo "$_raw_uri" | sed -E 's#.*@.*\/([^?]*).*#\1#')
git_ref=$(echo "$_raw_uri" | sed '/^.*ref=\([^&#]*\).*$/!d;s//\1/')
# TODO: Validate git_ref
if [ -z "$git_ref" ]; then
warning "git_ref is empty, defaulted to 'master'. Prefer to pin GIT ref in URI."
git_ref="master"
fi
git_sparse=$(echo "$_raw_uri" | sed '/^.*sparse=\([^&#]*\).*$/!d;s//\1/')
[ -z "$git_sparse" ] && git_sparse=1
debug "repo: $git_repo ref: $git_ref path: $git_path file: $helm_file sparse: $git_sparse"
readonly helm_repo_uri="git+$git_repo@$git_path?ref=$git_ref&sparse=$git_sparse"
debug "helm_repo_uri: $helm_repo_uri"
case "$helm_file" in
index.yaml) ;;
*.tgz) ;;
*) error "Target file name has to be either 'index.yaml' or a tgz release" ;;
esac
# Setup cleanup trap
cleanup() {
rm -rf "$git_root_path" \
"$helm_home_target_path" \
"$helm_target_path"
}
trap cleanup EXIT
readonly git_root_path="$(mktemp -d "$TMPDIR/helm-git.XXXXXX")"
readonly git_sub_path=$(path_join "$git_root_path" "$git_path")
git_checkout "$git_sparse" "$git_root_path" "$git_repo" "$git_ref" "$git_path" ||
error "Error while git_sparse_checkout"
readonly helm_target_path="$(mktemp -d "$TMPDIR/helm-git.XXXXXX")"
readonly helm_target_file="$(path_join "$helm_target_path" "$helm_file")"
# Set helm home
helm_home=$(helm home)
if [ -z "$helm_home" ]; then
readonly helm_home_target_path="$(mktemp -d "$TMPDIR/helm-git.XXXXXX")"
helm_init "$helm_home_target_path" || error "Couldn't init helm"
helm_home=$helm_home_target_path
fi
helm_args="$helm_args --home=$helm_home"
chart_search_root="$git_sub_path"
chart_search=$(find "$chart_search_root" -maxdepth 2 -name "Chart.yaml" -print)
chart_search_count=$(echo "$chart_search" | wc -l)
echo "$chart_search" | {
while IFS='' read -r chart_yaml_file; do
chart_path=$(dirname "$chart_yaml_file")
chart_name=$(helm_inspect_name "$chart_path")
helm_dependency_update "$chart_path" ||
error "Error while helm_dependency_update"
helm_package "$helm_target_path" "$chart_path" "$chart_name" ||
error "Error while helm_package"
done
}
[ "$chart_search_count" -eq "0" ] &&
error "No charts have been found"
helm_index "$helm_target_path" "$helm_repo_uri" ||
error "Error while helm_index"
cat "$helm_target_file"
}