-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·175 lines (148 loc) · 5.68 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·175 lines (148 loc) · 5.68 KB
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
#!/usr/bin/env bash
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
# or more contributor license agreements. Licensed under the Apache License,
# Version 2.0 (the "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# Elastic Docs Utils installer shim.
# Detects platform, downloads the correct binary from GitHub Releases,
# verifies the checksum, and runs the installer.
#
# Usage:
# curl -sSL https://ela.st/docs-utils-sh | bash
# curl -sSL https://ela.st/docs-utils-sh | bash -s -- --yes
set -euo pipefail
REPO="elastic/docs-utils"
RELEASES_BASE="https://github.com/${REPO}/releases"
INSTALL_DIR="${INSTALL_DIR:-/usr/local/bin}"
BINARY_NAME="elastic-docs-utils"
# Colors (same conventions as elastic-docs-skills)
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
CYAN='\033[0;36m'
BOLD='\033[1m'
NC='\033[0m'
info() { echo -e "${CYAN}${BOLD}ℹ${NC} $1"; }
ok() { echo -e "${GREEN}${BOLD}✓${NC} $1"; }
warn() { echo -e "${YELLOW}${BOLD}⚠${NC} $1"; }
err() { echo -e "${RED}${BOLD}✗${NC} $1" >&2; }
# Detect OS and arch.
detect_platform() {
local os arch
case "$(uname -s)" in
Darwin) os="darwin" ;;
Linux) os="linux" ;;
MINGW*|MSYS*|CYGWIN*) os="windows" ;;
*) err "Unsupported OS: $(uname -s)"; exit 1 ;;
esac
case "$(uname -m)" in
x86_64|amd64) arch="amd64" ;;
arm64|aarch64) arch="arm64" ;;
*) err "Unsupported architecture: $(uname -m)"; exit 1 ;;
esac
# Windows arm64 binary not published; fall back to amd64.
if [[ "$os" == "windows" && "$arch" == "arm64" ]]; then
arch="amd64"
fi
echo "${os}_${arch}"
}
# Fetch the latest release tag from GitHub.
latest_version() {
local tag
tag=$(curl -fsSL "https://api.github.com/repos/${REPO}/releases/latest" \
| grep '"tag_name"' | head -1 | sed -E 's/.*"tag_name": *"([^"]+)".*/\1/')
[[ -z "$tag" ]] && { err "Could not fetch latest release version"; exit 1; }
echo "$tag"
}
main() {
local script_dir platform
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" 2>/dev/null && pwd || pwd)"
platform="$(detect_platform)"
# ── Local-clone mode ─────────────────────────────────────────────────────
# When running from a clone of the repo (private or not), prefer pre-built
# binaries in bin/ over the network. No Go toolchain required.
# 1. Platform-specific binary in bin/ (populated before going public).
local platform_bin="$script_dir/bin/elastic-docs-utils_${platform}"
if [[ -x "$platform_bin" ]]; then
info "Using bundled binary: $platform_bin"
"$platform_bin" "$@"
return $?
fi
# 2. Binary built manually next to the script (developer workflow).
if [[ -x "$script_dir/elastic-docs-utils" ]]; then
info "Using local binary: $script_dir/elastic-docs-utils"
"$script_dir/elastic-docs-utils" "$@"
return $?
fi
# No local binary found — fall through to GitHub Releases download.
# (This is the normal path once the repo is public.)
info "Platform: ${platform}"
local version
version="$(latest_version)"
info "Latest release: ${version}"
local ext="tar.gz"
[[ "$platform" == windows* ]] && ext="zip"
local archive="${BINARY_NAME}_${platform}.${ext}"
local download_url="${RELEASES_BASE}/download/${version}/${archive}"
local checksum_url="${RELEASES_BASE}/download/${version}/checksums.txt"
# Download to a temp directory.
local tmpdir
tmpdir="$(mktemp -d "${TMPDIR:-/tmp}/elastic-docs-utils-XXXXXX")"
trap "rm -rf '$tmpdir'" EXIT
info "Downloading ${archive}..."
curl -fsSL "$download_url" -o "${tmpdir}/${archive}"
# Verify checksum.
info "Verifying checksum..."
local checksums
checksums="$(curl -fsSL "$checksum_url")"
local expected
expected="$(echo "$checksums" | grep "$archive" | awk '{print $1}')"
if [[ -z "$expected" ]]; then
warn "Could not find checksum for ${archive} — skipping verification"
else
local actual
actual="$(sha256sum "${tmpdir}/${archive}" 2>/dev/null | awk '{print $1}' \
|| shasum -a 256 "${tmpdir}/${archive}" | awk '{print $1}')"
if [[ "$actual" != "$expected" ]]; then
err "Checksum mismatch! Expected ${expected}, got ${actual}"
exit 1
fi
ok "Checksum verified"
fi
# Extract binary.
if [[ "$ext" == "zip" ]]; then
unzip -q "${tmpdir}/${archive}" -d "${tmpdir}"
else
tar -xzf "${tmpdir}/${archive}" -C "${tmpdir}"
fi
# Install binary.
local binary_path="${tmpdir}/${BINARY_NAME}"
[[ "$platform" == windows* ]] && binary_path="${binary_path}.exe"
if [[ ! -x "$binary_path" ]]; then
err "Binary not found after extraction: ${binary_path}"
exit 1
fi
# Try to install to INSTALL_DIR; fall back to ~/.local/bin.
local install_path="${INSTALL_DIR}/${BINARY_NAME}"
if ! install -m 755 "$binary_path" "$install_path" 2>/dev/null; then
INSTALL_DIR="${HOME}/.local/bin"
mkdir -p "$INSTALL_DIR"
install_path="${INSTALL_DIR}/${BINARY_NAME}"
install -m 755 "$binary_path" "$install_path"
warn "Installed to ${install_path} (not in /usr/local/bin — ensure ${INSTALL_DIR} is on your PATH)"
fi
ok "Installed Elastic Docs Utils ${version} → ${install_path}"
# Run the installer.
echo ""
"$install_path" "$@"
}
main "$@"