Skip to content

Commit 32cfe10

Browse files
committed
chore: Updated with review comments.
1 parent 9b9c15d commit 32cfe10

File tree

4 files changed

+52
-25
lines changed

4 files changed

+52
-25
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ patternfly-cli [command]
8181
- **`create`**: Create a new project from the available templates.
8282
- **`list`**: List all available templates (built-in and optional custom).
8383
- **`update`**: Update your project to a newer version.
84+
- **`cli-upgrade`**: Upgrade the globally installed CLI to the latest npm release. It runs `npm install -g @patternfly/patternfly-cli@latest`; use your package manager’s equivalent if you did not install with npm.
8485
- **`init`**: Initialize a git repository and optionally create a GitHub repository.
8586
- **`save`**: Commit and push changes to the current branch.
8687
- **`load`**: Pull the latest updates from GitHub.

scripts/install.sh

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ require_cmd() {
2323
command -v "$1" >/dev/null 2>&1
2424
}
2525

26+
# Run a command with elevation only when needed (non-root). Root can omit sudo.
27+
run_as_root() {
28+
if [ "$(id -u)" -eq 0 ]; then
29+
"$@"
30+
else
31+
require_cmd sudo || error "sudo is required to install system packages (install sudo or run this script as root)."
32+
sudo "$@"
33+
fi
34+
}
35+
2636
ensure_nvm_loaded() {
2737
export NVM_DIR="${NVM_DIR:-$HOME/.nvm}"
2838
if [ -s "$NVM_DIR/nvm.sh" ]; then
@@ -89,54 +99,48 @@ install_gh_macos() {
8999

90100
install_gh_linux_apt() {
91101
info "Installing GitHub CLI with apt (Debian/Ubuntu)."
92-
require_cmd sudo || error "sudo is required to install packages with apt."
93-
sudo apt-get update -y || error "apt-get update failed."
102+
run_as_root apt-get update -y || error "apt-get update failed."
94103
if ! require_cmd curl; then
95-
sudo apt-get install -y curl || error "Failed to install curl (needed for GitHub CLI apt setup)."
104+
run_as_root apt-get install -y curl || error "Failed to install curl (needed for GitHub CLI apt setup)."
96105
fi
97-
sudo install -d -m 755 /etc/apt/keyrings
106+
run_as_root install -d -m 755 /etc/apt/keyrings
98107
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg |
99-
sudo tee /etc/apt/keyrings/githubcli-archive-keyring.gpg >/dev/null || error "Failed to add GitHub CLI apt key."
100-
sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg
108+
run_as_root tee /etc/apt/keyrings/githubcli-archive-keyring.gpg >/dev/null || error "Failed to add GitHub CLI apt key."
109+
run_as_root chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg
101110
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" |
102-
sudo tee /etc/apt/sources.list.d/github-cli.list >/dev/null || error "Failed to add GitHub CLI apt source."
103-
sudo apt-get update -y || error "apt-get update failed after adding GitHub CLI source."
104-
sudo apt-get install -y gh || error "apt failed to install gh."
111+
run_as_root tee /etc/apt/sources.list.d/github-cli.list >/dev/null || error "Failed to add GitHub CLI apt source."
112+
run_as_root apt-get update -y || error "apt-get update failed after adding GitHub CLI source."
113+
run_as_root apt-get install -y gh || error "apt failed to install gh."
105114
}
106115

107116
install_gh_linux_dnf() {
108117
info "Installing GitHub CLI with dnf (Fedora/RHEL-compatible)."
109-
require_cmd sudo || error "sudo is required to install packages with dnf."
110-
sudo dnf install -y 'dnf-command(config-manager)' || warn "dnf-command(config-manager) may already be installed; continuing."
111-
sudo dnf config-manager --add-repo https://cli.github.com/packages/rpm/gh-cli.repo || error "Failed to add gh dnf repository."
112-
sudo dnf install -y gh || error "dnf failed to install gh."
118+
run_as_root dnf install -y 'dnf-command(config-manager)' || warn "dnf-command(config-manager) may already be installed; continuing."
119+
run_as_root dnf config-manager --add-repo https://cli.github.com/packages/rpm/gh-cli.repo || error "Failed to add gh dnf repository."
120+
run_as_root dnf install -y gh || error "dnf failed to install gh."
113121
}
114122

115123
install_gh_linux_yum() {
116124
info "Installing GitHub CLI with yum (older RHEL/CentOS)."
117-
require_cmd sudo || error "sudo is required to install packages with yum."
118-
sudo yum install -y yum-utils || error "yum-utils installation failed."
119-
sudo yum-config-manager --add-repo https://cli.github.com/packages/rpm/gh-cli.repo || error "Failed to add gh yum repository."
120-
sudo yum install -y gh || error "yum failed to install gh."
125+
run_as_root yum install -y yum-utils || error "yum-utils installation failed."
126+
run_as_root yum-config-manager --add-repo https://cli.github.com/packages/rpm/gh-cli.repo || error "Failed to add gh yum repository."
127+
run_as_root yum install -y gh || error "yum failed to install gh."
121128
}
122129

123130
install_gh_linux_pacman() {
124131
info "Installing GitHub CLI with pacman (Arch)."
125-
require_cmd sudo || error "sudo is required to install packages with pacman."
126-
sudo pacman -Sy --noconfirm github-cli || error "pacman failed to install github-cli."
132+
run_as_root pacman -Syu --noconfirm github-cli || error "pacman failed to install github-cli."
127133
}
128134

129135
install_gh_linux_zypper() {
130136
info "Installing GitHub CLI with zypper (openSUSE)."
131-
require_cmd sudo || error "sudo is required to install packages with zypper."
132-
sudo zypper refresh
133-
sudo zypper install -y gh || error "zypper failed to install gh."
137+
run_as_root zypper refresh
138+
run_as_root zypper install -y gh || error "zypper failed to install gh."
134139
}
135140

136141
install_gh_linux_apk() {
137142
info "Installing GitHub CLI with apk (Alpine)."
138-
require_cmd sudo || error "sudo is required to install packages with apk."
139-
sudo apk add --no-cache github-cli || error "apk failed to install github-cli."
143+
run_as_root apk add --no-cache github-cli || error "apk failed to install github-cli."
140144
}
141145

142146
install_gh_linux() {

src/__tests__/read-package-version.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,11 @@ describe('readPackageVersion', () => {
1010
expect(readPackageVersion(pkgPath)).toBe(pkg.version);
1111
expect(pkg.version.length).toBeGreaterThan(0);
1212
});
13+
14+
it("returns 'unknown' when the file does not exist", () => {
15+
const log = jest.spyOn(console, 'log').mockImplementation(() => {});
16+
const missing = path.join(process.cwd(), 'nonexistent-package-xyz.json');
17+
expect(readPackageVersion(missing)).toBe('unknown');
18+
log.mockRestore();
19+
});
1320
});

src/read-package-version.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
import fs from 'fs-extra';
22

3-
export function readPackageVersion(packageJsonPath: string): string {
3+
const FALLBACK = 'unknown'
4+
5+
function getVersion(packageJsonPath: string): string {
46
return (fs.readJsonSync(packageJsonPath) as { version: string }).version;
57
}
8+
9+
/** Same as readPackageVersion but never throws; use for CLI bootstrap before commands run. */
10+
export function readPackageVersion(packageJsonPath: string): string {
11+
try {
12+
const version = getVersion(packageJsonPath);
13+
if (typeof version === 'string' && version.length > 0) {
14+
return version;
15+
}
16+
} catch {
17+
console.log("Unable to load package.json to retrieve current cli version.")
18+
}
19+
return FALLBACK;
20+
}

0 commit comments

Comments
 (0)