@git.zone/cli 9.0.0 → 9.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,6 +4,7 @@
4
4
  set -euo pipefail
5
5
 
6
6
  SHOW_HELP=0
7
+ DRY_RUN=0
7
8
  SPECIFIED_VERSION=""
8
9
  SPECIFIED_MAJOR=""
9
10
  INSTALL_DIR="{{installer.installDir}}"
@@ -15,6 +16,14 @@ REPO_URL="{{{repository.gitUrl}}}"
15
16
  SERVICE_RESTART_NAME="{{installer.service.restartName}}"
16
17
  PRIMARY_PRESERVE_PATH="{{installer.primaryPreservePath}}"
17
18
 
19
+ # Every transfer is an HTTPS release asset or the Gitea API: no other protocol, also not on a redirect,
20
+ # at most 10 redirects, and the same limits as the npm postinstall (30 s to connect or without data,
21
+ # 15 minutes per transfer).
22
+ CURL_OPTS=(--proto '=https' --proto-redir '=https' --max-redirs 10 --connect-timeout 30 --speed-limit 1 --speed-time 30 --max-time 900)
23
+
24
+ # A release tag is a SemVer 2.0 version with an optional v; nothing else goes into a download URL.
25
+ RELEASE_TAG_REGEX='^v?(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-(0|[1-9][0-9]*|[0-9]*[A-Za-z-][0-9A-Za-z-]*)(\.(0|[1-9][0-9]*|[0-9]*[A-Za-z-][0-9A-Za-z-]*))*)?(\+[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?$'
26
+
18
27
  SERVICE_NAMES=(
19
28
  {{#each installer.service.detectNames}}
20
29
  "{{this}}"
@@ -51,6 +60,10 @@ while [[ $# -gt 0 ]]; do
51
60
  shift 2
52
61
  ;;
53
62
  {{/if}}
63
+ --dry-run)
64
+ DRY_RUN=1
65
+ shift
66
+ ;;
54
67
  --install-dir)
55
68
  if [[ $# -lt 2 ]]; then
56
69
  echo "Error: --install-dir requires a value"
@@ -85,11 +98,12 @@ if [[ $SHOW_HELP -eq 1 ]]; then
85
98
  echo ""
86
99
  echo "Options:"
87
100
  echo " -h, --help Show this help message"
88
- echo " --version VERSION Install a specific tag/version (e.g. vX.Y.Z)"
101
+ echo " --version VERSION Install a specific release: vX.Y.Z or X.Y.Z (latest: the newest)"
89
102
  {{#if installer.supportsMajor}}
90
103
  echo " --major MAJOR Install latest release for a major version (e.g. 1)"
91
104
  {{/if}}
92
105
  echo " --install-dir DIR Installation directory (default: {{installer.installDir}})"
106
+ echo " --dry-run Resolve the version and check the release assets; change nothing"
93
107
  {{#if installer.source.enabled}}
94
108
  echo " --binary Install release binary (default)"
95
109
  echo " --source Clone the tag and build from source locally"
@@ -104,20 +118,46 @@ if [[ $SHOW_HELP -eq 1 ]]; then
104
118
  {{#if installer.source.enabled}}
105
119
  echo " curl -sSL {{{installer.publicInstallUrl}}} | sudo bash -s -- --source"
106
120
  {{/if}}
121
+ echo " curl -sSL {{{installer.publicInstallUrl}}} | bash -s -- --dry-run"
107
122
  exit 0
108
123
  fi
109
124
 
125
+ # Succeeds when the value matches the extended regular expression in the C locale. Bash compiles the
126
+ # expression with the current locale's collation, where a range such as [A-Za-z] can match accented letters;
127
+ # the local LC_ALL applies to this match only and leaves the caller's locale as it was.
128
+ matches_ascii_pattern() {
129
+ local LC_ALL=C
130
+ [[ "$1" =~ $2 ]]
131
+ }
132
+
133
+ # Prints the release tag of a version (with its v), or fails when it is not a SemVer 2.0 version.
134
+ normalize_release_tag() {
135
+ if ! matches_ascii_pattern "$1" "$RELEASE_TAG_REGEX"; then
136
+ printf 'Error: %s is not a {{displayName}} release tag: %q\n' "$2" "$1" >&2
137
+ echo "Expected a SemVer version such as v1.2.3, 1.2.3 or v1.2.3-beta.1" >&2
138
+ exit 1
139
+ fi
140
+ echo "v${1#v}"
141
+ }
142
+
143
+ # --version latest is the same as no --version: the newest release.
144
+ if [[ "$SPECIFIED_VERSION" == "latest" ]]; then
145
+ SPECIFIED_VERSION=""
146
+ elif [[ -n "$SPECIFIED_VERSION" ]]; then
147
+ SPECIFIED_VERSION=$(normalize_release_tag "$SPECIFIED_VERSION" "--version argument") || exit 1
148
+ fi
149
+
110
150
  if [[ -n "$SPECIFIED_VERSION" && -n "$SPECIFIED_MAJOR" ]]; then
111
151
  echo "Error: --version and --major are mutually exclusive"
112
152
  exit 1
113
153
  fi
114
154
 
115
- if [[ -n "$SPECIFIED_MAJOR" && ! "$SPECIFIED_MAJOR" =~ ^[0-9]+$ ]]; then
155
+ if [[ -n "$SPECIFIED_MAJOR" ]] && ! matches_ascii_pattern "$SPECIFIED_MAJOR" '^[0-9]+$'; then
116
156
  echo "Error: --major must be a numeric major version, for example: --major 1"
117
157
  exit 1
118
158
  fi
119
159
 
120
- if [[ "$EUID" -ne 0 ]]; then
160
+ if [[ $DRY_RUN -eq 0 && "$EUID" -ne 0 ]]; then
121
161
  echo "Please run as root (sudo bash install.sh or pipe to sudo bash)"
122
162
  exit 1
123
163
  fi
@@ -153,7 +193,7 @@ get_latest_version() {
153
193
  echo "Fetching latest release version from Gitea..." >&2
154
194
  local api_url="${GITEA_BASE_URL}/api/v1/repos/${GITEA_REPO}/releases/latest"
155
195
  local response
156
- if ! response=$(curl -fsSL "$api_url" 2>/dev/null); then
196
+ if ! response=$(curl "${CURL_OPTS[@]}" -fsSL "$api_url" 2>/dev/null); then
157
197
  echo "Error: Failed to fetch latest release information from Gitea API" >&2
158
198
  echo "URL: $api_url" >&2
159
199
  exit 1
@@ -165,7 +205,7 @@ get_latest_version() {
165
205
  echo "Error: Could not determine latest version from API response" >&2
166
206
  exit 1
167
207
  fi
168
- echo "$version"
208
+ normalize_release_tag "$version" "tag_name from the Gitea releases API"
169
209
  }
170
210
 
171
211
  {{#if installer.supportsMajor}}
@@ -174,7 +214,7 @@ get_latest_major_version() {
174
214
  echo "Fetching latest v${major}.x release version from Gitea..." >&2
175
215
  local api_url="${GITEA_BASE_URL}/api/v1/repos/${GITEA_REPO}/releases?limit=50"
176
216
  local response
177
- if ! response=$(curl -fsSL "$api_url" 2>/dev/null); then
217
+ if ! response=$(curl "${CURL_OPTS[@]}" -fsSL "$api_url" 2>/dev/null); then
178
218
  echo "Error: Failed to fetch release list from Gitea API" >&2
179
219
  echo "URL: $api_url" >&2
180
220
  exit 1
@@ -186,7 +226,7 @@ get_latest_major_version() {
186
226
  echo "Error: Could not determine latest v${major}.x version from API response" >&2
187
227
  exit 1
188
228
  fi
189
- echo "$version"
229
+ normalize_release_tag "$version" "tag_name from the Gitea releases API"
190
230
  }
191
231
 
192
232
  {{/if}}
@@ -240,6 +280,109 @@ detect_binary_name() {
240
280
  esac
241
281
  }
242
282
 
283
+ # Sets NOTICE_FILES to the third-party notice files the release ships for an executable.
284
+ select_notice_files() {
285
+ NOTICE_FILES=()
286
+ case "$1" in
287
+ {{#each binary.targets}}
288
+ "{{name}}")
289
+ {{#each noticeFiles}}
290
+ NOTICE_FILES+=("{{this}}")
291
+ {{/each}}
292
+ ;;
293
+ {{/each}}
294
+ esac
295
+ }
296
+
297
+ # The extra release assets installed beside the executable, as name:mode:repository path. The mode is the
298
+ # file's mode in the Git index when the installer was generated.
299
+ EXTRA_FILES=(
300
+ {{#each installer.extraFiles}}
301
+ "{{name}}:{{mode}}:{{path}}"
302
+ {{/each}}
303
+ )
304
+
305
+ # Sets RELEASE_ASSETS to every release file a binary install of the executable downloads.
306
+ select_release_assets() {
307
+ local extra
308
+ select_notice_files "$1"
309
+ RELEASE_ASSETS=("$1" ${NOTICE_FILES[@]+"${NOTICE_FILES[@]}"})
310
+ for extra in ${EXTRA_FILES[@]+"${EXTRA_FILES[@]}"}; do
311
+ RELEASE_ASSETS+=("${extra%%:*}")
312
+ done
313
+ }
314
+
315
+ # The download URL of a release asset of $VERSION.
316
+ release_asset_url() {
317
+ echo "${GITEA_BASE_URL}/${GITEA_REPO}/releases/download/${VERSION}/$1"
318
+ }
319
+
320
+ # Downloads SHA256SUMS.txt of the release into $TEMP_DIR, once.
321
+ fetch_release_sums() {
322
+ local sums_file="$TEMP_DIR/SHA256SUMS.txt"
323
+ if [[ -f "$sums_file" ]]; then
324
+ return 0
325
+ fi
326
+ echo "Downloading checksums: $(release_asset_url SHA256SUMS.txt)"
327
+ if ! curl "${CURL_OPTS[@]}" -fsSL "$(release_asset_url SHA256SUMS.txt)" -o "$sums_file"; then
328
+ rm -f "$sums_file"
329
+ return 1
330
+ fi
331
+ }
332
+
333
+ # Prints the SHA-256 that SHA256SUMS.txt of the release lists for an asset, or nothing.
334
+ listed_checksum() {
335
+ local sum
336
+ local entry
337
+ while read -r sum entry; do
338
+ if [[ "${entry#\*}" == "$1" ]]; then
339
+ echo "$sum"
340
+ return 0
341
+ fi
342
+ done < "$TEMP_DIR/SHA256SUMS.txt"
343
+ }
344
+
345
+ # --dry-run: checks that the release carries every file this installation would download, and changes nothing.
346
+ run_dry_run() {
347
+ local binary_name
348
+ local asset
349
+ local status
350
+ local failed=0
351
+ echo "Dry run: nothing is installed, stopped or changed."
352
+ if [[ "$INSTALL_MODE" == "source" ]]; then
353
+ echo "A source install clones $REPO_URL at $VERSION and builds it."
354
+ exit 0
355
+ fi
356
+ binary_name=$(detect_binary_name) || exit 1
357
+ select_release_assets "$binary_name"
358
+ if ! fetch_release_sums; then
359
+ echo "Error: could not download $(release_asset_url SHA256SUMS.txt)" >&2
360
+ exit 1
361
+ fi
362
+ for asset in ${RELEASE_ASSETS[@]+"${RELEASE_ASSETS[@]}"}; do
363
+ if [[ -z "$(listed_checksum "$asset")" ]]; then
364
+ echo "Error: SHA256SUMS.txt of $VERSION has no entry for $asset" >&2
365
+ failed=1
366
+ fi
367
+ done
368
+ echo "Release assets:"
369
+ for asset in ${RELEASE_ASSETS[@]+"${RELEASE_ASSETS[@]}"}; do
370
+ status=$(curl "${CURL_OPTS[@]}" -sSL -r 0-0 -o /dev/null -w '%{http_code}' "$(release_asset_url "$asset")" 2>/dev/null) || true
371
+ status="${status:-000}"
372
+ echo " $status $(release_asset_url "$asset")"
373
+ case "$status" in
374
+ 200|206) ;;
375
+ *) failed=1 ;;
376
+ esac
377
+ done
378
+ if [[ $failed -ne 0 ]]; then
379
+ echo "Error: dry run failed: $VERSION lacks files this installation needs" >&2
380
+ exit 1
381
+ fi
382
+ echo "Dry run passed: $VERSION carries every file this installation needs."
383
+ exit 0
384
+ }
385
+
243
386
  echo "================================================"
244
387
  echo " {{displayName}} Installation Script"
245
388
  echo "================================================"
@@ -253,11 +396,11 @@ if [[ -n "$SPECIFIED_VERSION" ]]; then
253
396
  echo "Installing specified version: $VERSION"
254
397
  {{#if installer.supportsMajor}}
255
398
  elif [[ -n "$SPECIFIED_MAJOR" ]]; then
256
- VERSION=$(get_latest_major_version "$SPECIFIED_MAJOR")
399
+ VERSION=$(get_latest_major_version "$SPECIFIED_MAJOR") || exit 1
257
400
  echo "Installing latest v${SPECIFIED_MAJOR}.x version: $VERSION"
258
401
  {{/if}}
259
402
  else
260
- VERSION=$(get_latest_version)
403
+ VERSION=$(get_latest_version) || exit 1
261
404
  echo "Installing latest version: $VERSION"
262
405
  fi
263
406
  echo "Install mode: $INSTALL_MODE"
@@ -266,6 +409,7 @@ echo ""
266
409
  TEMP_DIR=$(mktemp -d)
267
410
  SOURCE_DIR="$TEMP_DIR/source"
268
411
  BACKUP_DIR=""
412
+ INSTALL_REPLACED=0
269
413
  SERVICE_WAS_RUNNING=0
270
414
  SERVICE_STOPPED=0
271
415
  SYSTEMD_AVAILABLE=0
@@ -276,9 +420,13 @@ cleanup_temp() {
276
420
  }
277
421
  trap cleanup_temp EXIT
278
422
 
423
+ if [[ $DRY_RUN -eq 1 ]]; then
424
+ run_dry_run
425
+ fi
426
+
279
427
  if command -v systemctl >/dev/null 2>&1; then
280
428
  SYSTEMD_AVAILABLE=1
281
- for service_name in "${SERVICE_NAMES[@]}"; do
429
+ for service_name in ${SERVICE_NAMES[@]+"${SERVICE_NAMES[@]}"}; do
282
430
  if systemctl is-active --quiet "$service_name" 2>/dev/null; then
283
431
  SERVICE_WAS_RUNNING=1
284
432
  break
@@ -290,11 +438,26 @@ if [[ -n "$PRIMARY_PRESERVE_PATH" && -e "$PRIMARY_PRESERVE_PATH" ]]; then
290
438
  PRESERVE_PATH_EXISTS=1
291
439
  fi
292
440
 
441
+ # Puts the moved-aside installation back, or removes an incomplete first installation.
293
442
  restore_previous_installation() {
294
- if [[ -n "$BACKUP_DIR" && -d "$BACKUP_DIR" ]]; then
443
+ if [[ $INSTALL_REPLACED -ne 1 ]]; then
444
+ return 0
445
+ fi
446
+ if [[ -z "$BACKUP_DIR" ]]; then
447
+ echo "Removing the incomplete installation at $INSTALL_DIR"
448
+ rm -rf "$INSTALL_DIR" || true
449
+ if [[ "$(readlink "$BIN_DIR/{{cliName}}" 2>/dev/null || true)" == "$INSTALL_DIR/"* ]]; then
450
+ rm -f "$BIN_DIR/{{cliName}}" || true
451
+ fi
452
+ return 0
453
+ fi
454
+ if [[ -d "$BACKUP_DIR" ]]; then
295
455
  echo "Restoring previous installation from $BACKUP_DIR..."
296
456
  rm -rf "$INSTALL_DIR" || true
297
- mv "$BACKUP_DIR" "$INSTALL_DIR" || true
457
+ if ! mv "$BACKUP_DIR" "$INSTALL_DIR"; then
458
+ echo "Error: could not restore $BACKUP_DIR to $INSTALL_DIR; restore it by hand" >&2
459
+ return 1
460
+ fi
298
461
  if [[ -f "$INSTALL_DIR/{{binary.installFileName}}" ]]; then
299
462
  mkdir -p "$BIN_DIR" || true
300
463
  ln -sf "$INSTALL_DIR/{{binary.installFileName}}" "$BIN_DIR/{{cliName}}" || true
@@ -309,15 +472,31 @@ restore_previous_installation() {
309
472
 
310
473
  restart_previous_service_on_error() {
311
474
  if [[ $SERVICE_STOPPED -eq 1 && $SYSTEMD_AVAILABLE -eq 1 && -n "$SERVICE_RESTART_NAME" ]]; then
312
- echo "Installation failed after stopping {{displayName}}; restarting previous service..."
313
- systemctl start "$SERVICE_RESTART_NAME" || true
475
+ echo "Restarting the previous service: $SERVICE_RESTART_NAME"
476
+ if ! systemctl restart "$SERVICE_RESTART_NAME"; then
477
+ echo "Error: the previous service $SERVICE_RESTART_NAME did not restart; check: systemctl status $SERVICE_RESTART_NAME" >&2
478
+ return 1
479
+ fi
314
480
  fi
315
481
  }
316
482
 
317
- handle_install_error() {
483
+ # Every failure after the current installation was touched ends here: put it back, restart it, exit 1.
484
+ abort_update() {
318
485
  trap - ERR
319
- restore_previous_installation
320
- restart_previous_service_on_error
486
+ echo "Error: $1" >&2
487
+ local restored=0
488
+ restore_previous_installation || restored=1
489
+ restart_previous_service_on_error || restored=1
490
+ if [[ $restored -eq 0 ]]; then
491
+ echo "Error: {{displayName}} $VERSION was not installed; the previous state is restored" >&2
492
+ else
493
+ echo "Error: {{displayName}} $VERSION was not installed, and the previous installation could not be fully restored" >&2
494
+ fi
495
+ exit 1
496
+ }
497
+
498
+ handle_install_error() {
499
+ abort_update "the {{displayName}} installation failed"
321
500
  }
322
501
  trap handle_install_error ERR
323
502
 
@@ -325,11 +504,11 @@ stop_service_if_running() {
325
504
  if [[ $SYSTEMD_AVAILABLE -ne 1 ]]; then
326
505
  return
327
506
  fi
328
- for service_name in "${SERVICE_NAMES[@]}"; do
507
+ for service_name in ${SERVICE_NAMES[@]+"${SERVICE_NAMES[@]}"}; do
329
508
  if systemctl is-active --quiet "$service_name" 2>/dev/null; then
330
509
  echo "Stopping service: $service_name"
331
- systemctl stop "$service_name"
332
510
  SERVICE_STOPPED=1
511
+ systemctl stop "$service_name" || abort_update "could not stop the service $service_name"
333
512
  fi
334
513
  done
335
514
  }
@@ -339,7 +518,7 @@ remove_legacy_services() {
339
518
  return
340
519
  fi
341
520
  local needs_reload=0
342
- for service_name in "${LEGACY_SERVICE_NAMES[@]}"; do
521
+ for service_name in ${LEGACY_SERVICE_NAMES[@]+"${LEGACY_SERVICE_NAMES[@]}"}; do
343
522
  if systemctl is-enabled --quiet "$service_name" 2>/dev/null \
344
523
  || systemctl is-active --quiet "$service_name" 2>/dev/null \
345
524
  || systemctl is-failed --quiet "$service_name" 2>/dev/null \
@@ -365,12 +544,72 @@ remove_legacy_services() {
365
544
  }
366
545
 
367
546
  move_previous_installation() {
368
- mkdir -p "$(dirname "$INSTALL_DIR")"
547
+ mkdir -p "$(dirname "$INSTALL_DIR")" || abort_update "could not create the parent directory of $INSTALL_DIR"
369
548
  if [[ -d "$INSTALL_DIR" ]]; then
370
549
  BACKUP_DIR="${INSTALL_DIR}.previous.$$"
371
550
  echo "Moving previous installation to $BACKUP_DIR"
372
- mv "$INSTALL_DIR" "$BACKUP_DIR"
551
+ mv "$INSTALL_DIR" "$BACKUP_DIR" || abort_update "could not move $INSTALL_DIR aside"
552
+ fi
553
+ INSTALL_REPLACED=1
554
+ }
555
+
556
+ sha256_of() {
557
+ local digest
558
+ if command -v sha256sum >/dev/null 2>&1; then
559
+ digest=$(sha256sum "$1")
560
+ elif command -v shasum >/dev/null 2>&1; then
561
+ digest=$(shasum -a 256 "$1")
562
+ else
563
+ echo "Error: neither sha256sum nor shasum is available to verify the download" >&2
564
+ exit 1
565
+ fi
566
+ echo "${digest%% *}"
567
+ }
568
+
569
+ # Refuses the download unless SHA256SUMS.txt of the release lists it with the same digest.
570
+ verify_release_checksum() {
571
+ local file="$1"
572
+ local name="$2"
573
+ local expected
574
+ local actual
575
+
576
+ if ! fetch_release_sums; then
577
+ echo "Error: could not download $(release_asset_url SHA256SUMS.txt); refusing to install an unverified binary" >&2
578
+ exit 1
579
+ fi
580
+ expected=$(listed_checksum "$name")
581
+ if [[ -z "$expected" ]]; then
582
+ echo "Error: SHA256SUMS.txt of $VERSION has no entry for $name; refusing to install it" >&2
583
+ exit 1
373
584
  fi
585
+ actual=$(sha256_of "$file")
586
+ if [[ "$actual" != "$expected" ]]; then
587
+ echo "Error: checksum mismatch for $name of $VERSION; refusing to install it" >&2
588
+ echo " expected: $expected" >&2
589
+ echo " actual: $actual" >&2
590
+ exit 1
591
+ fi
592
+ echo "Checksum verified: $name"
593
+ }
594
+
595
+ # Copies the verified extra release assets into the installation directory, with their modes.
596
+ install_extra_files() {
597
+ local extra
598
+ local name
599
+ local rest
600
+ for extra in ${EXTRA_FILES[@]+"${EXTRA_FILES[@]}"}; do
601
+ name="${extra%%:*}"
602
+ rest="${extra#*:}"
603
+ install -m "${rest%%:*}" "$1/$name" "$INSTALL_DIR/$name" || return 1
604
+ done
605
+ }
606
+
607
+ # Copies the verified notice files into the installation directory.
608
+ install_notice_files() {
609
+ local notice_file
610
+ for notice_file in ${NOTICE_FILES[@]+"${NOTICE_FILES[@]}"}; do
611
+ install -m 0644 "$1/$notice_file" "$INSTALL_DIR/$notice_file" || return 1
612
+ done
374
613
  }
375
614
 
376
615
  install_release_binary() {
@@ -379,25 +618,64 @@ install_release_binary() {
379
618
  local temp_file
380
619
 
381
620
  binary_name=$(detect_binary_name)
382
- download_url="${GITEA_BASE_URL}/${GITEA_REPO}/releases/download/${VERSION}/${binary_name}"
621
+ download_url=$(release_asset_url "$binary_name")
383
622
  temp_file="$TEMP_DIR/$binary_name"
384
623
 
385
624
  echo "Downloading {{displayName}} binary: $download_url"
386
- curl -fSL "$download_url" -o "$temp_file"
625
+ if ! curl "${CURL_OPTS[@]}" -fSL "$download_url" -o "$temp_file"; then
626
+ echo "Error: could not download $download_url; the current installation is untouched" >&2
627
+ exit 1
628
+ fi
629
+ verify_release_checksum "$temp_file" "$binary_name"
387
630
  chmod 0755 "$temp_file"
388
631
 
632
+ # The third-party notices are installed with the executable, under their release asset names.
633
+ local notice_file
634
+ local notice_dir="$TEMP_DIR/notices"
635
+ select_notice_files "$binary_name"
636
+ mkdir -p "$notice_dir"
637
+ for notice_file in ${NOTICE_FILES[@]+"${NOTICE_FILES[@]}"}; do
638
+ echo "Downloading notices: $(release_asset_url "$notice_file")"
639
+ if ! curl "${CURL_OPTS[@]}" -fSL "$(release_asset_url "$notice_file")" -o "$notice_dir/$notice_file"; then
640
+ echo "Error: could not download the notice file $notice_file of $VERSION; the current installation is untouched" >&2
641
+ exit 1
642
+ fi
643
+ verify_release_checksum "$notice_dir/$notice_file" "$notice_file"
644
+ done
645
+
646
+ # The extra release assets installed beside the executable.
647
+ local extra
648
+ local extra_name
649
+ local extra_dir="$TEMP_DIR/extras"
650
+ mkdir -p "$extra_dir"
651
+ for extra in ${EXTRA_FILES[@]+"${EXTRA_FILES[@]}"}; do
652
+ extra_name="${extra%%:*}"
653
+ echo "Downloading: $(release_asset_url "$extra_name")"
654
+ if ! curl "${CURL_OPTS[@]}" -fSL "$(release_asset_url "$extra_name")" -o "$extra_dir/$extra_name"; then
655
+ echo "Error: could not download $extra_name of $VERSION; the current installation is untouched" >&2
656
+ exit 1
657
+ fi
658
+ verify_release_checksum "$extra_dir/$extra_name" "$extra_name"
659
+ done
660
+
661
+ # The new binary must run before the current installation is touched.
389
662
  echo "Validating downloaded binary..."
390
- "$temp_file" --version >/dev/null
663
+ if ! "$temp_file" --version >/dev/null; then
664
+ echo "Error: the downloaded {{displayName}} $VERSION binary failed '--version'; the current installation is untouched" >&2
665
+ exit 1
666
+ fi
391
667
 
392
668
  stop_service_if_running
393
669
  move_previous_installation
394
670
 
395
671
  echo "Installing binary to $INSTALL_DIR"
396
- mkdir -p "$INSTALL_DIR"
397
- install -m 0755 "$temp_file" "$INSTALL_DIR/{{binary.installFileName}}"
398
-
399
- mkdir -p "$BIN_DIR"
400
- ln -sf "$INSTALL_DIR/{{binary.installFileName}}" "$BIN_DIR/{{cliName}}"
672
+ mkdir -p "$INSTALL_DIR" \
673
+ && install -m 0755 "$temp_file" "$INSTALL_DIR/{{binary.installFileName}}" \
674
+ && install_notice_files "$notice_dir" \
675
+ && install_extra_files "$extra_dir" \
676
+ && mkdir -p "$BIN_DIR" \
677
+ && ln -sf "$INSTALL_DIR/{{binary.installFileName}}" "$BIN_DIR/{{cliName}}" \
678
+ || abort_update "could not install the binary to $INSTALL_DIR"
401
679
  }
402
680
 
403
681
  {{#if installer.source.enabled}}
@@ -409,8 +687,10 @@ install_source_build() {
409
687
  echo "Cloning {{displayName}} source from $REPO_URL ($VERSION)..."
410
688
  git clone --depth 1 --branch "$VERSION" "$REPO_URL" "$SOURCE_DIR"
411
689
 
690
+ # The build and its check must pass before the current installation is touched.
412
691
  echo "Building {{displayName}} from source..."
413
692
  (
693
+ trap 'echo "Error: building or checking {{displayName}} $VERSION from source failed; the current installation is untouched" >&2' ERR
414
694
  cd "$SOURCE_DIR"
415
695
  {{#each installer.source.commands}}
416
696
  {{{this}}}
@@ -422,13 +702,40 @@ install_source_build() {
422
702
  move_previous_installation
423
703
 
424
704
  echo "Installing source build to $INSTALL_DIR"
425
- mv "$SOURCE_DIR" "$INSTALL_DIR"
426
- {{#each installer.source.executableFiles}}
427
- chmod 0755 "$INSTALL_DIR/{{this}}"
428
- {{/each}}
429
-
430
- mkdir -p "$BIN_DIR"
431
- ln -sf "$INSTALL_DIR/{{installer.source.executable}}" "$BIN_DIR/{{cliName}}"
705
+ mv "$SOURCE_DIR" "$INSTALL_DIR" || abort_update "could not install the source build to $INSTALL_DIR"
706
+ # Every listed executable must exist in the build: a missing one is a configuration or build defect.
707
+ for executable_file in{{#each installer.source.executableFiles}} "{{this}}"{{/each}}; do
708
+ if [[ ! -f "$INSTALL_DIR/$executable_file" ]]; then
709
+ abort_update "the source build has no $executable_file, which installer.modes.source.executableFiles lists"
710
+ fi
711
+ chmod 0755 "$INSTALL_DIR/$executable_file" || abort_update "could not make $INSTALL_DIR/$executable_file executable"
712
+ done
713
+ # The extra release assets sit beside the executable as in a binary install, with their committed modes.
714
+ local extra
715
+ local extra_name
716
+ local extra_rest
717
+ local extra_path
718
+ for extra in ${EXTRA_FILES[@]+"${EXTRA_FILES[@]}"}; do
719
+ extra_name="${extra%%:*}"
720
+ extra_rest="${extra#*:}"
721
+ extra_path="${extra_rest#*:}"
722
+ if [[ ! -f "$INSTALL_DIR/$extra_path" ]]; then
723
+ abort_update "the source build has no $extra_path, which installer.extraFiles installs as $extra_name"
724
+ fi
725
+ if [[ "$extra_path" != "$extra_name" ]]; then
726
+ if [[ -e "$INSTALL_DIR/$extra_name" || -L "$INSTALL_DIR/$extra_name" ]]; then
727
+ if [[ -L "$INSTALL_DIR/$extra_name" ]] || ! cmp -s "$INSTALL_DIR/$extra_path" "$INSTALL_DIR/$extra_name"; then
728
+ abort_update "the source build already has a different $extra_name; refusing to overwrite it with $extra_path"
729
+ fi
730
+ else
731
+ cp "$INSTALL_DIR/$extra_path" "$INSTALL_DIR/$extra_name" || abort_update "could not copy $extra_path to $INSTALL_DIR/$extra_name"
732
+ fi
733
+ fi
734
+ chmod "${extra_rest%%:*}" "$INSTALL_DIR/$extra_name" || abort_update "could not set the mode of $INSTALL_DIR/$extra_name"
735
+ done
736
+ mkdir -p "$BIN_DIR" \
737
+ && ln -sf "$INSTALL_DIR/{{installer.source.executable}}" "$BIN_DIR/{{cliName}}" \
738
+ || abort_update "could not link $BIN_DIR/{{cliName}}"
432
739
  }
433
740
 
434
741
  {{/if}}
@@ -448,28 +755,21 @@ fi
448
755
  echo "Symlink created: $BIN_DIR/{{cliName}}"
449
756
 
450
757
  {{#each installer.ensureDirs}}
451
- mkdir -p "{{this}}"
758
+ mkdir -p "{{this}}" || abort_update "could not create {{this}}"
452
759
  {{/each}}
453
760
 
454
761
  if ! "$BIN_DIR/{{cliName}}" --version >/dev/null; then
455
- echo "Error: Installed {{displayName}} CLI failed validation"
456
- restore_previous_installation
457
- restart_previous_service_on_error
458
- exit 1
459
- fi
460
-
461
- if [[ -n "$BACKUP_DIR" && -d "$BACKUP_DIR" ]]; then
462
- rm -rf "$BACKUP_DIR"
762
+ abort_update "the installed {{displayName}} CLI failed '--version'"
463
763
  fi
464
764
 
465
765
  if [[ $SERVICE_WAS_RUNNING -eq 1 && $SYSTEMD_AVAILABLE -eq 1 ]]; then
466
766
  {{#if installer.service.hasRefreshCommand}}
467
767
  echo "Refreshing systemd service..."
468
- {{{installer.service.refreshCommand}}}
768
+ {{{installer.service.refreshCommand}}} || abort_update "refreshing the service failed: {{{installer.service.refreshCommand}}}"
469
769
  {{/if}}
470
770
  if [[ -n "$SERVICE_RESTART_NAME" ]]; then
471
771
  echo "Restarting service: $SERVICE_RESTART_NAME"
472
- systemctl restart "$SERVICE_RESTART_NAME"
772
+ systemctl restart "$SERVICE_RESTART_NAME" || abort_update "the service $SERVICE_RESTART_NAME did not restart with {{displayName}} $VERSION"
473
773
  SERVICE_STOPPED=0
474
774
  echo "Service restarted successfully."
475
775
  echo ""
@@ -478,6 +778,11 @@ fi
478
778
 
479
779
  trap - ERR
480
780
 
781
+ # The backup goes only once the new installation runs.
782
+ if [[ -n "$BACKUP_DIR" && -d "$BACKUP_DIR" ]]; then
783
+ rm -rf "$BACKUP_DIR"
784
+ fi
785
+
481
786
  echo "================================================"
482
787
  echo " {{displayName}} Installation Complete!"
483
788
  echo "================================================"