forked from patternfly/patternfly-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·240 lines (210 loc) · 7.97 KB
/
install.sh
File metadata and controls
executable file
·240 lines (210 loc) · 7.97 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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/env bash
# Install Node (via nvm + Node.js 24 when Node is missing), enable Corepack,
# install GitHub CLI when missing, and install @patternfly/patternfly-cli globally.
set -euo pipefail
NVM_VERSION="${NVM_VERSION:-v0.40.3}"
error() {
printf '\n[%s] ERROR: %s\n\n' "$(date -u +'%Y-%m-%dT%H:%M:%SZ')" "$*" >&2
exit 1
}
info() {
printf '[install] %s\n' "$*"
}
warn() {
printf '[install] WARNING: %s\n' "$*" >&2
}
require_cmd() {
command -v "$1" >/dev/null 2>&1
}
ensure_nvm_loaded() {
export NVM_DIR="${NVM_DIR:-$HOME/.nvm}"
if [ -s "$NVM_DIR/nvm.sh" ]; then
# shellcheck source=/dev/null
. "$NVM_DIR/nvm.sh"
return 0
fi
return 1
}
install_node_via_nvm() {
info "Node.js not found. Installing nvm (${NVM_VERSION}) and Node.js 24."
if ! require_cmd curl && ! require_cmd wget; then
error "Need curl or wget to install nvm. Install one of them and re-run this script."
fi
if [ ! -d "$HOME/.nvm" ]; then
local install_url="https://raw.githubusercontent.com/nvm-sh/nvm/${NVM_VERSION}/install.sh"
if require_cmd curl; then
curl -fsSL "$install_url" | bash || error "Failed to install nvm."
else
wget -qO- "$install_url" | bash || error "Failed to install nvm."
fi
else
info "nvm is already present at ${HOME}/.nvm"
fi
ensure_nvm_loaded || error "nvm was installed but nvm.sh could not be loaded from ${NVM_DIR}/nvm.sh."
nvm install 24 || error "nvm failed to install Node.js 24."
nvm use 24 || error "nvm failed to activate Node.js 24."
nvm alias default 24 2>/dev/null || true
info "Node.js $(node --version) and npm $(npm --version) are ready (npm ships with this Node release)."
}
ensure_node() {
if require_cmd node; then
info "Node.js is already installed: $(command -v node) ($(node --version))"
return 0
fi
install_node_via_nvm
}
ensure_nvm_in_path_for_npm_globals() {
# If node came from nvm but this shell never sourced nvm, try to load it.
if ! require_cmd node; then
ensure_nvm_loaded && nvm use default >/dev/null 2>&1 || true
fi
if require_cmd node; then
return 0
fi
error "Node.js is not available in PATH after setup."
}
install_gh_macos() {
if require_cmd brew; then
info "Installing GitHub CLI with Homebrew."
brew install gh || error "Homebrew failed to install gh. See messages above."
return 0
fi
error "GitHub CLI is not installed and Homebrew was not found. Install Homebrew from https://brew.sh then re-run, or install gh manually from https://cli.github.com/."
}
install_gh_linux_apt() {
info "Installing GitHub CLI with apt (Debian/Ubuntu)."
require_cmd sudo || error "sudo is required to install packages with apt."
sudo apt-get update -y || error "apt-get update failed."
if ! require_cmd curl; then
sudo apt-get install -y curl || error "Failed to install curl (needed for GitHub CLI apt setup)."
fi
sudo install -d -m 755 /etc/apt/keyrings
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg |
sudo tee /etc/apt/keyrings/githubcli-archive-keyring.gpg >/dev/null || error "Failed to add GitHub CLI apt key."
sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" |
sudo tee /etc/apt/sources.list.d/github-cli.list >/dev/null || error "Failed to add GitHub CLI apt source."
sudo apt-get update -y || error "apt-get update failed after adding GitHub CLI source."
sudo apt-get install -y gh || error "apt failed to install gh."
}
install_gh_linux_dnf() {
info "Installing GitHub CLI with dnf (Fedora/RHEL-compatible)."
require_cmd sudo || error "sudo is required to install packages with dnf."
sudo dnf install -y 'dnf-command(config-manager)' || warn "dnf-command(config-manager) may already be installed; continuing."
sudo dnf config-manager --add-repo https://cli.github.com/packages/rpm/gh-cli.repo || error "Failed to add gh dnf repository."
sudo dnf install -y gh || error "dnf failed to install gh."
}
install_gh_linux_yum() {
info "Installing GitHub CLI with yum (older RHEL/CentOS)."
require_cmd sudo || error "sudo is required to install packages with yum."
sudo yum install -y yum-utils || error "yum-utils installation failed."
sudo yum-config-manager --add-repo https://cli.github.com/packages/rpm/gh-cli.repo || error "Failed to add gh yum repository."
sudo yum install -y gh || error "yum failed to install gh."
}
install_gh_linux_pacman() {
info "Installing GitHub CLI with pacman (Arch)."
require_cmd sudo || error "sudo is required to install packages with pacman."
sudo pacman -Sy --noconfirm github-cli || error "pacman failed to install github-cli."
}
install_gh_linux_zypper() {
info "Installing GitHub CLI with zypper (openSUSE)."
require_cmd sudo || error "sudo is required to install packages with zypper."
sudo zypper refresh
sudo zypper install -y gh || error "zypper failed to install gh."
}
install_gh_linux_apk() {
info "Installing GitHub CLI with apk (Alpine)."
require_cmd sudo || error "sudo is required to install packages with apk."
sudo apk add --no-cache github-cli || error "apk failed to install github-cli."
}
install_gh_linux() {
if [ ! -r /etc/os-release ]; then
error "Cannot read /etc/os-release. Install gh manually: https://cli.github.com/manual/installation"
fi
# shellcheck source=/dev/null
. /etc/os-release
local id_lc
id_lc="$(printf '%s' "${ID:-unknown}" | tr '[:upper:]' '[:lower:]')"
case "$id_lc" in
ubuntu | debian | linuxmint | pop | elementary | zorin | kali)
install_gh_linux_apt
;;
fedora | nobara | ultramarine)
install_gh_linux_dnf
;;
rhel | centos | rocky | almalinux)
if require_cmd dnf; then
install_gh_linux_dnf
else
install_gh_linux_yum
fi
;;
arch | manjaro | endeavouros)
install_gh_linux_pacman
;;
opensuse-tumbleweed | opensuse-leap | sled | sles)
install_gh_linux_zypper
;;
alpine)
install_gh_linux_apk
;;
*)
if printf '%s' "${ID_LIKE:-}" | tr '[:upper:]' '[:lower:]' | grep -qE '(debian|ubuntu)'; then
install_gh_linux_apt
elif printf '%s' "${ID_LIKE:-}" | tr '[:upper:]' '[:lower:]' | grep -q 'fedora'; then
install_gh_linux_dnf
elif printf '%s' "${ID_LIKE:-}" | tr '[:upper:]' '[:lower:]' | grep -q 'rhel\|centos'; then
if require_cmd dnf; then
install_gh_linux_dnf
else
install_gh_linux_yum
fi
else
error "Unsupported Linux distribution: ${ID:-unknown}. Install gh manually from https://cli.github.com/manual/installation"
fi
;;
esac
}
ensure_gh() {
if require_cmd gh; then
info "GitHub CLI is already installed: $(command -v gh) ($(gh --version 2>/dev/null | head -n1 || echo 'version unknown'))"
return 0
fi
case "$(uname -s)" in
Darwin)
install_gh_macos
;;
Linux)
install_gh_linux
;;
*)
error "Unsupported OS: $(uname -s). Install gh manually from https://cli.github.com/."
;;
esac
}
enable_corepack_step() {
info "Enabling Corepack."
corepack enable || error "corepack enable failed. Ensure Node.js is recent enough (16.9+) and try again."
}
install_patternfly_cli() {
info "Installing @patternfly/patternfly-cli globally from npm."
npm install -g @patternfly/patternfly-cli || error "npm failed to install @patternfly/patternfly-cli globally. Check permissions and your network."
}
main() {
info "Starting PatternFly CLI environment setup."
ensure_node
ensure_nvm_in_path_for_npm_globals
enable_corepack_step
ensure_gh
install_patternfly_cli
printf '\n'
printf 'SUCCESS: PatternFly CLI is installed.\n'
printf '\n'
printf 'Run the CLI with:\n'
printf ' patternfly-cli --help\n'
printf '\n'
printf 'If you just installed nvm, open a new terminal or run:\n'
printf ' export NVM_DIR="$HOME/.nvm" && [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"\n'
printf '\n'
}
main "$@"