diff --git a/src/utils/setup/setupAptPack.ts b/src/utils/setup/setupAptPack.ts index dd03c5db..9392c73d 100644 --- a/src/utils/setup/setupAptPack.ts +++ b/src/utils/setup/setupAptPack.ts @@ -1,19 +1,23 @@ /* eslint-disable require-atomic-updates */ import { exec } from "@actions/exec" +import { mightSudo } from "./sudo" let didUpdate: boolean = false /** A function that installs a package using apt */ -export async function setupAptPack(name: string, version?: string, updateRepositories: boolean = true) { - // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions, @typescript-eslint/no-unnecessary-condition - const apt = process.env.CI || process.getuid?.() === 0 ? "sudo apt-get" : "apt-get" +export async function setupAptPack(name: string, version?: string, repository: boolean | string = true) { + const apt = mightSudo("apt-get") - if (!didUpdate || updateRepositories) { + let exit = 0 + + if (!didUpdate || repository === true) { await exec(apt, ["update"]) didUpdate = true + } else if (typeof repository === "string") { + exit = await exec(mightSudo("add-apt-repository"), ["--update", repository]) } - const exit = await exec(apt, ["install", version !== undefined && version !== "" ? `${name}=${version}` : name]) + exit = await exec(apt, ["install", version !== undefined && version !== "" ? `${name}=${version}` : name]) if (exit !== 0) { throw new Error(`Failed to install ${name} ${version}`)