fix: rename execSudo to execRoot and isRoot to isSudo

This commit is contained in:
Amin Yahyaabadi 2022-08-07 17:29:05 -07:00
parent 299694053d
commit bf519e6b3d
11 changed files with 43 additions and 43 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -8,7 +8,7 @@ let isSudoCache: boolean | undefined = undefined
* *
* @note it caches the result for the subsequent calls to this function. * @note it caches the result for the subsequent calls to this function.
*/ */
export function isRoot(): boolean { export function isSudo(): boolean {
if (isSudoCache !== undefined) { if (isSudoCache !== undefined) {
return isSudoCache return isSudoCache
} }
@ -19,14 +19,14 @@ export function isRoot(): boolean {
/** Prepend `sudo` to the command if sudo is available */ /** Prepend `sudo` to the command if sudo is available */
export function prependSudo(command: string) { export function prependSudo(command: string) {
if (isRoot()) { if (isSudo()) {
return `sudo ${command}` return `sudo ${command}`
} }
return command return command
} }
/** /**
* Execute a command as sudo if sudo is available. Otherwise executes the command without sudo. * Execute a command as root if sudo is available. Otherwise executes the command normally without sudo.
* *
* @param program The program to spawn * @param program The program to spawn
* @param args The command arguments * @param args The command arguments
@ -34,8 +34,8 @@ export function prependSudo(command: string) {
* *
* Defaults to `{ stdio: "inherit" }` * Defaults to `{ stdio: "inherit" }`
*/ */
export function execSudo(program: string, args: string[] = [], execOptions: execa.SyncOptions = { stdio: "inherit" }) { export function execRoot(program: string, args: string[] = [], execOptions: execa.SyncOptions = { stdio: "inherit" }) {
if (isRoot()) { if (isSudo()) {
return execa.commandSync(`sudo ${[program, ...args].map((arg) => `'${arg}'`).join(" ")}`, execOptions) return execa.commandSync(`sudo ${[program, ...args].map((arg) => `'${arg}'`).join(" ")}`, execOptions)
} else { } else {
return execa.sync(program, args, execOptions) return execa.sync(program, args, execOptions)

View File

@ -5,7 +5,7 @@ import { isArch } from "../utils/env/isArch"
import { hasDnf } from "../utils/env/hasDnf" import { hasDnf } from "../utils/env/hasDnf"
import { setupDnfPack } from "../utils/setup/setupDnfPack" import { setupDnfPack } from "../utils/setup/setupDnfPack"
import { isUbuntu } from "../utils/env/isUbuntu" import { isUbuntu } from "../utils/env/isUbuntu"
import { execSudo } from "sudo-tools" import { execRoot } from "sudo-tools"
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
export async function setupBazel(version: string, _setupDir: string, _arch: string) { export async function setupBazel(version: string, _setupDir: string, _arch: string) {
@ -24,7 +24,7 @@ export async function setupBazel(version: string, _setupDir: string, _arch: stri
} else if (hasDnf()) { } else if (hasDnf()) {
// https://bazel.build/install/redhat // https://bazel.build/install/redhat
setupDnfPack("dnf-plugins-core", undefined) setupDnfPack("dnf-plugins-core", undefined)
execSudo("dnf", ["copr", "enable", "vbatts/bazel"]) execRoot("dnf", ["copr", "enable", "vbatts/bazel"])
return setupDnfPack("bazel4", undefined) return setupDnfPack("bazel4", undefined)
} else if (isUbuntu()) { } else if (isUbuntu()) {
// https://bazel.build/install/ubuntu // https://bazel.build/install/ubuntu
@ -32,7 +32,7 @@ export async function setupBazel(version: string, _setupDir: string, _arch: stri
"bazel-archive-keyring.gpg", "bazel-archive-keyring.gpg",
"https://bazel.build/bazel-release.pub.gpg" "https://bazel.build/bazel-release.pub.gpg"
) )
execSudo("bash", [ execRoot("bash", [
"-c", "-c",
`echo "deb [arch=amd64 signed-by=${keyFileName}] https://storage.googleapis.com/bazel-apt stable jdk1.8" | tee /etc/apt/sources.list.d/bazel.list`, `echo "deb [arch=amd64 signed-by=${keyFileName}] https://storage.googleapis.com/bazel-apt stable jdk1.8" | tee /etc/apt/sources.list.d/bazel.list`,
]) ])

View File

@ -61,7 +61,7 @@ async function buildKcov(file: string, dest: string) {
stdio: "inherit", stdio: "inherit",
}) })
await execa(cmake, ["--build", buildDir, "--config", "Release"], { cwd: out, stdio: "inherit" }) await execa(cmake, ["--build", buildDir, "--config", "Release"], { cwd: out, stdio: "inherit" })
// execSudo(cmake, ["--install", buildDir], out) // execRoot(cmake, ["--install", buildDir], out)
// return "user/local/bin" // the cmake install prefix // return "user/local/bin" // the cmake install prefix
return out return out
} }

View File

@ -1,7 +1,7 @@
import { dirname } from "path" import { dirname } from "path"
import which from "which" import which from "which"
import { isUbuntu } from "../utils/env/isUbuntu" import { isUbuntu } from "../utils/env/isUbuntu"
import { execSudo } from "sudo-tools" import { execRoot } from "sudo-tools"
import { addAptKeyViaDownload, setupAptPack } from "../utils/setup/setupAptPack" import { addAptKeyViaDownload, setupAptPack } from "../utils/setup/setupAptPack"
let binDir: string | undefined let binDir: string | undefined
@ -26,7 +26,7 @@ export async function setupNala(version: string, _setupDir: string, _arch: strin
"volian-archive-scar-unstable.gpg", "volian-archive-scar-unstable.gpg",
"https://deb.volian.org/volian/scar.key" "https://deb.volian.org/volian/scar.key"
) )
execSudo("/bin/bash", [ execRoot("/bin/bash", [
"-c", "-c",
`echo "deb [signed-by=${keyFileName}] http://deb.volian.org/volian/ scar main" | tee /etc/apt/sources.list.d/volian-archive-scar-unstable.list`, `echo "deb [signed-by=${keyFileName}] http://deb.volian.org/volian/ scar main" | tee /etc/apt/sources.list.d/volian-archive-scar-unstable.list`,
]) ])

View File

@ -1,13 +1,13 @@
import { isRoot } from "sudo-tools" import { isSudo } from "sudo-tools"
import { execSudo } from "sudo-tools" import { execRoot } from "sudo-tools"
/// change the owner to the SUDO_USER. This is required so the user can use the folder without sudo /// change the owner to the SUDO_USER. This is required so the user can use the folder without sudo
export function folderUserAccess(folder: string) { export function folderUserAccess(folder: string) {
if ( if (
(process.platform === "linux" || process.platform === "darwin") && (process.platform === "linux" || process.platform === "darwin") &&
isRoot() && isSudo() &&
process.env.SUDO_USER !== undefined process.env.SUDO_USER !== undefined
) { ) {
execSudo("chown", ["-R", process.env.SUDO_USER, folder], { cwd: folder, stdio: "inherit" }) execRoot("chown", ["-R", process.env.SUDO_USER, folder], { cwd: folder, stdio: "inherit" })
} }
} }

View File

@ -1,9 +1,9 @@
import { join } from "path" import { join } from "path"
import untildify from "untildify" import untildify from "untildify"
import { isRoot } from "sudo-tools" import { isSudo } from "sudo-tools"
export function untildify_user(path: string) { export function untildify_user(path: string) {
if (isRoot() && typeof process.env.SUDO_USER === "string") { if (isSudo() && typeof process.env.SUDO_USER === "string") {
// use the user profile even if root // use the user profile even if root
if (process.platform === "darwin") { if (process.platform === "darwin") {
return join("/Users/", process.env.SUDO_USER, path) return join("/Users/", process.env.SUDO_USER, path)

View File

@ -1,6 +1,6 @@
/* eslint-disable require-atomic-updates */ /* eslint-disable require-atomic-updates */
import { InstallationInfo } from "./setupBin" import { InstallationInfo } from "./setupBin"
import { execSudo } from "sudo-tools" import { execRoot } from "sudo-tools"
import { info } from "@actions/core" import { info } from "@actions/core"
import { isGitHubCI } from "../env/isCI" import { isGitHubCI } from "../env/isCI"
import { addEnv, cpprc_path, setupCppInProfile } from "../env/addEnv" import { addEnv, cpprc_path, setupCppInProfile } from "../env/addEnv"
@ -36,19 +36,19 @@ export async function setupAptPack(
if (Array.isArray(repositories) && repositories.length !== 0) { if (Array.isArray(repositories) && repositories.length !== 0) {
for (const repo of repositories) { for (const repo of repositories) {
// eslint-disable-next-line no-await-in-loop // eslint-disable-next-line no-await-in-loop
execSudo("add-apt-repository", ["--update", "-y", repo]) execRoot("add-apt-repository", ["--update", "-y", repo])
} }
updateRepos(apt) updateRepos(apt)
} }
if (version !== undefined && version !== "") { if (version !== undefined && version !== "") {
try { try {
execSudo(apt, ["install", "--fix-broken", "-y", `${name}=${version}`]) execRoot(apt, ["install", "--fix-broken", "-y", `${name}=${version}`])
} catch { } catch {
execSudo(apt, ["install", "--fix-broken", "-y", `${name}-${version}`]) execRoot(apt, ["install", "--fix-broken", "-y", `${name}-${version}`])
} }
} else { } else {
execSudo(apt, ["install", "--fix-broken", "-y", name]) execRoot(apt, ["install", "--fix-broken", "-y", name])
} }
return { binDir: "/usr/bin/" } return { binDir: "/usr/bin/" }
@ -65,12 +65,12 @@ function getApt() {
} }
function updateRepos(apt: string) { function updateRepos(apt: string) {
execSudo(apt, apt !== "nala" ? ["update", "-y"] : ["update"]) execRoot(apt, apt !== "nala" ? ["update", "-y"] : ["update"])
} }
/** Install apt utils and certificates (usually missing from docker containers) */ /** Install apt utils and certificates (usually missing from docker containers) */
async function initApt(apt: string) { async function initApt(apt: string) {
execSudo(apt, [ execRoot(apt, [
"install", "install",
"--fix-broken", "--fix-broken",
"-y", "-y",
@ -89,7 +89,7 @@ async function initApt(apt: string) {
} }
function initGpg() { function initGpg() {
execSudo("gpg", ["-k"]) execRoot("gpg", ["-k"])
} }
export function addAptKeyViaServer(keys: string[], name: string, server = "keyserver.ubuntu.com") { export function addAptKeyViaServer(keys: string[], name: string, server = "keyserver.ubuntu.com") {
@ -97,7 +97,7 @@ export function addAptKeyViaServer(keys: string[], name: string, server = "keyse
if (!existsSync(fileName)) { if (!existsSync(fileName)) {
initGpg() initGpg()
for (const key of keys) { for (const key of keys) {
execSudo("gpg", [ execRoot("gpg", [
"--no-default-keyring", "--no-default-keyring",
"--keyring", "--keyring",
`gnupg-ring:${fileName}`, `gnupg-ring:${fileName}`,
@ -106,7 +106,7 @@ export function addAptKeyViaServer(keys: string[], name: string, server = "keyse
"--recv-keys", "--recv-keys",
key, key,
]) ])
execSudo("chmod", ["644", fileName]) execRoot("chmod", ["644", fileName])
} }
} }
return fileName return fileName
@ -117,15 +117,15 @@ export async function addAptKeyViaDownload(name: string, url: string) {
if (!existsSync(fileName)) { if (!existsSync(fileName)) {
initGpg() initGpg()
await setupAptPack("curl", undefined) await setupAptPack("curl", undefined)
execSudo("bash", ["-c", `curl -s ${url} | gpg --no-default-keyring --keyring gnupg-ring:${fileName} --import`]) execRoot("bash", ["-c", `curl -s ${url} | gpg --no-default-keyring --keyring gnupg-ring:${fileName} --import`])
execSudo("chmod", ["644", fileName]) execRoot("chmod", ["644", fileName])
} }
return fileName return fileName
} }
export function updateAptAlternatives(name: string, path: string) { export function updateAptAlternatives(name: string, path: string) {
if (isGitHubCI()) { if (isGitHubCI()) {
return execSudo("update-alternatives", ["--install", `/usr/bin/${name}`, name, path, "40"]) return execRoot("update-alternatives", ["--install", `/usr/bin/${name}`, name, path, "40"])
} else { } else {
setupCppInProfile() setupCppInProfile()
return appendFileSync( return appendFileSync(

View File

@ -1,6 +1,6 @@
/* eslint-disable require-atomic-updates */ /* eslint-disable require-atomic-updates */
import { InstallationInfo } from "./setupBin" import { InstallationInfo } from "./setupBin"
import { execSudo } from "sudo-tools" import { execRoot } from "sudo-tools"
import { info, warning } from "../io/io" import { info, warning } from "../io/io"
// let didUpdate: boolean = false // let didUpdate: boolean = false
@ -12,19 +12,19 @@ export function setupDnfPack(name: string, version?: string): InstallationInfo {
const dnf = "dnf" const dnf = "dnf"
// if (!didUpdate) { // if (!didUpdate) {
// execSudo(dnf, ["-y", "check-update"]) // execRoot(dnf, ["-y", "check-update"])
// didUpdate = true // didUpdate = true
// } // }
if (version !== undefined && version !== "") { if (version !== undefined && version !== "") {
try { try {
execSudo(dnf, ["-y", "install", `${name}-${version}`]) execRoot(dnf, ["-y", "install", `${name}-${version}`])
} catch (err) { } catch (err) {
warning(`${(err as Error).toString()}\nInstalling the default version available via dnf`) warning(`${(err as Error).toString()}\nInstalling the default version available via dnf`)
execSudo(dnf, ["-y", "install", name]) execRoot(dnf, ["-y", "install", name])
} }
} else { } else {
execSudo(dnf, ["-y", "install", name]) execRoot(dnf, ["-y", "install", name])
} }
return { binDir: "/usr/bin/" } return { binDir: "/usr/bin/" }

View File

@ -1,6 +1,6 @@
/* eslint-disable require-atomic-updates */ /* eslint-disable require-atomic-updates */
import { InstallationInfo } from "./setupBin" import { InstallationInfo } from "./setupBin"
import { execSudo } from "sudo-tools" import { execRoot } from "sudo-tools"
import { info } from "../io/io" import { info } from "../io/io"
let didUpdate: boolean = false let didUpdate: boolean = false
@ -13,24 +13,24 @@ export function setupPacmanPack(name: string, version?: string, aur?: string): I
const pacman = "pacman" const pacman = "pacman"
if (!didUpdate) { if (!didUpdate) {
execSudo(pacman, ["-Syuu", "--noconfirm"]) execRoot(pacman, ["-Syuu", "--noconfirm"])
didUpdate = true didUpdate = true
} }
if (!didInit) { if (!didInit) {
// install base-devel // install base-devel
execSudo(pacman, ["-Sy", "--noconfirm", "base-devel"]) execRoot(pacman, ["-Sy", "--noconfirm", "base-devel"])
didInit = true didInit = true
} }
if (version !== undefined && version !== "") { if (version !== undefined && version !== "") {
try { try {
execSudo(aur ?? pacman, ["-S", "--noconfirm", `${name}=${version}`]) execRoot(aur ?? pacman, ["-S", "--noconfirm", `${name}=${version}`])
} catch { } catch {
execSudo(aur ?? pacman, ["-S", "--noconfirm", `${name}${version}`]) execRoot(aur ?? pacman, ["-S", "--noconfirm", `${name}${version}`])
} }
} else { } else {
execSudo(aur ?? pacman, ["-S", "--noconfirm", name]) execRoot(aur ?? pacman, ["-S", "--noconfirm", name])
} }
return { binDir: "/usr/bin/" } return { binDir: "/usr/bin/" }