16
16
17
17
# shellcheck disable=SC2034,SC2015
18
18
set -o errexit -o errtrace -o functrace -o nounset -o pipefail
19
+ root=" $( cd " $( dirname " ${BASH_SOURCE[0]:- $PWD } " ) " 2> /dev/null 1>&2 && pwd) "
20
+ readonly root
21
+ # shellcheck source=/dev/null
22
+ . " $root /scripts/lib.sh"
19
23
20
24
# #####################
21
25
# Definitions
@@ -67,141 +71,6 @@ STARGZ_SNAPSHOTTER_CHECKSUM=linux
67
71
# We specifically want the static ones
68
72
TINI_CHECKSUM=static
69
73
70
-
71
- # #####################
72
- # Lib
73
- # #####################
74
-
75
- # Simple logger
76
- readonly LOG_LEVEL_DEBUG=0
77
- readonly LOG_LEVEL_INFO=1
78
- readonly LOG_LEVEL_WARNING=2
79
- readonly LOG_LEVEL_ERROR=3
80
-
81
- readonly LOG_COLOR_BLACK=0
82
- readonly LOG_COLOR_RED=1
83
- readonly LOG_COLOR_GREEN=2
84
- readonly LOG_COLOR_YELLOW=3
85
- readonly LOG_COLOR_BLUE=4
86
- readonly LOG_COLOR_MAGENTA=5
87
- readonly LOG_COLOR_CYAN=6
88
- readonly LOG_COLOR_WHITE=7
89
- readonly LOG_COLOR_DEFAULT=9
90
-
91
- readonly LOG_STYLE_DEBUG=( setaf " $LOG_COLOR_WHITE " )
92
- readonly LOG_STYLE_INFO=( setaf " $LOG_COLOR_GREEN " )
93
- readonly LOG_STYLE_WARNING=( setaf " $LOG_COLOR_YELLOW " )
94
- readonly LOG_STYLE_ERROR=( setaf " $LOG_COLOR_RED " )
95
-
96
- _log::log (){
97
- local level
98
- local style
99
- local numeric_level
100
- local message=" $2 "
101
-
102
- level=" $( printf " %s" " $1 " | tr ' [:lower:]' ' [:upper:]' ) "
103
- numeric_level=" $( printf " LOG_LEVEL_%s" " $level " ) "
104
- style=" LOG_STYLE_${level} [@]"
105
-
106
- [ " ${! numeric_level} " -ge " $LOG_LEVEL " ] || return 0
107
-
108
- [ ! " $TERM " ] || [ ! -t 2 ] || >&2 tput " ${! style} " 2> /dev/null || true
109
- >&2 printf " [%s] %s: %s\n" " $( date 2> /dev/null || true) " " $( printf " %s" " $level " | tr ' [:lower:]' ' [:upper:]' ) " " $message "
110
- [ ! " $TERM " ] || [ ! -t 2 ] || >&2 tput op 2> /dev/null || true
111
- }
112
-
113
- log::init (){
114
- local _ll
115
- # Default log to warning if unspecified
116
- _ll=" $( printf " LOG_LEVEL_%s" " ${NERDCTL_CI_LOG_LEVEL:- warning} " | tr ' [:lower:]' ' [:upper:]' ) "
117
- # Default to 3 (warning) if unrecognized
118
- LOG_LEVEL=" ${! _ll:- 3} "
119
- }
120
-
121
- log::debug (){
122
- _log::log debug " $@ "
123
- }
124
-
125
- log::info (){
126
- _log::log info " $@ "
127
- }
128
-
129
- log::warning (){
130
- _log::log warning " $@ "
131
- }
132
-
133
- log::error (){
134
- _log::log error " $@ "
135
- }
136
-
137
- # Helpers
138
- host::require (){
139
- local binary=" $1 "
140
- command -v " $binary " > /dev/null || {
141
- log::error " You need $binary for this script to work, and it cannot be found in your path"
142
- exit 1
143
- }
144
- }
145
-
146
- fs::mktemp (){
147
- mktemp -dq " ${TMPDIR:-/ tmp} /$prefix .XXXXXX" 2> /dev/null || mktemp -dq || {
148
- log::error " Failed to create temporary directory"
149
- exit 1
150
- }
151
- }
152
-
153
- http::get (){
154
- local args=(curl --proto ' =https' --tlsv1.2 -fsSL)
155
- args+=(" $@ " )
156
-
157
- log::debug " ${args[*]} "
158
- " ${args[@]} "
159
- }
160
-
161
- http::checksum (){
162
- local urls=(" $@ " )
163
- local url
164
-
165
- local prefix=" nerdctl-checksum"
166
-
167
- local temp
168
- temp=" $( fs::mktemp) "
169
-
170
- for url in " ${urls[@]} " ; do
171
- http::get -o " $temp /${url##*/ } " " $url "
172
- done
173
-
174
- cd " $temp "
175
- shasum -a 256 ./*
176
- cd - > /dev/null || true
177
- }
178
-
179
- # Github API helpers
180
- # Set GITHUB_TOKEN to use authenticated requests to workaround limitations
181
- github::request (){
182
- local endpoint=" $1 "
183
- local args=(
184
- -H " Accept: application/vnd.github+json"
185
- -H " X-GitHub-Api-Version: 2022-11-28"
186
- )
187
-
188
- [ " ${GITHUB_TOKEN:- } " == " " ] || args+=(-H " Authorization: Bearer $GITHUB_TOKEN " )
189
-
190
- http::get " ${args[@]} " https://api.github.com/" $endpoint "
191
- }
192
-
193
- github::tags::getlatest (){
194
- local repo=" $1 "
195
- github::request " repos/$repo /tags" |
196
- jq -rc .[0].name
197
- }
198
-
199
- github::releases::latest (){
200
- local repo=" $1 "
201
- github::request " repos/$repo /releases" |
202
- jq -rc .[]
203
- }
204
-
205
74
version::compare (){
206
75
local raw_version_fd=" $1 "
207
76
local parsed
@@ -304,7 +173,7 @@ latest::release(){
304
173
higher_data=" $line "
305
174
higher_readable=" $( echo " $line " | jq -rc .name | sed -E ' s/(.*[ ])?(v?[0-9][0-9.a-z-]+).*/\2/' ) "
306
175
fi
307
- done < <( github::releases::latest " $repo " )
176
+ done < <( github::releases " $repo " )
308
177
309
178
log::info " >>> latest release detected: $higher_readable "
310
179
}
@@ -314,7 +183,7 @@ latest::tag(){
314
183
local repo=" $1 "
315
184
316
185
log::info " Analyzing tags for $repo "
317
- github::tags::getlatest " $repo "
186
+ github::tags::latest " $repo "
318
187
}
319
188
320
189
# Once a latest release has been retrieved for a given project, you can get the url to the asset matching OS and ARCH
@@ -342,13 +211,6 @@ assets::get(){
342
211
}
343
212
}
344
213
345
- log::init
346
- host::require jq
347
- host::require curl
348
- host::require shasum
349
- host::require docker
350
- host::require tput
351
-
352
214
# #####################
353
215
# Script
354
216
# #####################
0 commit comments