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
·222 lines (169 loc) · 5.91 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
#!/usr/bin/env sh
# See Helm plugins documentation: https://docs.helm.sh/using_helm/#downloader-plugins
# shellcheck disable=SC2039
set -e -o pipefail
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
[ "$HELM_GIT_DEBUG" = "1" ] && debug=1
## 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'; }
## Error handling
error() {
echo "Error in plugin '$bin_name': $*" >&2
exit 1
}
warning() {
echo "Warning in plugin '$bin_name': $*" >&2
}
## Temporary folders
export TMPDIR=${TMPDIR:-/tmp}
# stashdir_init()
stashdir_init() {
readonly stashdir_list_file=$(mktemp "$TMPDIR/helm-git.stash.XXXXXX")
stashdir_clean_skip=$debug
if [ $debug = 0 ]; then
trap stashdir_clean EXIT
fi
}
# stashdir_new(comment)
stashdir_new() {
_comment="$1"
new_dir=$(mktemp -d "$TMPDIR/helm-git.XXXXXX")
echo "$new_dir" >> "$stashdir_list_file"
echo "$new_dir"
if [ $debug = 1 ]; then
echo "stashdir_new: $_comment ($new_dir)" >&2
fi
}
# stashdir_clean()
stashdir_clean() {
[ "$stashdir_clean_skip" -eq "1" ] && return 0
xargs rm -rf < "$stashdir_list_file" >&2
rm -f "$stashdir_list_file" >&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_sparse_checkout(target_path, git_repo, git_ref, git_path)
git_sparse_checkout() {
_target_path=$1
_git_repo=$2
_git_ref=$3
_git_path=$4
cd "$_target_path" >&2
git init --quiet
git config core.sparseCheckout true
git remote add origin "$_git_repo" >&2
[ -n "$_git_path" ] && echo "$_git_path/*" > .git/info/sparse-checkout
git pull --depth=1 origin "$_git_ref" 2>/dev/null 1>&2 || error \
error "Unable to pull. Check your git_ref and git_path."
}
# 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=$(stashdir_new "helm_package '$_source_path'")
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
}
# 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
[ $debug = 1 ] && echo "input:$_raw_uri" >&2
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
[ $debug = 1 ] && echo "repo:$git_repo ref:$git_ref path:$git_path file:$helm_file" >&2
readonly helm_repo_uri="git+$git_repo@$git_path?ref=$git_ref"
stashdir_init
readonly git_root_path=$(stashdir_new "git_root_path")
readonly git_sub_path=$(path_join "$git_root_path" "$git_path")
git_sparse_checkout "$git_root_path" "$git_repo" "$git_ref" "$git_path" || \
error "Error while git_sparse_checkout"
readonly helm_target_path=$(stashdir_new "helm_target_path")
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=$(stashdir_new "helm home")
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"
}