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.
*/
export function isRoot(): boolean {
export function isSudo(): boolean {
if (isSudoCache !== undefined) {
return isSudoCache
}
@ -19,14 +19,14 @@ export function isRoot(): boolean {
/** Prepend `sudo` to the command if sudo is available */
export function prependSudo(command: string) {
if (isRoot()) {
if (isSudo()) {
return `sudo ${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 args The command arguments
@ -34,8 +34,8 @@ export function prependSudo(command: string) {
*
* Defaults to `{ stdio: "inherit" }`
*/
export function execSudo(program: string, args: string[] = [], execOptions: execa.SyncOptions = { stdio: "inherit" }) {
if (isRoot()) {
export function execRoot(program: string, args: string[] = [], execOptions: execa.SyncOptions = { stdio: "inherit" }) {
if (isSudo()) {
return execa.commandSync(`sudo ${[program, ...args].map((arg) => `'${arg}'`).join(" ")}`, execOptions)
} else {
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 { setupDnfPack } from "../utils/setup/setupDnfPack"
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
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()) {
// https://bazel.build/install/redhat
setupDnfPack("dnf-plugins-core", undefined)
execSudo("dnf", ["copr", "enable", "vbatts/bazel"])
execRoot("dnf", ["copr", "enable", "vbatts/bazel"])
return setupDnfPack("bazel4", undefined)
} else if (isUbuntu()) {
// https://bazel.build/install/ubuntu
@ -32,7 +32,7 @@ export async function setupBazel(version: string, _setupDir: string, _arch: stri
"bazel-archive-keyring.gpg",
"https://bazel.build/bazel-release.pub.gpg"
)
execSudo("bash", [
execRoot("bash", [
"-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`,
])

View File

@ -61,7 +61,7 @@ async function buildKcov(file: string, dest: string) {
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 out
}

View File

@ -1,7 +1,7 @@
import { dirname } from "path"
import which from "which"
import { isUbuntu } from "../utils/env/isUbuntu"
import { execSudo } from "sudo-tools"
import { execRoot } from "sudo-tools"
import { addAptKeyViaDownload, setupAptPack } from "../utils/setup/setupAptPack"
let binDir: string | undefined
@ -26,7 +26,7 @@ export async function setupNala(version: string, _setupDir: string, _arch: strin
"volian-archive-scar-unstable.gpg",
"https://deb.volian.org/volian/scar.key"
)
execSudo("/bin/bash", [
execRoot("/bin/bash", [
"-c",
`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 { execSudo } from "sudo-tools"
import { isSudo } 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
export function folderUserAccess(folder: string) {
if (
(process.platform === "linux" || process.platform === "darwin") &&
isRoot() &&
isSudo() &&
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 untildify from "untildify"
import { isRoot } from "sudo-tools"
import { isSudo } from "sudo-tools"
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
if (process.platform === "darwin") {
return join("/Users/", process.env.SUDO_USER, path)

View File

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

View File

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

View File

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