magic-nix-cache-action/src/helpers.ts

54 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-04-26 16:37:43 +02:00
import * as actionsCore from "@actions/core";
import * as fs from "node:fs/promises";
import * as os from "node:os";
import path from "node:path";
import { Tail } from "tail";
export function tailLog(daemonDir: string): Tail {
const log = new Tail(path.join(daemonDir, "daemon.log"));
actionsCore.debug(`tailing daemon.log...`);
log.on("line", (line) => {
actionsCore.info(line);
});
return log;
}
export async function netrcPath(): Promise<string> {
const expectedNetrcPath = path.join(
2024-06-05 18:54:12 +02:00
process.env["RUNNER_TEMP"] ?? os.tmpdir(),
2024-04-26 16:37:43 +02:00
"determinate-nix-installer-netrc",
);
try {
await fs.access(expectedNetrcPath);
return expectedNetrcPath;
} catch {
// `nix-installer` was not used, the user may be registered with FlakeHub though.
const destinedNetrcPath = path.join(
2024-06-05 18:54:12 +02:00
process.env["RUNNER_TEMP"] ?? os.tmpdir(),
2024-04-26 16:37:43 +02:00
"magic-nix-cache-netrc",
);
try {
await flakeHubLogin(destinedNetrcPath);
} catch (e) {
actionsCore.info("FlakeHub cache disabled.");
actionsCore.debug(`Error while logging into FlakeHub: ${e}`);
}
return destinedNetrcPath;
}
}
async function flakeHubLogin(netrc: string): Promise<void> {
const jwt = await actionsCore.getIDToken("api.flakehub.com");
await fs.writeFile(
netrc,
[
`machine api.flakehub.com login flakehub password ${jwt}`,
`machine flakehub.com login flakehub password ${jwt}`,
`machine cache.flakehub.com login flakehub password ${jwt}`,
].join("\n"),
);
actionsCore.info("Logged in to FlakeHub.");
}